public int GetInt()
 {
     if (myType.Equals(MyType.Int))
     {
         throw new Exception("wrong type");
     }
     return(i);
 }
Пример #2
0
 public T1 GetT1()
 {
     if (!myType.Equals(MyType.T1))
     {
         throw new Exception("wrong type");
     }
     return(i);
 }
Пример #3
0
        static void OverrideFromObject()
        {
            string str1 = "hello";
            string str2 = "hello";

            Console.WriteLine(str1);
            Console.WriteLine(str2);

            Console.WriteLine(str1 == str2);
            Console.WriteLine(str1.Equals(str2));
            Console.WriteLine(str1.GetHashCode());
            Console.WriteLine(str2.GetHashCode());//хеш коди будуть рівні бо слова однакові
            // і працює оптимізатор коду

            Console.WriteLine("add 1");
            str1 += "1";
            Console.WriteLine(str1.GetHashCode());//   hash code  - unique code for every object
            Console.WriteLine(str2.GetHashCode());

            Console.WriteLine("compare objects");
            MyType obj1 = new MyType(1);
            // obj1.name += "a";
            MyType obj2 = new MyType(2);

            Console.WriteLine(obj1.Equals(obj2));
            Console.WriteLine(obj1.GetHashCode());
            Console.WriteLine(obj2.GetHashCode());


            //---------------------------------
            Console.WriteLine("String builder vs string");
            string str3 = "hello";

            Console.WriteLine(str3.GetHashCode());
            str3 += "1";
            Console.WriteLine(str3.GetHashCode());
            str3 = str3.Remove(2, 1);
            Console.WriteLine(str3.GetHashCode());

            Console.WriteLine();
            StringBuilder strB = new StringBuilder("hello");

            Console.WriteLine(strB.GetHashCode());
            strB.Append("1");
            Console.WriteLine(strB.GetHashCode());
            strB.Remove(2, 1);
            Console.WriteLine(strB.GetHashCode());
            Console.WriteLine(strB);
        }