C#(甚至 CLR)中有多少种内置值类型
我们都知道 C# 中存在 15 种常见值类型:
sbyte, short, int, long, byte, ushort, uint, ulong, char, float, double, decimal, bool, enum, struct
如果单独计算 nullable
对应项,则它们有 30 个。
我记得一本 MS 认证书籍提到“有超过 400 个内置CLR 中的值 类型”,尽管我在网上找不到任何对它的引用,也没有发现其他人提到过它。
所以我很想知道 - 这就是全部吗?如果没有,您如何找到其余的以及您是否在任何项目中使用过它们?
We all know the 15 common value types that exists in C#:
sbyte, short, int, long, byte, ushort, uint, ulong, char, float, double, decimal, bool, enum, struct
If you count nullable
counterpart separately, it makes them 30.
I remember one of the MS certification books mentioning that "there are over 400 builtin value types in CLR", although I cannot find any references to it online, nor have I found anyone else mentioning it.
So I'm curious to know - is that all? If not, how do you find the rest and have you used them in any project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
struct
是一种值类型,因此也计算它们。struct
is a value type, so count those as well.我猜 400 个内置类型包括结构体和枚举。
根据 MSDN,实际类型只是您列出的类型 - http://msdn。 microsoft.com/en-us/library/ya5y69ds.aspx
I'd guess the 400 built in types includes structs and enums.
According to MSDN the actual types are only the ones you listed - http://msdn.microsoft.com/en-us/library/ya5y69ds.aspx
我怀疑它的意思是“在.NET框架中”而不是“在CLR中”。 (C# 定义的类型相对较少。请注意,
enum
和struct
本身不是类型。CLR 本身知道的类型甚至更少 - 它没有对的任何特殊支持例如 >decimal
。)您的列表不包括
DateTime
、TimeSpan
、Guid
、List.Enumerator
等基本上你可以通过反射加载一堆程序集以查找更多内容。我怀疑认证指南实际上指的是框架中的值类型集。I suspect it means "in the .NET framework" rather than "in the CLR". (C# defines relatively few. Note that
enum
andstruct
themselves aren't types. The CLR itself knows of even fewer - it doesn't have any special support fordecimal
for example.)Your list doesn't include things like
DateTime
,TimeSpan
,Guid
,List<T>.Enumerator
etc. Basically you could load a bunch of assemblies with reflection to find more. I suspect the certification guide really meant the set of value types in the framework.任何结构都是值类型,因此如果这个说法准确,我认为它一定意味着包含 BCL(而不是 CLR)中的每个结构。
Well any struct is a value type, so if this statement is accurate I assume it must mean including every struct in the BCL (not the CLR).