I was reading through this answer: https://stackoverflow.com/a/15209464/1073672 Here is the code, copied for completeness and slightly simplified.
using System;
namespace Test
{
interface IFoo
{
int Foobar{get;set;}
}
struct Foo : IFoo
{
public int Foobar{ get; set; }
}
class Bar
{
// These two lines. I can not understand how they will work.
Foo tmp;
public IFoo Biz{ get { return tmp; } set { tmp = (Foo) value; } }
public Bar()
{
Biz = new Foo(){Foobar=0};
}
}
class MainClass
{
// This is not really important.
public static void Main (string[] args)
{
var mybar = new Bar();
}
}
}
How do construction of Bar
and assignment to Biz
work?
My take:
- In Bar constructor create a struct of type
Foo()
and set its Foobar = 0; - Cast to type of
IFoo
by using boxing (?) (sinceBiz
has typeIFoo
) - Unbox the reference type
IFoo
to value type struct(Foo)
and assign to tmp.
So is this description correct? This is really un/boxing even if we don't make use of the object class?