Cast a class to an interface is the same as convert a Class to another Class in C#?Does box or unboxing occurs in this process?
interface Area
{
string TxtArea
{
get;
set;
}
}
Convert to it interface:
public void Test()
{
ExternArea extArea = new ExternArea();
if(extArea is Area)
{
((Area)extArea).TxtArea = "voila";
}
}
Assuming ExternArea is a class rather than a value type (struct or enum), there's no boxing involved. Boxing only ever converts a value type to a reference type instance.
Note that using as is generally preferred though:
Area area = extArea as Area;
if (area != null)
{
area.TxtArea = "voila";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With