vb.net .Net 中的不可变类型示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31721466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Examples of Immutable Types in .Net
提问by Nikhil Agrawal
We know the concept of immutability but need to know few immutable types other than
我们知道不变性的概念,但除了
- String
- DateTime
- 细绳
- 约会时间
Are there more?
还有更多吗?
回答by stakx - no longer contributing
A list of immutable types in the framework class library follows below. (Feel free to expand it!)
框架类库中的不可变类型列表如下。(随意扩展它!)
System.…
System.…
- All primitive value types: (Note: not all value types are immutable!)
ByteandSByteInt16andUInt16Int32andUInt32Int64andUInt64IntPtrSingleDouble
DecimalAll anonymous types created by the compiler ((Wrong for two reasons: These types are not in the FCL, and apparently VB.NET types are mutable.)new { ... }in C#,New With { ... }in VB.NET)- 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 theobj.PropertyChangedreference that is mutated to point to a newly constructed delegate instance; the original delegate instance stays unchanged.) DateTime,TimeSpan(mentioned in this answer)andDateTimeOffsetDBNullGuidNullable<T>String- The
Tuple<…>types introduced with .NET 4 (mentioned in this answer) UriVersionVoid
- 所有原始值类型:(注意:并非所有值类型都是不可变的!)
Byte和SByteInt16和UInt16Int32和UInt32Int64和UInt64IntPtrSingleDouble
Decimal编译器创建的所有匿名类型((错误原因有两个:这些类型不在 FCL 中,而且显然 VB.NET 类型是可变的。)new { ... }在 C# 中,New With { ... }在 VB.NET 中)- 所有枚举类型 (
enum,Enum) - 所有委托类型。(请参阅此答案。虽然看起来委托是可变的(因为您可以执行诸如 之类的操作
obj.PropertyChanged += callback,但实际上obj.PropertyChanged引用已发生变化以指向新构造的委托实例;原始委托实例保持不变。) DateTime,TimeSpan(在这个答案中提到)和DateTimeOffsetDBNullGuidNullable<T>StringTuple<…>.NET 4 引入的类型(在这个答案中提到)UriVersionVoid
System.Linq.…
System.Linq.…
Lookup<TKey, TElement>
Lookup<TKey, TElement>
回答by nawfal
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
我不确定您是在 .NET 中寻找公开的不可变类型还是完全不可变的类型。此外,您只想处理 .NET 中的公共类型?更深层次的问题是定义什么形成了不变性。是否有一个只有
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
即使这不足以找到集合类型的不变性。一些不可变集合类型(虽然不是默认 .NET 核心的一部分)可以在这里找到: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
- 一些类类型,如
String,Tuple, 匿名类型 - 大多数结构(值得注意的例外包括大多数枚举器)
- 枚举
- 代表
不可变的集合类型,如
ImmutableArray(预发行版)
不可变字典
不可变排序字典
不可变哈希集
不可变列表
不可变队列
不可变排序集
不可变栈
回答by Mark Shevchenko
TimeSpan, or modern type family Tuple. Tupleis immutable cause it implemented to support functional languages (F# f.e.) in .NET.
TimeSpan,或现代类型家庭Tuple。Tuple是不可变的,因为它实现了支持 .NET 中的函数式语言 (F# fe)。
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 readonlykeyword.
要使类或结构不可变,只需删除所有公共/受保护/内部设置器;并更好地使用readonly关键字声明所有字段。
回答by Gy?rgy K?szeg
Some examples from mscorlib:
来自mscorlib 的一些例子:
- All of the primitive types
- enums
decimal(U)IntPtrDateTime,DateTimeOffset,TimeSpanKeyValuePair<,>- as an opposite,DictionaryEntryis not immutableTuple<...>(Multicast)Delegate- similarly to strings, always a new instance is returned when it is changed.GuidVersion
- 所有原始类型
- 枚举
decimal(U)IntPtrDateTime,DateTimeOffset,TimeSpanKeyValuePair<,>- 相反,DictionaryEntry不是一成不变的Tuple<...>(Multicast)Delegate- 与字符串类似,更改时总是返回一个新实例。GuidVersion
Interestingly, stringis 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 wstrcpyinternal method, which is used by the StringBuilderclass to manipulate a string instance.
有趣的string是,实际上并不是一成不变的;但是,我们可以将其视为“实际上不可变”类型,这意味着字符串实例的内容不能通过公共方式更改(至少在安全上下文中)。但是它有一个wstrcpy内部方法,StringBuilder类使用它来操作字符串实例。

