I've just run across some code I don't understand. It is effectively
Class c = new BaseClass() as Class;
I don't understand the advantage of doing this, so I created my own console application to see what it does.
namespace Initialize
{
class Program
{
static void Main(string[] args)
{
Demo demo = new Demo();
demo.BaseProp = "";
demo.DemoProp = "";
BaseDemo baseDemo = new BaseDemo();
baseDemo.BaseProp = "";
BaseDemo baseDemo2 = new Demo();
baseDemo2.BaseProp = "";
BaseDemo baseDemo3 = new Demo() as BaseDemo;
baseDemo3.BaseProp = "";
//fails runtime
//Demo demo2 = (Demo)baseDemo;
Demo demo3 = (Demo)baseDemo2;
demo3.BaseProp = "";
demo3.DemoProp = "";
Demo demo4 = (Demo)baseDemo3;
demo4.BaseProp = "";
demo4.DemoProp = "";
}
}
class BaseDemo
{
public string BaseProp { get; set; }
}
class Demo : BaseDemo
{
public string DemoProp { get; set; }
}
}
I can only assume this offers some additional help in relation to polymorphous but I can't work out how or see any difference between:
BaseDemo baseDemo2 = new Demo();
and
BaseDemo baseDemo3 = new Demo() as BaseDemo;