I would like to know how to use the methods ReferenceEquals
, Equals
, GetType
, CompareTo
, GetTypeCode
.
I would like to know how to use the methods ReferenceEquals
, Equals
, GetType
, CompareTo
, GetTypeCode
.
Here's the list:
1- GetType - > Get the type of the variable Ex:
var teste = VariavelDecimal.GetType(); // Retorna Decimal
2- GetTypeCode - > Get the code Adjacent of the specified type Ex:
TypeCode typeCode = Type.GetTypeCode( testObject.GetType() );
3- CompareTo - > Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other Ex object:
var compareValue = theDay.CompareTo(DateTime.Today);
4-ReferenceEquals - > Indicates whether 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 above example has been taken from: link
5 Equals - > Checks whether 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 has been taken from: link
ReferenceEquals
Compares two references to objects and returns true
if they are equal.
Equals
Compare the objects and see if they are the same.
GetType
Gets 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 it does contain an enumeration rather than a type value.