Example #1
0
        static void Main()
        {
            var janet = new Person { FirstName = "Janet", LastName = "Jackson" };
              Person[] persons1 = { new Person { FirstName = "Michael", LastName = "Jackson" }, janet };
              Person[] persons2 = { new Person { FirstName = "Michael", LastName = "Jackson" }, janet };
              if (persons1 != persons2)
            Console.WriteLine("not the same reference");

              if (!persons1.Equals(persons2))
            Console.WriteLine("equals returns false - not the same reference");

              if ((persons1 as IStructuralEquatable).Equals(persons2, EqualityComparer<Person>.Default))
              {
            Console.WriteLine("the same content");
              }

              var t1 = Tuple.Create<int, string>(1, "Stephanie");
              var t2 = Tuple.Create<int, string>(1, "Stephanie");
              if (t1 != t2)
            Console.WriteLine("not the same reference to the tuple");

              if (t1.Equals(t2))
            Console.WriteLine("equals returns true");

              TupleComparer tc = new TupleComparer();

              if ((t1 as IStructuralEquatable).Equals(t2, tc))
              {
            Console.WriteLine("yes, using TubpleComparer");
              }
        }
Example #2
0
 static void SortPersons(Person[] persons)
 {
     Array.Sort(persons);
     foreach (Person p in persons)
     {
         Console.WriteLine(p);
     }
 }
Example #3
0
        static void SortUsingPersonComparer(Person[] persons)
        {
            Array.Sort(persons,
                new PersonComparer(PersonCompareType.FirstName));

            foreach (Person p in persons)
            {
                Console.WriteLine(p);
            }
        }
Example #4
0
        static void CopyArrays()
        {
            Person[] beatles = {
                     new Person { FirstName="John", LastName="Lennon" },
                     new Person { FirstName="Paul", LastName="McCartney" }
                   };

            Person[] beatlesClone = (Person[])beatles.Clone();
            Person[] pc = beatles;
            pc[0].FirstName = "F**k";
            foreach (Person p in beatles)
            {

                Console.WriteLine("p.FirstName:{0},p.lastName:{1}", p.FirstName, p.LastName);
            }

            //now changed the clone array.
            beatlesClone[0] = new Person() { FirstName = "Jack", LastName = "Deng" };
            Console.WriteLine("Clone array1");
            foreach(Person p in beatlesClone)
            {

                Console.WriteLine("p.FirstName:{0},p.lastName:{1}", p.FirstName , p.LastName);
            }
            Console.WriteLine("original array1");
            foreach (Person p in beatles)
            {

                Console.WriteLine("p.FirstName:{0},p.lastName:{1}", p.FirstName, p.LastName);
            }
            beatlesClone[0].FirstName = "Jack";
            beatlesClone[0].LastName = "Li";
            Console.WriteLine("Clone array2");
            foreach (Person p in beatlesClone)
            {

                Console.WriteLine("p.FirstName:{0},p.lastName:{1}", p.FirstName, p.LastName);
            }

            Console.WriteLine("original array2");
            foreach (Person p in beatles)
            {

                Console.WriteLine("p.FirstName:{0},p.lastName:{1}", p.FirstName, p.LastName);
            }
        }
Example #5
0
        static void SimpleArrays()
        {
            Person[] myPersons = new Person[2];

            myPersons[0] = new Person { FirstName = "Ayrton", LastName = "Senna" };
            myPersons[1] = new Person { FirstName = "Michael", LastName = "Schumacher" };

            Person[] myPersons2 = 
            { 
                new Person { FirstName="Ayrton", LastName="Senna"},
                new Person { FirstName="Michael", LastName="Schumacher"} 
            };
        }