Hello there!

What was rationale of having unboxed values in C# as r-values?

Ref types in heap are always considered as l-values. And in my opinion it
would be reasonably to consider value types in heap as l-values as well. I
don't mean that this is so ubiquitous operation (unboxing to l-value), but
anyway in that cases where unboxing is  used it could be valuable, for
instance in collections (in their present implementation) of value types.

P.S. In my project constcli (http://constcli.sscli.net), I just for fun have
allowed this feature. Actually I've only changed several lines in C#
compiler and then was able to compile and run this code:

public struct Rect
{
    public int left;
    public int top;
    public int width;
    public int height;

    public override string ToString()
    {
        return "left: " + left + ", top: " + top + ", width: " + width + ",
height: " + height;
    }

    public void Inflate(int x, int y)
    {
        left -= x;
        top -= y;
        width += x * 2;
        height += y * 2;
    }
}

public class unbox
{
    public static void Main(string[] args)
    {
        Rect r = new Rect();
        object o = r;

        Console.WriteLine(o);

        r.Inflate(1, 1);
        (Rect)o = r;
        Console.WriteLine(o);

        ((Rect)o).left ++;
        r = (Rect)o;
        Console.WriteLine(r);

        ((Rect)o).Inflate(-2, -2);
        Console.WriteLine(o);
    }
}

P.P.S. Looking from the more general perspective I do not see any need in
box and unbox il instructions, cast instructions could handle this work
well.
--
Vladimir Nesterovsky
visit: http://constcli.sscli.net
e-mail: [EMAIL PROTECTED], [EMAIL PROTECTED]
home: http://www.nesterovsky-bros.com

Reply via email to