Examples of immutable types in .Net

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

  • Line
  • Datetime

Is there any more?

+6
source share
4 answers

The following is a list of immutable types in the framework class library. (Feel free to expand it!)

System.…

  • All primitive value types: (Note: not all value types are immutable !)
    • Byte and SByte
    • Int16 and UInt16
    • Int32 and UInt32
    • Int64 and UInt64
    • IntPtr
    • Single
    • Double
  • Decimal
  • All anonymous types created by the compiler ( new { ... } in C #, New With { ... } in VB.NET) (Wrong for two reasons: these types are not in FCL, and apparently, are VB types. NET are mutable.)
  • All enum types ( enum , enum )
  • All types of delegates. (see this answer . Although it might seem that delegates are mutable (since you can do things like obj.PropertyChanged += callback , this is actually a link obj.PropertyChanged , which is mutated to point to the newly created delegate instance, the original instance delegate remains unchanged.)
  • DateTime , TimeSpan (mentioned in this answer ) and DateTimeOffset
  • DBNull
  • Guid
  • Nullable<T>
  • String
  • Tuple<…> types Tuple<…> introduced with .NET 4 (mentioned in this answer )
  • Uri
  • Version
  • Void

System.Linq.…

  • Lookup<TKey, TElement>
+9
source

TimeSpan , or a family of modern type Tuple . Tuple is immutable because it is implemented to support functional languages ​​(F # fe) 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 code.

To make a class or structure immutable, simply delete all public / protected / internal setters; and it is better to declare all fields with the readonly keyword.

+1
source

I'm not sure if you are looking for absolutely immutable types in .NET or types that are absolutely immutable in general. Also, do you only want to care about public types in .NET? A deeper problem is determining which forms are immutable. A class that only has

 public readonly int[] Numbers; 

makes it immutable? The number itself cannot be changed, but its contents can be. You get the idea.

In any case, you can test yourself programmatically. For deeper nested checks you will need a recursion (which I will not do here)

Download all the assemblies you want to test and do something like (not verified)

 var immutables = AppDomain.CurrentDomain .GetAssemblies() .SelectMany(t => t.GetTypes()) .Where(t => t .GetProperties(your binding flags depending on your definition) .All(p => !p.CanWrite) && t .GetFields(your binding flags depending on your definition) .All(f => f.IsInitOnly) .ToList(); 

Even this will not be enough to search for the immutability of collection types. Some of the immutable collection types (although not part of the default .NET kernel) can be found here: Immutable collections


Some notable immutable:

  • some types of classes, such as String , Tuple , anonymous types
  • most structures (notable exceptions include most counters)
  • enums
  • delegates
  • immutable collection types e.g.

    ImmutableArray (Preview)

    ImmutableDictionary

    ImmutableSortedDictionary

    ImmutableHashSet

    ImmutableList

    ImmutableQueue

    ImmutableSortedSet

    ImmutableStack

0
source

Some examples from mscorlib :

  • All primitive types
  • transfers
  • decimal
  • (U)IntPtr
  • DateTime , DateTimeOffset , TimeSpan
  • KeyValuePair<,> - as the opposite, DictionaryEntry not immutable
  • Tuple<...>
  • (Multicast)Delegate - like strings, a new instance is always returned when it is changed.
  • Guid
  • Version

Interestingly, string is not really really immutable; however, we can consider it as a “practically unchanged” type, which means that the contents of an instance of a string cannot be modified in public ways (at least in a safe context). But it has, for example, an internal wstrcpy method, which is used by the StringBuilder class to control the string instance.

0
source

All Articles