Why isn't the new() generic constraint satisfied by a class with optional parameters in the constructor?

While this logically should work, it unfortunately does not. The CLR still sees your constructor as a parameter-based constructor Remember that, while C# supports optional parameters, this is done at the compiler level, at compile time. The underlying type still only contains a constructor taking a single parameter .

As far as the CLR is concerned, the "default parameters" are converted to attributes, like so: public Widget((Optional, DefaultParameterValue("foo") string name) { The CLR is a multi-language runtime. Generics are made to work at the CLR level, for all languages, so the constraints must be true in languages without default parameters, as well. Languages are not required to understand the OptionalAttribute, nor the DefaultParameterValueAttribute, so this cannot work uniformly for all languages, hence it's not allowed Edit: In response to your comment: What I don't understand is why the C# compiler cannot generate the necessary code to satisfy the CLR Theoretically, the C# compiler team could have the language generate two separate constructors, instead of one constructor marked with attributes.

This would, potentially, explode into many constructors, as named parameters create the capabilities for many, many possible combinations of "constructors" (or method calls for methods), especially when multiple arguments are available. I personally am glad that they did not, since it would cause confusion due to an overabundance of methods and constructors in the generated types, which would cause the public API to look very different than the code that generated it. Take the following constructor: public Widget( int id = 0, string name = "foo", float width=1.0f, float height=1.0f, float depth=1.0f ) { Were you to automatically generate all of the possible combinations here, the compiler would need to generate 120 constructors for this single constructor, since there are N!

Possible ways to call this.

While this logically should work, it unfortunately does not. The CLR still sees your constructor as a parameter-based constructor. Remember that, while C# supports optional parameters, this is done at the compiler level, at compile time.

The underlying type still only contains a constructor taking a single parameter . As far as the CLR is concerned, the "default parameters" are converted to attributes, like so: public Widget((Optional, DefaultParameterValue("foo") string name) { // ... The CLR is a multi-language runtime. Generics are made to work at the CLR level, for all languages, so the constraints must be true in languages without default parameters, as well.

Languages are not required to understand the OptionalAttribute, nor the DefaultParameterValueAttribute, so this cannot work uniformly for all languages, hence it's not allowed. Edit: In response to your comment: What I don't understand is why the C# compiler cannot generate the necessary code to satisfy the CLR Theoretically, the C# compiler team could have the language generate two separate constructors, instead of one constructor marked with attributes. This would, potentially, explode into many constructors, as named parameters create the capabilities for many, many possible combinations of "constructors" (or method calls for methods), especially when multiple arguments are available.

I personally am glad that they did not, since it would cause confusion due to an overabundance of methods and constructors in the generated types, which would cause the public API to look very different than the code that generated it. Take the following constructor: public Widget( int id = 0, string name = "foo", float width=1.0f, float height=1.0f, float depth=1.0f ) { // ... Were you to automatically generate all of the possible combinations here, the compiler would need to generate 120 constructors for this single constructor, since there are N! Possible ways to call this...

Joshua: The C# compiler generates the code I pasted above. This is how it works - it's not making a parameterless constructor, but rather an optional one. I'll edit to add details... – Reed Copsey Apr 30 '10 at 17:34 Ok, thanks, that explains why it doesn't work.

I still wonder what the implications of generating the parameterless constructor would be. – Joshua Flanagan Apr 30 '10 at 17:40 @Joshua: I edited my answer to explain why this is potentially a bad idea... Make more sense now? Remember, it's not just optional, you can also use named parameters, so N parameters means N!

Possible combinations that would need to be generated... – Reed Copsey Apr 30 '10 at 17:42 ... and now you've described them. Well done. – Joshua Flanagan Apr 30 '10 at 17:42.

While this logically should work, it unfortunately does not. The CLR still sees your constructor as a parameter-based constructor. Remember that, while C# supports optional parameters, this is done at the compiler level, at compile time.

The underlying type still only contains a constructor taking a single parameter . The CLR is a multi-language runtime. Generics are made to work at the CLR level, for all languages, so the constraints must be true in languages without default parameters, as well.

Languages are not required to understand the OptionalAttribute, nor the DefaultParameterValueAttribute, so this cannot work uniformly for all languages, hence it's not allowed. Theoretically, the C# compiler team could have the language generate two separate constructors, instead of one constructor marked with attributes. This would, potentially, explode into many constructors, as named parameters create the capabilities for many, many possible combinations of "constructors" (or method calls for methods), especially when multiple arguments are available.

I personally am glad that they did not, since it would cause confusion due to an overabundance of methods and constructors in the generated types, which would cause the public API to look very different than the code that generated it. Were you to automatically generate all of the possible combinations here, the compiler would need to generate 120 constructors for this single constructor, since there are N! Possible ways to call this...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions