Re: Value type and a parameterless constructor

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



For value types you can't explicitly create a parameterless constructor, because the compiler doest that.

Yes, you will still have a parameterless constructor provided by the compiler.


puzzlecracker wrote:
"The C# specification states that all value types have a default
parameterless constructor, and it uses the same syntax to call both
explicitly declared constructors and the parameterless one, relying on
the compiler to do the right thing underneath."

Does it mean that if we create a Value type with other constructors,
we will still have a parameterless constructor provided by compiler?
.



Relevant Pages

  • Re: SV: Creating structs
    ... This means that the compiler knows that it doesn't have to do anything special for an array of structs; it's just filled with zeros the way any memory allocated from the heap would be. ... Likewise for structs found as fields in classes (which also get filled with zeros, not counting instance-initialization -- i.e. parameterless constructor or field initializers -- that _is_ supported for classes and so which might occur). ... If the language supported defining your own parameterless constructor, it would introduce a special case in which the compiler sometimes would have to generate a bunch of code to iterate over an array, or to add implicit calls to the instance-initialization code for class members that are structs. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: remove structs default constructor?
    ... mean that C# is the only language supported. ... a parameterless constructor for value types. ... C# is just the language - *not* the compiler output. ... IL type initializer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Value type and a parameterless constructor
    ... because the compiler doest that. ... you will still have a parameterless constructor provided by the ... This is an interesting impedance mismatch between the CLI spec ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Value type and a parameterless constructor
    ... explicitly declared constructors and the parameterless one, relying on ... we will still have a parameterless constructor provided by compiler? ... I did write a rudimentary test after i posted the question... ...
    (microsoft.public.dotnet.languages.csharp)