Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between C# & CLI when it comes in with value types and constructors?

I read recently that the C# and CLI standards define different ways to handle value types and constructors.

According to the CLI specification value types can't have parameterless constructors, whereas in the C# specification value types have a default parameterless constructor. If, according to the CLI specification, you need to create a value without specifying any parameters, there's a special instruction to do that with.

So my questions are

  • why would the C# designers intentionally deviate from the CLI standard - what benefit was there to this, and why doesn't the CLI allow this functionality?
  • in my limited experience, any time I find myself using a 'special instruction' to allow for functionality that wasn't initially intended, it's usually a bit of a hack. How is this different?
like image 479
DaveDev Avatar asked Aug 05 '10 09:08

DaveDev


People also ask

What's the difference between C C+ and C++?

Difference between C and C++. The main difference between both these languages is C is a procedural programming language and does not support classes and objects, while C++ is a combination of both procedural and object-oriented programming languages.

Is C or C+ Better?

Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.

Is C+ a thing?

C+ (grade), an academic grade. C++, a programming language. C with Classes, predecessor to the C++ programming language. ANSI C, a programming language (as opposed to K&R C)

What is difference in C language?

The original C programming language is not object-oriented, which is the most significant difference between the two. C is what's called a “procedural” programming language, while C++ is a hybrid language that's a combination of procedural and object-oriented. There are other key differences between C and C++.


1 Answers

In various places, it makes sense from a consistency point of view to think of value types as having a parameterless constructor. You can always create a value without providing any arguments, and that's true in both the CLI and C#. In C#, you can just use standard constructor syntax:

int x = new int();

rather than there being one syntax for this and a different syntax for invoking a "real" constructor.

Note that as of C# 2, there's the default value operator which I suppose could have been used instead:

int x = default(int);

That feels closer to the IL generated, really. I suppose it's just possible that if we'd had that to start with, C# wouldn't have "pretended" that all value types have parameterless constructors.

On the other hand, consider generics:

public void Foo<T>() where T : new()
{
    T t = new T();
}

Should that be allowed for value types? It is - but if C# didn't allow new int() then it wouldn't make much sense to allow it in a generic form...

One interesting point you may want to look at in more depth - although C# won't let you define a custom value type parameterless constructor, you can do so in IL, and C# will sometimes use it (and sometimes not) depending on the context. See my blog entry for more details.

like image 171
Jon Skeet Avatar answered Nov 15 '22 08:11

Jon Skeet