[Mono-list] Boxed types in C#
Sergey Chaban
serge@wildwestsoftware.com
Tue, 11 Jun 2002 23:48:37 +0300
> in C# it seems that the System.Int32 type is
> a value type and hence cannot hold null at all. It also seems that
> although you can box it into a variable of type "object", there's no way
> to express the *type* of the object in C#.
Use generic ValueType class to pass boxed values around.
(Or, alternatively, use one of the interfaces implemented by those
wrapper structures, such as IConvertible).
Something like this:
public ValueType this[int i] {
get {return mytable[i];}
}
public void FooBar() {
bool ok = MyVal is Int32; //or MyVal != null
int i = (ok ? (int)MyVal : 0);
}
Or write your own wrapper class similar to Java Integer,
but with implicit conversion operators.
Sergey