2

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.

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • Is there anywhere specific that you have been unsure of the usage? It normally means something created by the developer (i.e you) – Sayse Mar 16 '15 at 15:23
  • [MSDN: User-Defined Conversions Tutorial](https://msdn.microsoft.com/en-us/library/aa288476%28v=vs.71%29.aspx) – Tim Schmelter Mar 16 '15 at 15:25
  • 1
    @ShaunLuttin - Apologies I seem to have misread the quote (somehow). The user is always the person that is using the .Net Framework, you/your team/the developer(s) – Sayse Mar 16 '15 at 15:30
  • In this context user-defined means implicit and explicit operators. I'm not sure why are you confused about `DateTime` and `String`. Obviously they are not user defined. – Sriram Sakthivel Mar 16 '15 at 15:34

3 Answers3

2

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.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

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.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
0

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.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Who exactly is "the user"? For instance, are those who write the .NET core libraries considered users? – Shaun Luttin Mar 16 '15 at 15:27
  • http://stackoverflow.com/questions/631059/was-c-sharp-compiler-written-in-c indicates that it's possible to implement the compiler in C#, though I lack the knowledge to confirm it. – Shaun Luttin Mar 16 '15 at 15:29
  • @ShaunLuttin The new C# compiler is written in C#, and is called "Roslyn". See my link. Who exactly is a user? Anyone who writes code that isn't supplied out of the box for you with the .NET framework. – Yuval Itzchakov Mar 16 '15 at 15:30
  • Removing the "Really?" would improve the post. – Shaun Luttin Mar 16 '15 at 15:40
  • If someone other than me writes a library that's not part of the BCL, and then I reference his/her library, then is all of that considered user-defined also? – Shaun Luttin Mar 16 '15 at 15:42