Nullable Array and Why Do We Need Them

南楼画角 提交于 2021-02-11 14:05:45

问题


I see code like this and I understand it as making an array nullable but I dont understand why do we need it since arrays are ref types, so they are already nullable.

So, my question is why do we need this?

private readonly decimal?[] _amounts = new decimal?[_count];

回答1:


It's worth to mention that from C# 8.0 you can have a nullable reference type: https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/nullable-reference-types

but as others mentioned this:

private readonly decimal?[] _amounts = new decimal?[_count];

means the value-type elements in array can be null. decimal is value type and normally you can't assing null to it but if you have decimal? then you can.

With C# 8.0 and nullable reference types feature enabled you should declare reference types as nullable reference types if you want to assing null to them, otherwise you'll get compiler warning by default. You can declare one like this:

private decimal?[]? _amounts;

Now it means that both elements in array can be null and entire array (_amounts variable) may be null.

So in general the question mark after element value type ? and before [] -> SomeValueType?[] means that elements in array can be null. From C# 8.0 (with feature enabled in project) question mark ? after array type SomeArrayType[] -> SomeArrayType[]? means that you can assign null to variable that holds reference to the array.




回答2:


Declaring it as decimal?[] means that the elements the array contains can be null or non-null.

Without making it nullable, the elements the array can store cannot be null.

In other words, decimal?[] reads as "an array of nullable decimals". The ? is referring to the elements the array can contain and not the array itself as all arrays are reference types.




回答3:


The nullable specifier here refers not to the array itself, but to the contents of it. As you correctly say, the array in itself is a reference type, and there fore, always nulable (at least until C#8).

A few examples of the difference:

decimal[] nonNullable = new decimal[2];
nonNullable[0] = 1;             //OK, a non null item
nonNullable[1] = null;          //Compile error: array doesn't accepts nulls
nonNullable = null;             //OK, set the array reference to null

decimal?[] nullable = new decimal?[2];
nullable[0] = 1;                //OK, a non null item
nullable[1] = null;             //OK, a null item (actually, a Nullable<decimal> instance)
nullable = null;                //OK, set the array reference to null



回答4:


There's a difference between the array object itself and the elements of the array. When you use decimal?[] you are declaring an array whose elements are nullable decimal values. If you were to use decimal[]? (I'm not sure that's valid syntax, but just for explanation purposes, assume it is) you would be declaring a variable that may refer to an array or may not refer to anything. The later is the useless case you describe because, like you say (at least in older versions of C#) all array variables are reference variables that can already be set to null.

To clarify the difference, consider the code posted by Alejandro, and also this code:

decimal[]? reallyNullable = null;

Array.Sort(reallyNullable); // ArgumentNullException -- there is no array here
Array.Sort(nullable); // OK, assuming null can be compared to decimal.

The reason the last line is OK is because there is actually an array object referenced here. The only thing that's null are some of the values in it.



来源:https://stackoverflow.com/questions/54010316/nullable-array-and-why-do-we-need-them

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!