Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- object x = 1;
- object y = 1;
- //Everything is fine here
- //This also means that every type that inherits object has the "Equals method"
- //See: http://msdn.microsoft.com/en-us/library/ms173149.aspx
- Console.WriteLine(x.Equals(y));
- SimpleClass firstObject = new SimpleClass(1);
- SimpleClass secondOBject = new SimpleClass(1);
- //Value and reference types are compared differently.
- //Reference types actually call the "ReferenceEquals" method.
- //This method checks if the two variables refer to the same given object.
- //See: http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx
- Console.WriteLine("Simple Class objects equal?: " + firstObject.Equals(secondOBject));
- //If in the console we input the properties of the two objects, we might enter the same
- //properties but have different objects.
- //What if we want to say if 1 is bigger than 2 or not.
- y = 2;
- //I. Using an operator
- //Compile time error. Operators are not implemented for the object type
- //That means that in the general case they or not implemented for the derived
- //types too
- //Console.WriteLine(x<y);
- //II. Using the "CompareTo method"
- //Too bad. The IComparable interface is not implemented for the object type
- //That means that in the general case it is not implemented for the derived types too
- //Console.WriteLine(x.CompareTo(y));
- //If two types are different no compile time error with the "Equals" method
- Console.WriteLine(y.Equals(firstObject));
- Console.WriteLine(firstObject.Equals(y));
- }
- class SimpleClass
- {
- public int Value {get;set;}
- public SimpleClass(int value)
- {
- Value = value;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement