what's the easiest way to detect the given object is value or reference type in .net?
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.
精彩评论