Even though value types don't have object pointers (are not reference
types), methods such as Equals(), GetHashCode() and ToString() (virtual
methods) can be called as non virtual methods (no boxing required)
This is because Syste.ValueType overrides this methods and since you cannot
inherit from a value type the CLR can safely call this methods as non
virtual (no boxing)
Not all ValueType methods behave the same, methods such as GetType() or
MemberwiseClone () will require your value type to be boxed because this
methods are defined directly in the System.Object and (not automatically
overridden).
Other reasons why ValueTypes can be boxed is if you implement an interface
on the ValueType and then cast the ValueType to the interface, this will
cause the ValueType to be boxed.
Concerning the memory leak issue: When a ValueType gets boxed, it will
eventually become unreachable and then the garbage collector will clean it
up so there is no such thing as a memory leak.
"Joe" <js************ *@logicor.com> wrote in message
news:op******** *******@jsheble-laptop.logicorp hx2.com...
Consider the following code loop:
for(int x = 0; x < 100; x++)
{
string sLoop = "Loop # " + (x+1).ToString( );
Console.WriteLi ne(x);
}
I was told that the (x+1).ToString( ) was a boxing statement, and would
therefore cause a memory leak because there's no explicit unboxing
statements. Now granted this is a simplistic example, and wouldn't
normally be found in a real-workld application. But it's not uncommon to
see statements such as (x+1).ToString( ), so how would you explicitly
un-box this?
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/