Equals and ReferenceEquals : Reference Equal : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Reference Equal »

 

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));
   
  }
   
}
           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Reference Equal