[Mono-list] Boxed types in C#

Stuart Ballard sballard@netreach.com
Tue, 11 Jun 2002 13:58:41 -0400


[I'm not on the list, so please keep me cc'd if possible (though I will 
check the archives periodically). Thanks.]

Although this is a general C# issue, I'm posting to this list because 
I'm interested in insight from people who've actually implemented a VM 
and C# compiler. It seems to be an issue that is entirely un-addressed 
by C# and the CTS, and I was wondering if there was some implementation 
reason why.

Background: I'm looking for the .NET equivalent of java's Integer, 
Boolean etc classes. That is, a reference type that holds a 
representation of a primitive type. Specifically, I need a type that I 
can declare a variable to that might hold either null or an int.

I had assumed that C#'s clever boxing would make this extremely easy (by 
eliminating the need to say new Integer(i) and i.intValue() all over the 
place), but it appears not: 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#. It's either an "int" which is 
primitive, or an "object" which carries no type information.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/html/vcManagedExtensionsSpec_5_2_1.asp 
(sorry if that wraps) says the following:

---
Given argument type V, a __gc object of type "boxed V" is allocated on 
the common language runtime heap.
---

This suggests that perhaps you might be able to write:

int i = 10
object o = (object) i;
boxed int bi = (boxed int) o;
int j = bi;

However,
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfCSharpKeywords_PG.asp
indicates that "boxed" is not a C# keyword.

If this *were* possible, it would have the following advantages over all 
existing solutions:
- Unlike "object", it could be cast to and from "int" without an 
explicit cast, and (if the implementation were smart) without a runtime 
typecheck.
- Unlike "int" it could take the null value.
- Unlike a user defined class, it could be used as an int in all 
situations where an int is required without any extra syntax.

It also seems from the quote above that these "boxed" types are already 
implemented internally, just not accessible from the language.

So my question is, does anyone know of a better way to express what I 
want to do? I'd like to be able, for example, to express a nullable 
integer database field as a single property. With "boxed int" it would 
be easy to do:

boxed int MyField { get { ... }
                     set { ... } }

In Java I could do this by using "Integer". In order to avoid the 
painful explicit boxing conversions, I had to maintain three separate 
properties: getField() would return an int, getWrappedField() would 
return an Integer, and getHasField() returned a boolean indicating 
whether the field was non-null. This was, of course, a complete pain in 
the neck, but at least it worked. To do this in C# (without a "boxed 
int" type) it seems like I'd have to jump through the same hoops *and* 
define the Integer class equivalent myself (eg class Integer { public 
int value }).

I'm wondering about writing to MS and asking for a "boxed" keyword in 
future versions of C#, but I'd like feedback from people who are 
familiar with the intimate details of how boxing is implemented. I'd 
also be interested to know how hard it would be to implement a "boxed" 
keyword in my copy of mcs, just to experiment with the idea and provide 
a proof of concept if I *do* submit the idea to MS.

Thanks in advance for any help, and sorry for the partially off-topic 
question.

Stuart.

-- 
Stuart Ballard, Programmer
NetReach - Internet Solutions
(215) 283-2300, ext. 126
http://www.netreach.com/