Type int? vs type int
I've this comparison which equals false
as expected
bool eq = typeof(int?).Equals(typeof(int));
now I have this code
List<object> items = new List<object>() { (int?)123 };
int result = items.OfType<int>().FirstOrDefault();
but this returns 123
- anyway that value is of type int?
How can this be?
c# casting
|
show 2 more comments
I've this comparison which equals false
as expected
bool eq = typeof(int?).Equals(typeof(int));
now I have this code
List<object> items = new List<object>() { (int?)123 };
int result = items.OfType<int>().FirstOrDefault();
but this returns 123
- anyway that value is of type int?
How can this be?
c# casting
int?
boxed asint
, and basically every Nullable type, Edit : Marc Gravell have the full answer
– styx
2 days ago
Related post about nullable type: stackoverflow.com/questions/4028830/…. This is called as "type lifting".
– Tetsuya Yamamoto
2 days ago
Before reading this topic I wouldn't even guess that evenList<int?>
already holds justint
types. Proof
– Sinatr
2 days ago
7
@Sinatr no, that is incorrect;List<int?>
holdsint?
. The important distinction in this example is the use ofList<object>
. What you're seeing in that "proof" is something very different;GetType()
on anyT?
either returns theT
, or throws a NRE. It never returnsT?
- better example: dotnetfiddle.net/3Gy3Fa - and as for why: becauseGetType()
is non-virtual, it cannot be overridden, and thus callingGetType()
is a boxing operation (even if used via "constrained call"). And when you box aT?
, you either get aT
as anobject
, or anull
.
– Marc Gravell♦
2 days ago
@TetsuyaYamamoto That is not a relevant link. How is the boxing/unboxing behavior of Nullable<T> possible? is.
– GSerg
2 days ago
|
show 2 more comments
I've this comparison which equals false
as expected
bool eq = typeof(int?).Equals(typeof(int));
now I have this code
List<object> items = new List<object>() { (int?)123 };
int result = items.OfType<int>().FirstOrDefault();
but this returns 123
- anyway that value is of type int?
How can this be?
c# casting
I've this comparison which equals false
as expected
bool eq = typeof(int?).Equals(typeof(int));
now I have this code
List<object> items = new List<object>() { (int?)123 };
int result = items.OfType<int>().FirstOrDefault();
but this returns 123
- anyway that value is of type int?
How can this be?
c# casting
c# casting
asked 2 days ago
Dr. SnailDr. Snail
718728
718728
int?
boxed asint
, and basically every Nullable type, Edit : Marc Gravell have the full answer
– styx
2 days ago
Related post about nullable type: stackoverflow.com/questions/4028830/…. This is called as "type lifting".
– Tetsuya Yamamoto
2 days ago
Before reading this topic I wouldn't even guess that evenList<int?>
already holds justint
types. Proof
– Sinatr
2 days ago
7
@Sinatr no, that is incorrect;List<int?>
holdsint?
. The important distinction in this example is the use ofList<object>
. What you're seeing in that "proof" is something very different;GetType()
on anyT?
either returns theT
, or throws a NRE. It never returnsT?
- better example: dotnetfiddle.net/3Gy3Fa - and as for why: becauseGetType()
is non-virtual, it cannot be overridden, and thus callingGetType()
is a boxing operation (even if used via "constrained call"). And when you box aT?
, you either get aT
as anobject
, or anull
.
– Marc Gravell♦
2 days ago
@TetsuyaYamamoto That is not a relevant link. How is the boxing/unboxing behavior of Nullable<T> possible? is.
– GSerg
2 days ago
|
show 2 more comments
int?
boxed asint
, and basically every Nullable type, Edit : Marc Gravell have the full answer
– styx
2 days ago
Related post about nullable type: stackoverflow.com/questions/4028830/…. This is called as "type lifting".
– Tetsuya Yamamoto
2 days ago
Before reading this topic I wouldn't even guess that evenList<int?>
already holds justint
types. Proof
– Sinatr
2 days ago
7
@Sinatr no, that is incorrect;List<int?>
holdsint?
. The important distinction in this example is the use ofList<object>
. What you're seeing in that "proof" is something very different;GetType()
on anyT?
either returns theT
, or throws a NRE. It never returnsT?
- better example: dotnetfiddle.net/3Gy3Fa - and as for why: becauseGetType()
is non-virtual, it cannot be overridden, and thus callingGetType()
is a boxing operation (even if used via "constrained call"). And when you box aT?
, you either get aT
as anobject
, or anull
.
– Marc Gravell♦
2 days ago
@TetsuyaYamamoto That is not a relevant link. How is the boxing/unboxing behavior of Nullable<T> possible? is.
– GSerg
2 days ago
int?
boxed as int
, and basically every Nullable type, Edit : Marc Gravell have the full answer– styx
2 days ago
int?
boxed as int
, and basically every Nullable type, Edit : Marc Gravell have the full answer– styx
2 days ago
Related post about nullable type: stackoverflow.com/questions/4028830/…. This is called as "type lifting".
– Tetsuya Yamamoto
2 days ago
Related post about nullable type: stackoverflow.com/questions/4028830/…. This is called as "type lifting".
– Tetsuya Yamamoto
2 days ago
Before reading this topic I wouldn't even guess that even
List<int?>
already holds just int
types. Proof– Sinatr
2 days ago
Before reading this topic I wouldn't even guess that even
List<int?>
already holds just int
types. Proof– Sinatr
2 days ago
7
7
@Sinatr no, that is incorrect;
List<int?>
holds int?
. The important distinction in this example is the use of List<object>
. What you're seeing in that "proof" is something very different; GetType()
on any T?
either returns the T
, or throws a NRE. It never returns T?
- better example: dotnetfiddle.net/3Gy3Fa - and as for why: because GetType()
is non-virtual, it cannot be overridden, and thus calling GetType()
is a boxing operation (even if used via "constrained call"). And when you box a T?
, you either get a T
as an object
, or a null
.– Marc Gravell♦
2 days ago
@Sinatr no, that is incorrect;
List<int?>
holds int?
. The important distinction in this example is the use of List<object>
. What you're seeing in that "proof" is something very different; GetType()
on any T?
either returns the T
, or throws a NRE. It never returns T?
- better example: dotnetfiddle.net/3Gy3Fa - and as for why: because GetType()
is non-virtual, it cannot be overridden, and thus calling GetType()
is a boxing operation (even if used via "constrained call"). And when you box a T?
, you either get a T
as an object
, or a null
.– Marc Gravell♦
2 days ago
@TetsuyaYamamoto That is not a relevant link. How is the boxing/unboxing behavior of Nullable<T> possible? is.
– GSerg
2 days ago
@TetsuyaYamamoto That is not a relevant link. How is the boxing/unboxing behavior of Nullable<T> possible? is.
– GSerg
2 days ago
|
show 2 more comments
2 Answers
2
active
oldest
votes
Nullable types have special "boxing" rules; "boxing" is when a value-type is treated as object
, as per your code. Unlike regular value-types, a nullable value-type is boxed either as null
(regular null
, no type), or as the non-nullable type (the T
in T?
). So: an int?
is boxed as an int
, not an int?
. Then when you use OfType<int>()
on it, you get all the values that are int
, which is: the single value you passed in, since it is of type int
.
phew ok thank you for that explanaion. Is that C# basic knowledge?
– Dr. Snail
2 days ago
12
@Dr.Snail "basic" is relative / subjective, and I'd wager that a good percentage of developers never have a need to know that nuance; it is useful context if you're dealing with boxing, though... and technically it isn't really C# knowledge, but rather: .NET knowledge (it would apply to all languages)
– Marc Gravell♦
2 days ago
4
@Sinatr you can't - the list never containsint?
- it only containsint
because of the boxing rules on nullable types
– Marc Gravell♦
2 days ago
1
@KyleJohnson you added a nullable int to the list. Naively, if nullable ints and ints are different things, you'd expect asking for all the ints in the list to return nothing. There are languages that do it that way, but C# has chosen to do it differently.
– mbrig
2 days ago
1
(and yes, my first sentence there is technically wrong. The nullable int never got added to the list. But if you don't know what's happening here, that's what it looks like is happening)
– mbrig
2 days ago
|
show 2 more comments
A nullable value type is boxed by the following rules:
- If
HasValue
returnsfalse
, the null reference is produced. - If
HasValue
returnstrue
, a value of the underlying value typeT
is
boxed, not the instance of nullable.
In your example second rule has been followed as you have value, e.g.
var i = (object)(int?)123;
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%2f55372675%2ftype-int-vs-type-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Nullable types have special "boxing" rules; "boxing" is when a value-type is treated as object
, as per your code. Unlike regular value-types, a nullable value-type is boxed either as null
(regular null
, no type), or as the non-nullable type (the T
in T?
). So: an int?
is boxed as an int
, not an int?
. Then when you use OfType<int>()
on it, you get all the values that are int
, which is: the single value you passed in, since it is of type int
.
phew ok thank you for that explanaion. Is that C# basic knowledge?
– Dr. Snail
2 days ago
12
@Dr.Snail "basic" is relative / subjective, and I'd wager that a good percentage of developers never have a need to know that nuance; it is useful context if you're dealing with boxing, though... and technically it isn't really C# knowledge, but rather: .NET knowledge (it would apply to all languages)
– Marc Gravell♦
2 days ago
4
@Sinatr you can't - the list never containsint?
- it only containsint
because of the boxing rules on nullable types
– Marc Gravell♦
2 days ago
1
@KyleJohnson you added a nullable int to the list. Naively, if nullable ints and ints are different things, you'd expect asking for all the ints in the list to return nothing. There are languages that do it that way, but C# has chosen to do it differently.
– mbrig
2 days ago
1
(and yes, my first sentence there is technically wrong. The nullable int never got added to the list. But if you don't know what's happening here, that's what it looks like is happening)
– mbrig
2 days ago
|
show 2 more comments
Nullable types have special "boxing" rules; "boxing" is when a value-type is treated as object
, as per your code. Unlike regular value-types, a nullable value-type is boxed either as null
(regular null
, no type), or as the non-nullable type (the T
in T?
). So: an int?
is boxed as an int
, not an int?
. Then when you use OfType<int>()
on it, you get all the values that are int
, which is: the single value you passed in, since it is of type int
.
phew ok thank you for that explanaion. Is that C# basic knowledge?
– Dr. Snail
2 days ago
12
@Dr.Snail "basic" is relative / subjective, and I'd wager that a good percentage of developers never have a need to know that nuance; it is useful context if you're dealing with boxing, though... and technically it isn't really C# knowledge, but rather: .NET knowledge (it would apply to all languages)
– Marc Gravell♦
2 days ago
4
@Sinatr you can't - the list never containsint?
- it only containsint
because of the boxing rules on nullable types
– Marc Gravell♦
2 days ago
1
@KyleJohnson you added a nullable int to the list. Naively, if nullable ints and ints are different things, you'd expect asking for all the ints in the list to return nothing. There are languages that do it that way, but C# has chosen to do it differently.
– mbrig
2 days ago
1
(and yes, my first sentence there is technically wrong. The nullable int never got added to the list. But if you don't know what's happening here, that's what it looks like is happening)
– mbrig
2 days ago
|
show 2 more comments
Nullable types have special "boxing" rules; "boxing" is when a value-type is treated as object
, as per your code. Unlike regular value-types, a nullable value-type is boxed either as null
(regular null
, no type), or as the non-nullable type (the T
in T?
). So: an int?
is boxed as an int
, not an int?
. Then when you use OfType<int>()
on it, you get all the values that are int
, which is: the single value you passed in, since it is of type int
.
Nullable types have special "boxing" rules; "boxing" is when a value-type is treated as object
, as per your code. Unlike regular value-types, a nullable value-type is boxed either as null
(regular null
, no type), or as the non-nullable type (the T
in T?
). So: an int?
is boxed as an int
, not an int?
. Then when you use OfType<int>()
on it, you get all the values that are int
, which is: the single value you passed in, since it is of type int
.
answered 2 days ago
Marc Gravell♦Marc Gravell
792k19821592561
792k19821592561
phew ok thank you for that explanaion. Is that C# basic knowledge?
– Dr. Snail
2 days ago
12
@Dr.Snail "basic" is relative / subjective, and I'd wager that a good percentage of developers never have a need to know that nuance; it is useful context if you're dealing with boxing, though... and technically it isn't really C# knowledge, but rather: .NET knowledge (it would apply to all languages)
– Marc Gravell♦
2 days ago
4
@Sinatr you can't - the list never containsint?
- it only containsint
because of the boxing rules on nullable types
– Marc Gravell♦
2 days ago
1
@KyleJohnson you added a nullable int to the list. Naively, if nullable ints and ints are different things, you'd expect asking for all the ints in the list to return nothing. There are languages that do it that way, but C# has chosen to do it differently.
– mbrig
2 days ago
1
(and yes, my first sentence there is technically wrong. The nullable int never got added to the list. But if you don't know what's happening here, that's what it looks like is happening)
– mbrig
2 days ago
|
show 2 more comments
phew ok thank you for that explanaion. Is that C# basic knowledge?
– Dr. Snail
2 days ago
12
@Dr.Snail "basic" is relative / subjective, and I'd wager that a good percentage of developers never have a need to know that nuance; it is useful context if you're dealing with boxing, though... and technically it isn't really C# knowledge, but rather: .NET knowledge (it would apply to all languages)
– Marc Gravell♦
2 days ago
4
@Sinatr you can't - the list never containsint?
- it only containsint
because of the boxing rules on nullable types
– Marc Gravell♦
2 days ago
1
@KyleJohnson you added a nullable int to the list. Naively, if nullable ints and ints are different things, you'd expect asking for all the ints in the list to return nothing. There are languages that do it that way, but C# has chosen to do it differently.
– mbrig
2 days ago
1
(and yes, my first sentence there is technically wrong. The nullable int never got added to the list. But if you don't know what's happening here, that's what it looks like is happening)
– mbrig
2 days ago
phew ok thank you for that explanaion. Is that C# basic knowledge?
– Dr. Snail
2 days ago
phew ok thank you for that explanaion. Is that C# basic knowledge?
– Dr. Snail
2 days ago
12
12
@Dr.Snail "basic" is relative / subjective, and I'd wager that a good percentage of developers never have a need to know that nuance; it is useful context if you're dealing with boxing, though... and technically it isn't really C# knowledge, but rather: .NET knowledge (it would apply to all languages)
– Marc Gravell♦
2 days ago
@Dr.Snail "basic" is relative / subjective, and I'd wager that a good percentage of developers never have a need to know that nuance; it is useful context if you're dealing with boxing, though... and technically it isn't really C# knowledge, but rather: .NET knowledge (it would apply to all languages)
– Marc Gravell♦
2 days ago
4
4
@Sinatr you can't - the list never contains
int?
- it only contains int
because of the boxing rules on nullable types– Marc Gravell♦
2 days ago
@Sinatr you can't - the list never contains
int?
- it only contains int
because of the boxing rules on nullable types– Marc Gravell♦
2 days ago
1
1
@KyleJohnson you added a nullable int to the list. Naively, if nullable ints and ints are different things, you'd expect asking for all the ints in the list to return nothing. There are languages that do it that way, but C# has chosen to do it differently.
– mbrig
2 days ago
@KyleJohnson you added a nullable int to the list. Naively, if nullable ints and ints are different things, you'd expect asking for all the ints in the list to return nothing. There are languages that do it that way, but C# has chosen to do it differently.
– mbrig
2 days ago
1
1
(and yes, my first sentence there is technically wrong. The nullable int never got added to the list. But if you don't know what's happening here, that's what it looks like is happening)
– mbrig
2 days ago
(and yes, my first sentence there is technically wrong. The nullable int never got added to the list. But if you don't know what's happening here, that's what it looks like is happening)
– mbrig
2 days ago
|
show 2 more comments
A nullable value type is boxed by the following rules:
- If
HasValue
returnsfalse
, the null reference is produced. - If
HasValue
returnstrue
, a value of the underlying value typeT
is
boxed, not the instance of nullable.
In your example second rule has been followed as you have value, e.g.
var i = (object)(int?)123;
add a comment |
A nullable value type is boxed by the following rules:
- If
HasValue
returnsfalse
, the null reference is produced. - If
HasValue
returnstrue
, a value of the underlying value typeT
is
boxed, not the instance of nullable.
In your example second rule has been followed as you have value, e.g.
var i = (object)(int?)123;
add a comment |
A nullable value type is boxed by the following rules:
- If
HasValue
returnsfalse
, the null reference is produced. - If
HasValue
returnstrue
, a value of the underlying value typeT
is
boxed, not the instance of nullable.
In your example second rule has been followed as you have value, e.g.
var i = (object)(int?)123;
A nullable value type is boxed by the following rules:
- If
HasValue
returnsfalse
, the null reference is produced. - If
HasValue
returnstrue
, a value of the underlying value typeT
is
boxed, not the instance of nullable.
In your example second rule has been followed as you have value, e.g.
var i = (object)(int?)123;
edited 2 days ago
answered 2 days ago
JohnnyJohnny
3,5551021
3,5551021
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%2f55372675%2ftype-int-vs-type-int%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
int?
boxed asint
, and basically every Nullable type, Edit : Marc Gravell have the full answer– styx
2 days ago
Related post about nullable type: stackoverflow.com/questions/4028830/…. This is called as "type lifting".
– Tetsuya Yamamoto
2 days ago
Before reading this topic I wouldn't even guess that even
List<int?>
already holds justint
types. Proof– Sinatr
2 days ago
7
@Sinatr no, that is incorrect;
List<int?>
holdsint?
. The important distinction in this example is the use ofList<object>
. What you're seeing in that "proof" is something very different;GetType()
on anyT?
either returns theT
, or throws a NRE. It never returnsT?
- better example: dotnetfiddle.net/3Gy3Fa - and as for why: becauseGetType()
is non-virtual, it cannot be overridden, and thus callingGetType()
is a boxing operation (even if used via "constrained call"). And when you box aT?
, you either get aT
as anobject
, or anull
.– Marc Gravell♦
2 days ago
@TetsuyaYamamoto That is not a relevant link. How is the boxing/unboxing behavior of Nullable<T> possible? is.
– GSerg
2 days ago