Examples of Immutable Types in .Net

前端 未结 5 1276
执念已碎
执念已碎 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.

    0 讨论(0)
  • 2021-02-04 11:07

    With .NET 5 and new C# 9.0 release pretty much every object can be immutable now (or contain immutable state in properties)

    More about it here: https://martinstanik.com/2020/10/09/immutable-data-types-after-net-5-release/

    0 讨论(0)
  • 2021-02-04 11:08

    I am not sure if you're looking for publicly immutable types in .NET or types totally immutable at all. Furthermore you want to take care of only the public types in .NET? The deeper problem is defining what forms immutability. Does a class that only has

    public readonly int[] Numbers;
    

    makes it immutable? The Numbers itself can't be changed but its contents can be. You get the idea.

    Anyway you could inspect yourself programmatically. For deeper nested checks you will need recursion (which I wont do here)

    Load all assemblies you wanna check, and do something like (not tested)

    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 wont be enough for finding immutability of collection types. Some of the immutable collection types (though not a part of default .NET core) can be found here: Immutable Collections


    Some notable immutables:

    • some class types like String, Tuple, anonymous types
    • most structs (notable exceptions include most enumerators)
    • enums
    • delegates
    • immutable collection types like

      ImmutableArray (prerelease version)

      ImmutableDictionary

      ImmutableSortedDictionary

      ImmutableHashSet

      ImmutableList

      ImmutableQueue

      ImmutableSortedSet

      ImmutableStack

    0 讨论(0)
  • 2021-02-04 11:13

    Some examples from mscorlib:

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

    Interestingly, string is actually not really immutable; however, we can treat it as a "practically immutable" type, meaning, that the content of a string instance cannot be changed by the public ways (at least, in a safe context). But it has for example a wstrcpy internal method, which is used by the StringBuilder class to manipulate a string instance.

    0 讨论(0)
  • 2021-02-04 11:16

    A list of immutable types in the framework class library follows below. (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 the FCL, and apparently VB.NET types are mutable.)
    • All enumeration types (enum, Enum)
    • All delegate types. (see this answer. While it might seem that delegates are mutable (since you can do things like obj.PropertyChanged += callback, it's actually the obj.PropertyChanged reference that is mutated to point to a newly constructed delegate instance; the original delegate instance stays unchanged.)
    • DateTime, TimeSpan (mentioned in this answer) and DateTimeOffset
    • DBNull
    • Guid
    • Nullable<T>
    • String
    • The Tuple<…> types introduced with .NET 4 (mentioned in this answer)
    • Uri
    • Version
    • Void

    System.Linq.…

    • Lookup<TKey, TElement>
    0 讨论(0)
提交回复
热议问题