Пример #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with System.Object *****\n");

            Person p1 = new Person("Homer", "Simpson", 50);
            Person p2 = new Person("Homer", "Simpson", 50);

            Console.WriteLine("p1.ToString() = {0}", p1.ToString());
            Console.WriteLine("p2.ToString() = {0}", p2.ToString());

            Console.WriteLine("p1 = p2 ?: {0}", p1.Equals(p2));

            Console.WriteLine("Same hash codes?: {0}", 
                p1.GetHashCode() == p2.GetHashCode());

            p2.Age = 45;
            Console.WriteLine("p1.ToString() = {0}", p1.ToString());
            Console.WriteLine("p2.ToString() = {0}", p2.ToString());
            Console.WriteLine("p1 p2 ?: {0}", p1.Equals(p2));
            Console.WriteLine("Same hash codes?: {0}",
                p1.GetHashCode() == p2.GetHashCode());

            StaticMethodsOfObject();

            Console.ReadLine();
        }
Пример #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with System.Object *****\n");
            Person p4 = new Person();

            // Use inherited members of System.Object.
            Console.WriteLine("ToString: {0}", p4.ToString());
            Console.WriteLine("Hash Code: {0}", p4.GetHashCode());
            Console.WriteLine("type: {0}", p4.GetType());

            // Make some other references to p1.
            Person p3 = p4;
            object o = p3;

            // Are the references pointing to the same object in memory?
            if (o.Equals(p4) && p3.Equals(o))
            {
                Console.WriteLine("Same instance!");
            }
            Console.WriteLine();

            // NOTE: We want these to be identical to test
            // the Equals() and GetHashCode() methods.
            Person p1 = new Person("Homer", "Simpson", 50);
            Person p2 = new Person("Homer", "Simpson", 50);

            // Get stringified version of objects.
            Console.WriteLine("p1.ToString() = {0}", p1.ToString());
            Console.WriteLine("p2.ToString() = {0}", p2.ToString());

            // Test overridden Equals().
            Console.WriteLine("p1 = p2?: {0}", p1.Equals(p2));

            // Test overridden Equals()
            Console.WriteLine("p1 = p2: {0}", p1.Equals(p2));

            // Test hash codes.
            Console.WriteLine("Same hash codes?: {0}", p1.GetHashCode() == p2.GetHashCode());
            Console.WriteLine();

            // Change age of p2 and test again.
            p2.Age = 45;
            Console.WriteLine("p1.ToString() = {0}", p1.ToString());
            Console.WriteLine("p2.ToString() = {0}", p2.ToString());
            Console.WriteLine("p1 = p2?: {0}", p1.Equals(p2));
            Console.WriteLine("Same hash codes? {0}", p1.GetHashCode() == p2.GetHashCode());

            Console.WriteLine();
            StaticMembersOfObject();

            Console.ReadKey();
        }
Пример #3
0
		public static void Main (string[] args)
		{
			var person = new Person ();

			Console.WriteLine ("ToString: {0}", person.ToString ());
			Console.WriteLine ("Hash Code: {0}", person.GetHashCode ());
			Console.WriteLine ("Type: {0}", person.GetType ());

			Person person2 = person;
			object obj = person;

			//Are the references pointing to the same object in memory?
			if (obj.Equals (person) && person2.Equals (obj)) {
				Console.WriteLine("Same Instance");
			
				var pers1 = new Person("Homer", "Simpson", 50);
				var pers2 = new Person("Homer", "Simpson", 50);

				Console.WriteLine("pers1.ToString() = {0}",pers1.ToString());
				Console.WriteLine("pers2.ToString() = {0}",pers2.ToString());
				Console.WriteLine("pers1 = pers2: {0}", pers1.Equals(pers2));
				Console.WriteLine("Same hash codes? : {0}", pers1.GetHashCode() == pers2.GetHashCode());
				Console.ReadLine();
			}
			Console.ReadLine();
		}
Пример #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with System.Object *****\n");

            ////use inherited members of System.Object...
            //Console.WriteLine("ToString: {0}", p1.ToString());
            //Console.WriteLine("Hash Code: {0}", p1.GetHashCode());
            //Console.WriteLine("Type: {0}", p1.GetType());

            ////make some other refs to p1
            //Person p2 = p1;
            //object o = p2;
            ////are these refs pointing to the same obj in memory?
            //if (o.Equals(p1) && p2.Equals(o))
            //{
            //    Console.WriteLine("Same instance!");
            //}

            //make 2 identical Persons to test Equals() and GetHashCode()
            Person p1 = new Person("Homer", "Simpson", 50);
            Person p2 = new Person("Homer", "Simpson", 50);

            //get stringified versions of those objs
            Console.WriteLine("p1.ToString() = {0}", p1.ToString());
            Console.WriteLine("p2.ToString() = {0}", p2.ToString());

            //test overriden Equals()
            Console.WriteLine("p1 = p2?: {0}", p1.Equals(p2));

            //test hash codes
            Console.WriteLine("same hash codes?: {0}", p1.GetHashCode()==
                p2.GetHashCode());
            Console.WriteLine();

            //change age of p2 and retest
            p2.Age = 45;
            Console.WriteLine("p1.ToString() = {0}", p1.ToString());
            Console.WriteLine("p2.ToString() = {0}", p2.ToString());
            Console.WriteLine("p1 = p2?: {0}", p1.Equals(p2));
            Console.WriteLine("same hash codes?: {0}", p1.GetHashCode() ==
            p2.GetHashCode());
            Console.WriteLine();

            StaticMembersOfObject();

            Console.ReadLine();
        }
Пример #5
0
        static void Main(string[] args)
        {
            Person p1 = new Person();

            Console.WriteLine("ToString: {0}", p1.ToString());
            Console.WriteLine("Hash Code: {0}", p1.GetHashCode());
            Console.WriteLine("Type: {0}", p1.GetType());

            Person p2 = p1;
            object o = p2;

            if (o.Equals(p1) && p2.Equals(o))
            {
                Console.WriteLine("Same instance");
            }

            Person pNull = null;
            Console.WriteLine();
            Console.WriteLine("is pNull a person {0}", pNull is Person);
            Console.WriteLine("is p1 equal to null {0}", p1.Equals(null));
            Console.WriteLine("is empty string equal to null {0}", "" == null);
            Console.WriteLine();


            Person p11 = new Person("Homer", "Simpson", 50);
            Person p12 = new Person("Homer", "Simpson", 50);


            Console.WriteLine("ToString: {0}", p11.ToString());
            Console.WriteLine("p11.Hash Code: {0}", p11.GetHashCode());
            Console.WriteLine("p12.Hash Code: {0}", p12.GetHashCode());
            Console.WriteLine("is p11 equal to p12 {0}", p11.Equals(p12));

            p12.Age = 45;
            Console.WriteLine("is p11 equal to p12 {0}", p11.Equals(p12));
            Console.WriteLine("p11.Hash Code: {0}", p11.GetHashCode());
            Console.WriteLine("p12.Hash Code: {0}", p12.GetHashCode());

            Console.WriteLine();

            Console.WriteLine("=> static object methods");
            Person p3 = new Person("Sally", "Jones", 4);
            Person p4 = new Person("Sally", "Jones", 4);
            Console.WriteLine("object.Equals {0}", object.Equals(p3, p4));
            Console.WriteLine("object.ReferenceEquals {0}", object.ReferenceEquals(p3, p4));
            Console.ReadLine();
        }
Пример #6
0
		public static void Main (string[] args)
		{
			var person = new Person ();

			Console.WriteLine ("ToString: {0}", person.ToString ());
			Console.WriteLine ("Hash Code: {0}", person.GetHashCode ());
			Console.WriteLine ("Type: {0}", person.GetType ());

			Person person2 = person;
			object obj = person;

			//Are the references pointing to the same object in memory?
			if (obj.Equals (person) && person2.Equals (obj)) {
				Console.WriteLine("Same Instance");
			}
			Console.ReadLine();
		}
Пример #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Fun with System.Object");

            Person p1 = new Person();
            Person p2 = p1;
            object o = p2;

            if (o.Equals(p1) && p2.Equals(o))
            {
                Console.WriteLine("Same Instance");
            }

            Console.WriteLine("ToString: {0}", p1.ToString());
            Console.WriteLine("GetHashCode: {0}", p1.GetHashCode());
            Console.WriteLine("GetType: {0}", p1.GetType());

            Console.ReadLine();
        }
Пример #8
0
        static void Main( string[] args )
        {
            Console.WriteLine("***** Fun with System.Object *****\n");

            // NOTE:  We want these to be identical to test
            // the Equals() and GetHashCode() methods.
            Person p1 = new Person("Homer", "Simpson", 50);
            Person p2 = new Person("Homer", "Simpson", 50);

            // Get stringified version of objects.
            Console.WriteLine("p1.ToString() = {0}", p1.ToString());
            Console.WriteLine("p2.ToString() = {0}", p2.ToString());

            // Test Overridden Equals()
            Console.WriteLine("p1 = p2?: {0}", p1.Equals(p2));

            // Test hash codes.
            Console.WriteLine("Same hash codes?: {0}", p1.GetHashCode() == p2.GetHashCode());
            Console.WriteLine();

            // Change age of p2 and test again.
            p2.Age = 45;
            Console.WriteLine("p1.ToString() = {0}", p1.ToString());
            Console.WriteLine("p2.ToString() = {0}", p2.ToString());
            Console.WriteLine("p1 = p2?: {0}", p1.Equals(p2));
            Console.WriteLine("Same hash codes?: {0}", p1.GetHashCode() == p2.GetHashCode());

            Console.WriteLine();
            StaticMembersOfObject();

            Console.ReadLine();
        }