interface不是类型,interface是关键字,定义为 interface 是什么类型,是由其“变量”决定的。如果一定要强去理解“接口到底是什么类型”?那么它必然是引用类型(这句话与前面那句矛盾)。
class也不是类型,只不过定义为 class 都是引用类型。
看看实例吧:
public interface ITest
{
string Text { get; set; }
}
public struct TestStruct : ITest
{
public string Text { get; set; }
}
public class TestClass : ITest
{
public string Text { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test1:");
Test1();
Console.WriteLine("Test2:");
Test2();
Console.ReadKey();
}
static void Test1()
{
TestStruct t1 = new TestStruct();
t1.Text = "我爱辣妹";
Func(t1);
Console.WriteLine(t1.Text);
TestClass t2 = new TestClass();
t2.Text = "我爱辣妹";
Func(t2);
Console.WriteLine(t2.Text);
}
static void Test2()
{
ITest t1 = new TestStruct();
t1.Text = "我爱辣妹";
Func(t1);
Console.WriteLine(t1.Text);
ITest t2 = new TestClass();
t2.Text = "我爱辣妹";
Func(t2);
Console.WriteLine(t2.Text);
}
static void Func(ITest test)
{
test.Text = "辣妹爱我";
}
}
转载于:https://www.cnblogs.com/sofire/archive/2010/12/07/1898658.html