简体   繁体   中英

Are the .NET framework libraries considered to be user-defined?

The C# documentation uses the term "user-defined" a fair amount. For instance:

The as operator can't perform... user-defined conversions...

Though I haven't come across a formal definition, I assume that anything not in the C# language specification is user-defined.

That said, I initially thought user-defined meant anything built out of the language (as opposed to being part of the language), but that might not hold water, 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 .

What about anything that's part of the Framework libraries but not part of C#? Yuval's answer indicates that the Framework libraries are not user defined whereas Patricks answer indicates they are.

The full sentence you reference is:

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.

In this case, user-defined conversions are conversions that are not handled by the language itself. For instance, the language handles conversions between numeric types. Those conversions are built into the compiler, and are kind of tricky, since they do not reflect the 'normal' behavior of the language (it is now since they built it).

In those cases not covered by the compiler, you need to do a cast yourself. You can use an conversion operator for that, as explained here on MSDN .

And to directly answer the title question: Yes . The .NET framework libraries ARE considered to be user-defined.

Generally user-defined means anything which is defined by the user(the developer). But from C# perspective, whichever not implemented in CLR or C# will fall into user-defined.

This means that even if is implemented in .Net framework libraries , it is classified as user defined.

For example, There exist an explicit conversion operator from XElement to string ; it is implemented in .Net framework(BCL) as explicit operator. Still that is a user defined converison.

As per the quote below

The as operator can't perform... user-defined conversions...

You can't use as operator for performing user defined conversion (explicit in this case) nevertheless it is provided by BCL.

For example, Following snippet won't compile:

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

So, in this context user-defined is anything which is not provided by C# or CLR ; .Net framework is no exception here.

because it's possible to implement the C# compiler in C#.

It is definitely possible

It seems clear that int and double are not user-defined; it's less clear, though, with String and DateTime.

User-defined conversions means exactly as it looks, conversions defined by the user. You can see it specified clearly in the 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.

Now for this:

What about anything that's part of .NET but not part of C#?

.NET Framework = Base class libraries + Runtime. All types supplied by the BCL are part of the framework. string and DateTime are also a part of the framework. You can, yourself, create different types which are user-defined types , which means you created them because they weren't available in the BCL for you already. There are built in conversations for primitive types which are known by the compiler. Other defined conversions would fall under user-defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM