开发者

what's the easiest way to detect the given object is value or reference type in .net?

开发者 https://www.devze.com 2023-01-26 09:19 出处:网络
What\'s the way to check if an object is a value type or reference t开发者_StackOverflow社区ype?o.GetType().IsValueType

What's the way to check if an object is a value type or reference t开发者_StackOverflow社区ype?


o.GetType().IsValueType


Use IsValueType like this:

if (yourObject.GetType().IsValueType) {
    // it's a value type
} else {
    // it's a reference type
}


They already answered, but don't forget that, if an object is an instance of a class, then it is a reference type, and if an object is an "instance" of a struct, then it is a value type. But there is an exception. String is a reference type but behaves like value type, because of a immutability.

0

精彩评论

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