Nullable Array and Why Do We Need Them
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];
c# arrays nullable
add a comment |
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];
c# arrays nullable
2
It looks like the main confusion here is that it is not a "nullable array of decimals", but "a regular array of nullable decimals".
– Yeldar Kurmangaliyev
Jan 2 at 17:08
Thanks, that explains it
– cd491415
Jan 2 at 17:11
add a comment |
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];
c# arrays nullable
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];
c# arrays nullable
c# arrays nullable
edited Jan 2 at 17:05
Uwe Keim
27.7k32134216
27.7k32134216
asked Jan 2 at 17:02


cd491415cd491415
315111
315111
2
It looks like the main confusion here is that it is not a "nullable array of decimals", but "a regular array of nullable decimals".
– Yeldar Kurmangaliyev
Jan 2 at 17:08
Thanks, that explains it
– cd491415
Jan 2 at 17:11
add a comment |
2
It looks like the main confusion here is that it is not a "nullable array of decimals", but "a regular array of nullable decimals".
– Yeldar Kurmangaliyev
Jan 2 at 17:08
Thanks, that explains it
– cd491415
Jan 2 at 17:11
2
2
It looks like the main confusion here is that it is not a "nullable array of decimals", but "a regular array of nullable decimals".
– Yeldar Kurmangaliyev
Jan 2 at 17:08
It looks like the main confusion here is that it is not a "nullable array of decimals", but "a regular array of nullable decimals".
– Yeldar Kurmangaliyev
Jan 2 at 17:08
Thanks, that explains it
– cd491415
Jan 2 at 17:11
Thanks, that explains it
– cd491415
Jan 2 at 17:11
add a comment |
4 Answers
4
active
oldest
votes
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
beacause with C# 8.0 reference types cannot be null by default (if this feature is enabled in the project).
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 MUST have nullable reference type (stated explicitly) if you want to assing null to them. You can declare like this:
private decimal?? _amounts;
Now it means that both elements in array can be null and entire array (_amounts variable) can 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.
Awesome explanation. Much appreciated BART!
– cd491415
Jan 2 at 17:57
add a comment |
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.
add a comment |
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
Please note my question initiates array as new decimal?[count], not new decimal[count]
– cd491415
Jan 2 at 17:13
1
@cd491415 Yes, I noted that. I put both for the sake of comparison to show in which cases they have different behavior, both your specific case and a made up one.
– Alejandro
Jan 2 at 18:42
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010316%2fnullable-array-and-why-do-we-need-them%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
beacause with C# 8.0 reference types cannot be null by default (if this feature is enabled in the project).
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 MUST have nullable reference type (stated explicitly) if you want to assing null to them. You can declare like this:
private decimal?? _amounts;
Now it means that both elements in array can be null and entire array (_amounts variable) can 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.
Awesome explanation. Much appreciated BART!
– cd491415
Jan 2 at 17:57
add a comment |
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
beacause with C# 8.0 reference types cannot be null by default (if this feature is enabled in the project).
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 MUST have nullable reference type (stated explicitly) if you want to assing null to them. You can declare like this:
private decimal?? _amounts;
Now it means that both elements in array can be null and entire array (_amounts variable) can 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.
Awesome explanation. Much appreciated BART!
– cd491415
Jan 2 at 17:57
add a comment |
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
beacause with C# 8.0 reference types cannot be null by default (if this feature is enabled in the project).
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 MUST have nullable reference type (stated explicitly) if you want to assing null to them. You can declare like this:
private decimal?? _amounts;
Now it means that both elements in array can be null and entire array (_amounts variable) can 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.
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
beacause with C# 8.0 reference types cannot be null by default (if this feature is enabled in the project).
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 MUST have nullable reference type (stated explicitly) if you want to assing null to them. You can declare like this:
private decimal?? _amounts;
Now it means that both elements in array can be null and entire array (_amounts variable) can 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.
edited Jan 2 at 17:53
answered Jan 2 at 17:43
BARTBART
34119
34119
Awesome explanation. Much appreciated BART!
– cd491415
Jan 2 at 17:57
add a comment |
Awesome explanation. Much appreciated BART!
– cd491415
Jan 2 at 17:57
Awesome explanation. Much appreciated BART!
– cd491415
Jan 2 at 17:57
Awesome explanation. Much appreciated BART!
– cd491415
Jan 2 at 17:57
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 2 at 17:16
answered Jan 2 at 17:04


AomineAomine
42.6k74677
42.6k74677
add a comment |
add a comment |
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
Please note my question initiates array as new decimal?[count], not new decimal[count]
– cd491415
Jan 2 at 17:13
1
@cd491415 Yes, I noted that. I put both for the sake of comparison to show in which cases they have different behavior, both your specific case and a made up one.
– Alejandro
Jan 2 at 18:42
add a comment |
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
Please note my question initiates array as new decimal?[count], not new decimal[count]
– cd491415
Jan 2 at 17:13
1
@cd491415 Yes, I noted that. I put both for the sake of comparison to show in which cases they have different behavior, both your specific case and a made up one.
– Alejandro
Jan 2 at 18:42
add a comment |
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
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
edited Jan 2 at 17:16


Yeldar Kurmangaliyev
25.2k94068
25.2k94068
answered Jan 2 at 17:11
AlejandroAlejandro
4,39722239
4,39722239
Please note my question initiates array as new decimal?[count], not new decimal[count]
– cd491415
Jan 2 at 17:13
1
@cd491415 Yes, I noted that. I put both for the sake of comparison to show in which cases they have different behavior, both your specific case and a made up one.
– Alejandro
Jan 2 at 18:42
add a comment |
Please note my question initiates array as new decimal?[count], not new decimal[count]
– cd491415
Jan 2 at 17:13
1
@cd491415 Yes, I noted that. I put both for the sake of comparison to show in which cases they have different behavior, both your specific case and a made up one.
– Alejandro
Jan 2 at 18:42
Please note my question initiates array as new decimal?[count], not new decimal[count]
– cd491415
Jan 2 at 17:13
Please note my question initiates array as new decimal?[count], not new decimal[count]
– cd491415
Jan 2 at 17:13
1
1
@cd491415 Yes, I noted that. I put both for the sake of comparison to show in which cases they have different behavior, both your specific case and a made up one.
– Alejandro
Jan 2 at 18:42
@cd491415 Yes, I noted that. I put both for the sake of comparison to show in which cases they have different behavior, both your specific case and a made up one.
– Alejandro
Jan 2 at 18:42
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 2 at 17:27
answered Jan 2 at 17:14
BlueMonkMNBlueMonkMN
18.8k656120
18.8k656120
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010316%2fnullable-array-and-why-do-we-need-them%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
It looks like the main confusion here is that it is not a "nullable array of decimals", but "a regular array of nullable decimals".
– Yeldar Kurmangaliyev
Jan 2 at 17:08
Thanks, that explains it
– cd491415
Jan 2 at 17:11