|
I've recently stumbled upon the following compiler behavior and I wonder if this was intentionally done this way (for whatever reason) or it's a bug (and in such case where to report it so that it won't be ping-ponged between repos): struct S1
{
public S1() { } // explicit parameterless ctor: CS8618
public string Prop { get; }
}
struct S2
{
public string Prop { get; } // no explicit parameterless ctor: no CS8618; expected: should be triggered
}
struct S3
{
public S3(string x) => Prop = x;
public string Prop { get; } // no explicit parameterless ctor: no CS8618; expected: should be triggered
}
class Test
{
void CS8618DoesNotPropagate()
{
// no CS8618 below
// expected: the access to default(S*)-initialized arrays should complain about not assigned notnull-marked Prop
var hehe1 = (new S1[1])[0];
var hehe2 = (new S2[1])[0];
var hehe3 = (new S3[1])[0];
}
} |
Replies: 2 comments 7 replies
This is incorrect. Parameterless constructor is not involved in array creation of C#. Console.WriteLine((new S[1])[0].a);
struct S
{
public int a;
public S()
{
a = 1;
}
}prints 0. I'm not recommending the usage of term "implicit parameterless ctor". It's actually the alias of |
|
Closing the discussion as it is by design:
|
Closing the discussion as it is by design: