3
struct str {
    public str(int n) { }
}
   
    
class tes {
    public tes(int h) { }
}
    

//main method
tes uhi = new();  //error no such constructor
str uhi1 = new();

I can see that for a class, if I define a parameterized constructor that the default one is no longer available but in case of structs, the compiler automatically generates a public parameter-less constructor even if I define a parameterized one.

Can you please tell me why is that so?

Charlieface
  • 52,284
  • 6
  • 19
  • 43
Jason9789
  • 295
  • 1
  • 9
  • I am sorry, why did the answers and comments get deleted? – Jason9789 Aug 07 '22 at 11:07
  • 3
    Because the author of that answer decided to delete it (and that deletes all comments on the answer as well). So I am linking the docs here, so we don't forget for potential new answers that the default value expression and the structure-type array instantiation both ignore the parameterless struct constructors ([docs](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct#struct-initialization-and-default-values)). Sadly, the answer is not as simple as "because the parameterless constructor is needed". Is it useful? Of course! But why is it auto-generated? – Ma3x Aug 07 '22 at 11:38
  • Thank you @Ma3x, You have been helping a lot, let me check This – Jason9789 Aug 07 '22 at 12:10
  • Does this answer your question? [Why can't I define a default constructor for a struct in .NET?](https://stackoverflow.com/questions/333829/why-cant-i-define-a-default-constructor-for-a-struct-in-net) – Charlieface Aug 07 '22 at 14:35
  • 1
    @Charlieface Sadly, it does not. While the accepted answer and some other answers do talk about C# versions prior to 10, they do not cover C# 10+ and they don't answer why would a parameterless constructor always be required (and thus autogenerated when not provided) in C# 10+. In versions prior to C# 10 you could not even define one. What I could _assume_ from all this is that since it was somehow always available prior to C# 10, that is why now has to be auto-generated when not provided? Is that the reason - backward compatibility? – Ma3x Aug 07 '22 at 15:50
  • Sorry didn't realize you were specifically referring to C#10, especially as it only just got released recently. Perhaps you should mention that. Yes they do answer that: there is always an implicit default constructor for structs, as defined in the spec since the beginning. See also https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/structs#1549-constructors – Charlieface Aug 07 '22 at 16:04
  • @Charlieface No, in fact, the question is not specific to C# 10. But prior to C# 10 you could not define a parameterless constructor so the one being there (which you can call with `new SomeStruct()`) was doing the same as using `default`. And the comment by Jon Skeet on that answer: "The documentation is incorrect, although being very precise about this in the docs may make it harder to understand. In most cases you can think about it as if the compiler created a constructor, but it's not actually there in the IL" would mean that it is just a convenience that it is always available in C#? – Ma3x Aug 07 '22 at 16:22
  • @Ma3x I suppose you can look at it that C# does not define that it must be compiled into IL anywhere (because the designers originally intended cross-compilation to Java), therefore in IL it doesn't need to define that constructor under the hood, but under another virtual machine and bytecode it might have to. Either way: the spec say it's implicitly defined, so it is. – Charlieface Aug 07 '22 at 16:26
  • @Charlieface Yeah that is good enough for me, in fact in comments under answers that eventually got deleted here, I already assumed it was due to a design decision and not because the CLR would require it. – Ma3x Aug 07 '22 at 16:29

0 Answers0