Beispiel #1
0
        private async static void TakesStruct(MyStruct m)
        {
            await Task.Delay(300);

            await Task.Run(() =>
            {
                Console.WriteLine($"From inside: {m.Value} - {m.OtherValue}");
                Console.WriteLine(m.GetHashCode());
            });
        }
Beispiel #2
0
        public void Structs()
        {
            var s1 = new MyStruct(1, 2);
            var s2 = new MyStruct(1, 2);
            var s3 = new MyStruct(1, 3);

            IsTrue(s1.Equals(s2));
            IsTrue((object)s1 == (object)s2);
            IsTrue((System.ValueType)s1 == (System.ValueType)s2);
            IsFalse(s1.Equals(s3));
            IsTrue(s1.GetHashCode() != -1);
            Equal(s1.Sum, 3);
        }
Beispiel #3
0
        public void TestGetHashCode_valuetype()
        {
            MyStruct s = new MyStruct();

            s.SetMsg("Hello");
            s.ID       = 1;
            s.Birthday = new DateTime(1981, 1, 1);

            Console.WriteLine(s.GetHashCode());
            Console.WriteLine(s.GetMsg().GetHashCode());
            //s.SetMsg("NewMsg");
            s.ID       = 2;
            s.Birthday = new DateTime(1981, 11, 11);
            Console.WriteLine(s.GetHashCode());
            Console.WriteLine(s.GetMsg().GetHashCode());

            MyStruct s2 = new MyStruct();

            s2.SetMsg(s.GetMsg());
            s2.ID = 3;
            Assert.AreEqual(s.GetHashCode(), s2.GetHashCode());
        }
Beispiel #4
0
        private static void StructPassing()
        {
            var m = new MyStruct
            {
                Value      = true,
                OtherValue = true
            };

            TakesStruct(m);

            m.Value      = false;
            m.OtherValue = false;

            Console.WriteLine($"From outside: {m.Value} - {m.OtherValue}");
            Console.WriteLine(m.GetHashCode());
        }
Beispiel #5
0
    public static void Main()
    {
        MyStruct struct1 = new MyStruct {
            MyParam1 = 0
        };
        MyStruct struct2 = struct1;

        struct1.MyParam1 = 20;
        Console.WriteLine($"{struct1.GetHashCode()} != {struct2.GetHashCode()}");
        Console.WriteLine($"{struct1.MyParam1} == 20");
        Console.WriteLine($"{struct2.MyParam1} == 0");

        var class1 = new StructClass();
        var class2 = class1;

        Console.WriteLine($"{class1.GetHashCode()} == {class2.GetHashCode()}");
    }
Beispiel #6
0
 public override int GetHashCode()
 {
     unchecked
     {
         int hashCode = Number;
         hashCode = (hashCode * 397) ^ (Text != null ? Text.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (Nested != null ? Nested.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (List != null ? List.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (Array != null ? Array.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (Dictionary != null ? Dictionary.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ IgnoreInt;
         hashCode = (hashCode * 397) ^ PrivateInt;
         hashCode = (hashCode * 397) ^ (Polymorf1 != null ? Polymorf1.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (Polymorf2 != null ? Polymorf2.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (Custom != null ? Custom.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ Struct.GetHashCode();
         return(hashCode);
     }
 }
Beispiel #7
0
        static void Main(string[] args)
        {
            MyStruct instance;

            Console.WriteLine(instance.GetHashCode()); //HashCod'ы ОДИНАКОВЫ
            instance.Method();

            //Упаковка Boxing
            IInterface boxed = instance;

            Console.WriteLine(boxed.GetHashCode());   //HashCod'ы ОДИНАКОВЫ
            boxed.Method();

            //Распаковка UnBoxing
            MyStruct unBoxed = (MyStruct)boxed;

            Console.WriteLine(unBoxed.GetHashCode());  //HashCod'ы ОДИНАКОВЫ
            unBoxed.Method();

            //Delay
            Console.ReadKey();
        }