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
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.
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.
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)
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++.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With