Visual Studio: Better equals Method for executing code snippets

Source: Internet
Author: User

The equals code snippets provided by Visual Studio is expanded as follows:

// Override object. equals

Public override bool equals (Object OBJ)

{

//

// See the full list of guidelines

// Http://go.microsoft.com/fwlink? Linkid = 85237

// And also the guidance for operator =

// Http://go.microsoft.com/fwlink? Linkid = 85238

//

 

If (OBJ = NULL | GetType ()! = Obj. GetType ())

{

Return false;

}

 

// Todo: Write Your Implementation of equals () here

Throw new notimplementedexception ();

Return base. Equals (OBJ );

}

 

// Override object. gethashcode

Public override int gethashcode ()

{

// Todo: Write Your Implementation of gethashcode () here

Throw new notimplementedexception ();

Return base. gethashcode ();

}

 

I think there are the following Disadvantages:

  1. Use GetType ()! = Obj. GetType () to determine the type. It is clear that the derived type is not considered.
  2. There is no strongly typed equals method execution.
  3. Contains additional comments.

 

Therefore, I wrote an enhanced code snippets, named sex. Overcome these shortcomings.

When you use sex in a type:

 

The Expo was launched as follows:

Class

{

Public override bool equals (Object OBJ)

{

VaR TMP = OBJ as;

If (TMP = NULL)

{

Return false;

}

Return equals (TMP );

}

 

Public bool equals (a obj)

{

}

 

Public override int gethashcode ()

{

 

}

}

It automatically contains the correct, strong type equals method execution! The gethashcode method is also available.

 

File Download
Download Page
Note: the link is the Microsoft SkyDrive page. When downloading, use a browser to download it directly. Some download tools may not be available for downloading.
File Type:. snippet File

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.