public void Structs_are_passed_by_value()
        {
            var s = new SomeStruct(1, 2);

            Modify(s);

            Assert.AreEqual(1, s.X);
            Assert.AreEqual(2, s.Y);
        }
        private void Modify(out SomeStruct s)
        {
            // If s is not assigned a new value here the compiler complains,
            // and no values can be read from s since it might not be initialized

            // This is not allowed:
            // var oldX = s.X

            s = new SomeStruct(123, 2);
        }
        public void Ref_is_the_same_as_out_but_requires_that_the_object_has_been_initialized_and_does_not_have_to_assign_to_the_reference()
        {
            var s = new SomeStruct(1, 2);

            // This would not be allowed:
            // SomeStruct s;

            ModifyWithRefKeyword(ref s);

            Assert.AreEqual(123, s.X);
            Assert.AreEqual(2, s.Y);
        }
 // Cant be called Modify, since you can't have one overload with out and one with ref
 private void ModifyWithRefKeyword(ref SomeStruct s)
 {
     // These changes do actually affect the original value
     s.X = 123;
 }
 private void Modify(SomeStruct s)
 {
     // These changes do not affect the original value
     s.X = 123;
 }
Пример #6
0
        public void Structs_with_same_values_have_the_same_hash_code_by_default()
        {
            var a = new SomeStruct(1, 2);
            var b = new SomeStruct(1, 2);

            Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
        }
Пример #7
0
        public void Structs_with_same_values_are_equal_by_default()
        {
            var a = new SomeStruct(1, 2);
            var b = new SomeStruct(1, 2);

            Assert.AreEqual(a, b);
        }
Пример #8
0
        public void Structs_with_different_values_are_not_equal_by_default()
        {
            var a = new SomeStruct(1, 2);
            var b = new SomeStruct(1, 3);

            Assert.AreNotEqual(a, b);
        }