Part 57 to 58 Why shocould you override ToString and Equal Method, overridetostring

Source: Internet
Author: User

Part 57 to 58 Why shocould you override ToString and Equal Method, overridetostring

Part 57 Why shocould you override ToString Method

Sometimes you can override ToString method like that:

namepace Examplepublic class MainClass{  Customer C = new Customer();  C.firstName = "Lin";  C.lastName="Gester";  Console.Write(C.ToString());  //it will write Lin Gester;}public class Customer{  public string FirstName{get;set;}  public string LastName{get;set;}  public override string ToString()  {    return this.FirstName+""+this.LastName;  }}

Part 58 Why shocould you override Equals Method

public class MainClass{    private static void Main()    {       Customer C1 = new Customer();       C1.FirstName = "Lin";        C1.LastName = "Gester";       Customer C2 = new Customer();       C2.FirstName = "Lin";        C2.LastName = "Gester";       Console.Write(C1==C2);       Console.Write(C1.Equals(C2));                }  }public class Customer{    public string FirstName{get;set;}    public string LastName{get;set;}    public override bool Equals(Object obj)    {        if(obj==null)        {            return false;        }        if(!(obj is Customer))        {            return false;        }        return this.FirstName==((Customer)obj).FirstName&&this.LastName==((Customer)obj).LastName;    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.