Examples of Immutable Types in .Net

前端 未结 5 1275
执念已碎
执念已碎 2021-02-04 10:22

We know the concept of immutability but need to know few immutable types other than

  • String
  • DateTime

Are there more?

5条回答
  •  后悔当初
    2021-02-04 11:04

    TimeSpan, or modern type family Tuple. Tuple is immutable cause it implemented to support functional languages (F# f.e.) in .NET.

    Of course you can make your own classes and structures mutable or immutable, as you wish. Immutable types (aka value objects) are useful in multithreading programming, functional programming, to clear a code.

    To make a class or struct immutable just remove all public/protected/internal setters; and better declare all fields with readonly keyword.

提交回复
热议问题