开发者

Are nullable types stored on the stack?

开发者 https://www.devze.com 2023-01-23 02:19 出处:网络
I开发者_Python百科f you have Nullable<int>, does this live on the stack?How many bytes does it take up?It shouldn\'t matter is the correct answer to both of your questions.

I开发者_Python百科f you have Nullable<int>, does this live on the stack? How many bytes does it take up?


It shouldn't matter is the correct answer to both of your questions.

But, to entertain you, an instance of Nullable<T> that isn't an element of a collection, is not a field of a reference type, is not a local in an iterator block or a hoisted local in a lambda or an anonymous method is probably stored on the stack.

But again, it shouldn't matter. This is an implementation detail (meaning it might be one way for the Microsoft implementation of the CLR but it doesn't have to be, and it could be another way for the Mono implementation of the CLR).


Microsoft employee Eric Lippert doesn't like it when you refer to value types living on the stack, but yes, Nullable<> is a value type.

They generally live on the stack, and simply have a boolean member indicating whether the value is null or not. Hence (excluding padding bytes due to alignment) a Nullable<int> takes 5 bytes.


A Nullable<T> is a struct (and therefore a value type), so the same rules of allocation location apply as to any other struct.

Eric Lippert recently posted this: The Truth About Value Types

SO: What is the size of a Nullable<Int32>?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号