1

I work with C# 8 with non nullable reference types and I define a class MyClass:

class MyClass
{
    public int Prop1 { get; set; } = 0;
    public bool Prop2 { get; set; } = false;
}

I want to create an array

var myArray = new MyClass[10];

Since I am working with non nullable reference types and I am defining the default values for each property in my class, I'd expect that myArray would be initialised with a this "default" object, with Prop1 = 0 and Prop2 = false. However, even with non nullable types, the default value is still null.

Is there a way to define (override) the default value so that when I use the tag default or when I create an array it uses this more consistent element?

xavier
  • 1,860
  • 4
  • 18
  • 46
  • 5
    No. If you declare a `new SomeType[10]`, then each element of that array has its default value. If you need elements to have a different value, you need to set that. This is one of the places where NRT's hit the brick wall of the reality of past design decisions, and there's no good solution – canton7 Apr 28 '21 at 10:00
  • 1
    Array elements (of reference arrays) are always implicitly nullable. Note that you can't define a "non-nullable class". You can define that an _instance variable_ or a _field_ is non-nullable, but you cannot declare a class as non-nullable. – PMF Apr 28 '21 at 10:02
  • @canton7: yes, that's the point: I want to override the default value so that the default value is not null but a defined one. – xavier Apr 28 '21 at 10:06
  • @PMF: you are right about the "non nullable class" thing, thank you for the remark. I'll change the text of the question. – xavier Apr 28 '21 at 10:07
  • 1
    @xavier and the point of my comment was that arrays are always default-initialised. If you want to initialise the members of your array, you need to do so manually (e.g. with a loop, etc) – canton7 Apr 28 '21 at 10:12
  • When you allocate an array of reference type, `new SomeType[10]`, all the references are initialized to `null` (no objects are created and initialized automatically) – gigiabbrescia Apr 28 '21 at 10:33
  • @canton7: exactly, the fact that they are "default-initialised" doesn't answer my question. If I could override the default value for a class, a "default initialisation" would perfectly work for my case. If you tell me "no, the default value can't be overwritten", as it seems so, then I need to initialise them manually. All in all, I see I'll have to loop. Thank you! – xavier Apr 28 '21 at 10:33
  • 1
    @xavier I think the problem is the term "default-initialised". That means that all reference types are null, all primitive value types are 0, and all other value types have all of their fields default-initialised. You cannot control the default-initialisation process. Since new arrays are default-initialised, by definition there is no way to override this process – canton7 Apr 28 '21 at 10:35
  • Ok, that was my problem. If I could declare a different default value for reference types when I was using non nullable reference types. Now I see clearly I can't :-( Thank you for your answer and patience. – xavier Apr 28 '21 at 10:50
  • 1
    This is so crazy. They should have added another array initializer syntax for arrays of non-nullable reference types. One that takes a `Func` to use as a factory or something. – Nick Strupat Jun 23 '22 at 22:41

0 Answers0