.NET 框架库是否被认为是用户定义的?
Are the .NET framework libraries considered to be user-defined?
C# 文档大量使用了术语 "user-defined"。例如:
The as operator can't perform... user-defined conversions...
虽然我还没有找到正式的定义,但我假设 C# 语言规范中 而非 的任何内容都是用户定义的。
就是说,我最初认为用户定义意味着任何由语言构建的东西(而不是语言的一部分),但这可能站不住脚,因为可以在 C# 中实现 C# 编译器。
很明显int
和double
不是用户定义的;但是,String
和 DateTime
不太清楚。
那些属于框架库但不属于 C# 的东西呢? Yuval 的回答表明框架库不是用户定义的,而 Patricks 的回答表明它们是。
Note that the as operator performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.
在这种情况下,user-defined 转换是语言本身不处理的转换。例如,该语言处理数字类型之间的转换。这些转换内置于编译器中,有点棘手,因为它们不反映语言的 'normal' 行为(自从他们构建它以来就是现在)。
在编译器未涵盖的情况下,您需要自己进行转换。您可以为此使用转换运算符,如此处所述 on MSDN.
并直接回答标题问题:是。 .NET 框架库被认为是 user-defined.
because it's possible to implement the C# compiler in C#.
It seems clear that int and double are not user-defined; it's less
clear, though, with String and DateTime.
User-defined conversions 是指用户自定义的转换。您可以在 User-Defined Conversions Tutorial:
中清楚地看到它
C# allows programmers to declare conversions on classes or structs so
that classes or structs can be converted to and/or from other classes
or structs, or basic types. Conversions are defined like operators and
are named for the type to which they convert.
现在:
What about anything that's part of .NET but not part of C#?
.NET Framework = 基础 class 库 + 运行时。 BCL 提供的所有类型都是框架的一部分。 string
和 DateTime
也是框架的一部分。您可以自己创建不同的类型,它们是 用户定义的类型 ,这意味着您创建它们是因为 BCL 中还没有这些类型供您使用。内置了编译器已知的原始类型的对话。其他定义的转换属于用户定义。
通常用户定义的意思是用户(开发者)定义的任何东西。但是从C#的角度来看,CLR和C#中没有实现的都属于用户自定义。
这意味着即使在 .Net 框架库中实现,它也被归类为用户定义。
例如,存在从XElement
到string
的显式转换运算符;它作为显式运算符在 .Net 框架 (BCL) 中实现。这仍然是用户定义的转换。
根据下面的引述
The as operator can't perform... user-defined conversions...
您不能使用 as
运算符来执行用户定义的转换(在这种情况下是显式的),但它是由 BCL 提供的。
例如,以下代码段将无法编译:
string someString = "someString";
XElement element = new XElement("SomeElement", "Value");
someString = (string)element;//Cast just works fine
someString = element as string;//You can't use as keyword here
因此,在此上下文中,用户定义的是 C# 或 CLR 未提供的任何内容; .Net 框架也不例外。
C# 文档大量使用了术语 "user-defined"。例如:
The as operator can't perform... user-defined conversions...
虽然我还没有找到正式的定义,但我假设 C# 语言规范中 而非 的任何内容都是用户定义的。
就是说,我最初认为用户定义意味着任何由语言构建的东西(而不是语言的一部分),但这可能站不住脚,因为可以在 C# 中实现 C# 编译器。
很明显int
和double
不是用户定义的;但是,String
和 DateTime
不太清楚。
那些属于框架库但不属于 C# 的东西呢? Yuval 的回答表明框架库不是用户定义的,而 Patricks 的回答表明它们是。
Note that the as operator performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.
在这种情况下,user-defined 转换是语言本身不处理的转换。例如,该语言处理数字类型之间的转换。这些转换内置于编译器中,有点棘手,因为它们不反映语言的 'normal' 行为(自从他们构建它以来就是现在)。
在编译器未涵盖的情况下,您需要自己进行转换。您可以为此使用转换运算符,如此处所述 on MSDN.
并直接回答标题问题:是。 .NET 框架库被认为是 user-defined.
because it's possible to implement the C# compiler in C#.
It seems clear that int and double are not user-defined; it's less clear, though, with String and DateTime.
User-defined conversions 是指用户自定义的转换。您可以在 User-Defined Conversions Tutorial:
中清楚地看到它C# allows programmers to declare conversions on classes or structs so that classes or structs can be converted to and/or from other classes or structs, or basic types. Conversions are defined like operators and are named for the type to which they convert.
现在:
What about anything that's part of .NET but not part of C#?
.NET Framework = 基础 class 库 + 运行时。 BCL 提供的所有类型都是框架的一部分。 string
和 DateTime
也是框架的一部分。您可以自己创建不同的类型,它们是 用户定义的类型 ,这意味着您创建它们是因为 BCL 中还没有这些类型供您使用。内置了编译器已知的原始类型的对话。其他定义的转换属于用户定义。
通常用户定义的意思是用户(开发者)定义的任何东西。但是从C#的角度来看,CLR和C#中没有实现的都属于用户自定义。
这意味着即使在 .Net 框架库中实现,它也被归类为用户定义。
例如,存在从XElement
到string
的显式转换运算符;它作为显式运算符在 .Net 框架 (BCL) 中实现。这仍然是用户定义的转换。
根据下面的引述
The as operator can't perform... user-defined conversions...
您不能使用 as
运算符来执行用户定义的转换(在这种情况下是显式的),但它是由 BCL 提供的。
例如,以下代码段将无法编译:
string someString = "someString";
XElement element = new XElement("SomeElement", "Value");
someString = (string)element;//Cast just works fine
someString = element as string;//You can't use as keyword here
因此,在此上下文中,用户定义的是 C# 或 CLR 未提供的任何内容; .Net 框架也不例外。