Jan 31, 2024

Structs in C# are fun - Part 6/9: Struct with default argument values in ctors a.k.a, are you not confused yet ?

Leia este post em Português

Lire cet post en français.

  1. Structs in C# are fun
  2. Brief introduction to Value Types vs Reference Types
  3. Field initialization in structs
  4. Constructors and struct behavior
  5. Other scenarios in which struct constructors behavior may surprise you.
  6. Struct with default argument values in constructors, a.k.a, are you not confused yet?(this post)
  7. required feature from C# 11 will not save your a** job.
  8. Struct used as default argument values.
  9. Bonus: Struct evolution in C#.

In the previous post we saw that the C# compiler may not emit a ctor invocation in some scenarios. Unfortunately the experience with constructors in structs, at least in my opinion, may get even more confusing.

To illustrate this point, suppose you have the following code:


Print(new S2()); 
Print(new S2(13));

void Print(S2 s) => System.Console.WriteLine(s.v);

struct S2
{ 
    public int v;
    public S2(int i = 42)  => v = i;
    public S2() => v = 84;
}

What do you expect to happen when you compile/run this code1?

  1. Compiler will emit an error claiming new S2() is an ambiguous call.
  2. It compiles and outputs 42, 13.
  3. It compiles and outputs 84, 13.

The correct answer, which may surprise some developers, is that it compiles successfully and prints 84 and 13 (i.e, the third option).

That happens because in line 1 the C# compiler sees the parameterless constructor as a better match than the one with the default parameter value ruling out options 1 and 21. The bright side is that even not being totally obvious, at least this behavior is consistent across classes/structs,

However, with structs, it may get even more complex/confusing; imagine you have exactly the same code as above, with the only difference being the removal of the parameterless constructor:

Print(new S2()); 
Print(new S2(13));

void Print(S2 s) => System.Console.WriteLine(s.v);

struct S2
{ 
    public int v;
    public S2(int i = 42)  => v = i;
}

now, for sure it will print 42, 13, right ?

Nope! In this scenario, differently from classes, a constructor with default values for all of its parameters is not invoked, even on an explicit new expressions2 which means the code above will print 0 and 133.

In the next post we'll take a quick look into required members C# 11 feature4 as a way to help pin-pointing scenarios in which no constructor is being invoked.

As always, all feedback is welcome.

Have fun!


  1. To be fair the behavior here is the same for classes, so at least it is consistent

  2. The specification of parameterless constructors in structs explicitly states that this behavior is the same as in previous versions of C#

  3. That happens because line #1 is equivalent to Print(default(S2)); for more details see this post.

  4. I am aware that this is not the intended use of this feature but it may be used to flag struct instantiations that does not invoke a constructor.

No comments: