Saturday, March 27, 2004 3:21 PM
richard
A small point about boxing and value types
An explicit call to a struct method, even if it overrides an object method, will not implicitly box. However, consider this case:
static void Main()
{
int i=65536;
Console.WriteLine("This is boxed: {0}", i);
Console.WriteLine("This is not boxed: {1}", i.ToString());
}In the first WriteLine the second argument expects an object and therefore needs to implicitly box the int before calling the overridden ToString
method. In the second, by calling ToString yourself, you return a reference type (a stirng) to the method (not a value type - so no boxing),
so the compiler doesn't need to box the int.
Looking at the IL of the above C# snippet supports that conclusion. Personally I think if performance isn't the absolutely hyper-critical factor
in your app (which it isn't in 99.99% of apps), you should use the first form for readability.