B
Brette.Net
Hello All,
I was doing a little messing around with some type conversion they
other day. Code is below.
struct ValueStruct
{
int a;
public ValueStruct(int inA)
{
a = inA;
}
}
public class Runner
{
public static void Main()
{
ValueStruct mValueStruct = new ValueStruct(10);
Console.WriteLine(mValueStruct.GetType().IsValueType);
Console.WriteLine(mValueStruct.ToString());
Console.WriteLine(mValueStruct.GetType().IsValueType);
}
}
My understanding is that if you call ToString() or any of the Object
virtual methods on a value type, the actual value type has to be
boxed. This makes sense as the base implementations of these methods
are on the System.Object type.
However, I noticed that calling the IsValueType after the ToString()
call still returns true. I understand why this happening because after
all it is still a struct thus it impl of IsValueType will be true.
So is there a better way to determine if an object is a value type or
a reference type? Obviously in this case the IsValueType is not really
telling me the correct information.
Thanks,
B
I was doing a little messing around with some type conversion they
other day. Code is below.
struct ValueStruct
{
int a;
public ValueStruct(int inA)
{
a = inA;
}
}
public class Runner
{
public static void Main()
{
ValueStruct mValueStruct = new ValueStruct(10);
Console.WriteLine(mValueStruct.GetType().IsValueType);
Console.WriteLine(mValueStruct.ToString());
Console.WriteLine(mValueStruct.GetType().IsValueType);
}
}
My understanding is that if you call ToString() or any of the Object
virtual methods on a value type, the actual value type has to be
boxed. This makes sense as the base implementations of these methods
are on the System.Object type.
However, I noticed that calling the IsValueType after the ToString()
call still returns true. I understand why this happening because after
all it is still a struct thus it impl of IsValueType will be true.
So is there a better way to determine if an object is a value type or
a reference type? Obviously in this case the IsValueType is not really
telling me the correct information.
Thanks,
B