C#中的结构与类
2009-03-04 08:17:49 来源:WEB开发网15、结构和类对于System.Object.Equals()方法的体现是不相同的。例如定义这样的结构和类:
struct StructurePerson
{
public string FirstName;
public string LastName;
}
class ClassPerson
{
public string FirstName;
public string LastName;
}
如果运行如下的代码:
class Program
{
static void Main(string[] args)
{
StructurePerson strX = new StructurePerson();
strX.LastName = "Bejaoui";
strX.FirstName = "Bechir";
StructurePerson strY = new StructurePerson();
strY.LastName = "Bejaoui";
strY.FirstName = "Bechir";
if (strX.Equals(strY))
{
Console.WriteLine("strX = strY");
}
else
{
Console.WriteLine("strX != strY");
}//This code displays strX = strY
ClassPerson clsX = new ClassPerson();
clsX.LastName = "Bejaoui";
clsX.FirstName = "Bechir";
ClassPerson clsY = new ClassPerson();
clsY.LastName = "Bejaoui";
clsY.FirstName = "Bechir";
if (clsX.Equals(clsY))
{
Console.WriteLine("clsX = clsY");
}
else
{
Console.WriteLine("clsX != clsY");
}//This code displays clsX != clsY
Console.Read();
}
}
更多精彩
赞助商链接