Пример #1
0
        public void ImmutableRecordsAndWith()
        {
            var immutableRecord = new ImmutableRecordType {
                IntProperty = 42, StringProperty = "foo"
            };
            //Sometimes you want a copy of an immutable object, but with
            //one or more properties changed. You can use a with
            //expression to do that.
            var newImmutableRecord = immutableRecord with {
                IntProperty = 43
            };

            Assert.That(newImmutableRecord.IntProperty, Is.EqualTo(43));
            Assert.That(newImmutableRecord.StringProperty, Is.EqualTo("foo"));
        }
Пример #2
0
        public void RecordsDoNotUseDuckTyping()
        {
            var recordType1 = new MyRecordType {
                IntProperty = 42, StringProperty = "foo"
            };
            var recordType2 = new ImmutableRecordType {
                IntProperty = 42, StringProperty = "foo"
            };

            Assert.That(recordType1.Equals(recordType2), Is.False);

            //The compiler already knows that these two records are different types,
            //so the following line results in a compiler error.
            //Assert.That(recordType1 == recordType2, Is.False);
        }
Пример #3
0
        public void ImmutableRecords()
        {
            var mutableRecord = new MyRecordType {
                IntProperty = 42
            };
            var immutableRecord = new ImmutableRecordType {
                IntProperty = 42
            };

            mutableRecord.IntProperty = 43;

            //The following line results in a compiler error.
            //immutableRecord.IntProperty = 43;

            Assert.That(mutableRecord.IntProperty, Is.EqualTo(43));
            Assert.That(immutableRecord.IntProperty, Is.EqualTo(42));
        }