Interfaces can be both value and reference type
Yesterday someone posted a message on our user group asking if interfaces were reference or value type. Initially i thought reference type, but then i thought that since value types can implement interfaces too, it needs to be a value type as well.
I wrote a small code snippet and used propertyGrid control to get visual results :)
public interface ITest
{
}
public class TestClass : ITest
{
}
public struct TestStruct : ITest
{
}
Here TestClass is a Ref type and TestStruct is a Value type.
i wrote this code in the Form_load event
private void Form1_Load(object sender, System.EventArgs e)
{
TestClass tc = new TestClass();ITest itc = (ITest)tc;propertyGrid1.SelectedObject = itc.GetType();TestStruct ts = new TestStruct();ITest its = (ITest)ts;propertyGrid2.SelectedObject = its.GetType();
}
When i ran the propertyGrid showed that
itc.ValueType is 'false' and its.ValueType is 'true'.
itc.ValueType is 'false' and its.ValueType is 'true'.
So interface has the same semantics as the type that implements the interface.