How to use Referenceequals, Equals, Gettype, Compareto, Gettypecode?

Asked

Viewed 165 times

2

I would like to know how to use and what the methods are for ReferenceEquals, Equals, GetType, CompareTo, GetTypeCode.

2 answers

3


Follows the list :

1- Gettype -> Take the type of the variable Ex:

var teste = VariavelDecimal.GetType(); // Retorna Decimal

2- Gettypecode -> Grab the Adjacent code of the specified type Ex:

   TypeCode    typeCode = Type.GetTypeCode( testObject.GetType() );

3- Comparet -> Compares the current instance with another object of the same type and returns an integer number that indicates whether the current instance precedes, follows or occurs at the same position in the order of classification as the other object Ex:

var compareValue = theDay.CompareTo(DateTime.Today); 

4-Referenceequals -> Indicates if two objects have the same reference Ex :

    // Create two reference type instances that have identical values.
    TestClass tcA = new TestClass() { Num = 1, Name = "New TestClass" };
    TestClass tcB = new TestClass() { Num = 1, Name = "New TestClass" };

    Console.WriteLine("ReferenceEquals(tcA, tcB) = {0}",                                        Object.ReferenceEquals(tcA, tcB)); // false

This example above was taken from : http://msdn.microsoft.com/pt-br/library/dd183759.aspx

5-Equals -> Checks if the specified object is equal to the current object Ex:

  Person person1a = new Person("John");
  Person person1b = person1a;
  Person person2 = new Person(person1a.ToString());

  Console.WriteLine("Calling Equals:"); 
  Console.WriteLine("person1a and person1b: {0}", person1a.Equals(person1b));               
  Console.WriteLine("person1a and person2: {0}", person1a.Equals(person2));  

  Console.WriteLine("\nCasting to an Object and calling Equals:");
  Console.WriteLine("person1a and person1b: {0}", ((object) person1a).Equals((object) person1b));
  Console.WriteLine("person1a and person2: {0}", ((object) person1a).Equals((object) person2));

Example above was taken from : http://msdn.microsoft.com/pt-br/library/bsc2ak47(v=vs.110). aspx

  • 1

    Thank you so much guy for the list, very helpful, hug !

1

ReferenceEquals

Compare two references to objects and return true if they are equal.

Equals

Compares objects and checks if they are equal.

GetType

Get the class type of an object.

CompareTo

Compares two objects according to a value (such as a property, for example) that can be defined by the programmer, if the object is complex.

GetTypeCode

Equivalent to GetType, but brings an enumeration instead of a type value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.