Csharp/C Sharp/Language Basics/Reference Equal — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:39, 26 мая 2010

Equals and ReferenceEquals

using System;
   
   
public class Name {
  public string firstName;
  public string lastName;
   
  public Name(string firstName, string lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
   
  public void Display() {
    Console.WriteLine("firstName = " + firstName);
    Console.WriteLine("lastName = " + lastName);
  }
   
  public static Name Copy(Name car) {
    return (Name) car.MemberwiseClone();
  }
}
   
   
class Test {
  public static void Main() {
    Name myName = new Name("T", "M");
    Name myOtherName = new Name("P", "B");
    Console.WriteLine("myName details:");
    myName.Display();
    Console.WriteLine("myOtherName details:");
    myOtherName.Display();
   
    // set the myName object reference equal to myOtherName
    Console.WriteLine("Setting myName equal to myOtherName");
    myName = myOtherName;
   
    // check for equality
    Console.WriteLine("Name.Equals(myName, myOtherName) ="+ Name.Equals(myName, myOtherName));
    Console.WriteLine("Name.ReferenceEquals(myName, myOtherName) ="+ Name.ReferenceEquals(myName, myOtherName));
   
  }
   
}