コード例 #1
0
        private static void DemonstrateReferenceTypeMutabilityAndReferenceAssignment()
        {
            ConsoleHelper.WriteHeading(nameof(DemonstrateReferenceTypeMutabilityAndReferenceAssignment));

            var counterOne = new MyCounter();
            // counterTwo is same object as counterone
            var counterTwo = counterOne;

            // so when we increment the value inside counterOne it's also updating what we get from counterTwo
            counterOne.Increment();

            Console.WriteLine($"counterOne = {counterOne.Value}, counterTwo = {counterTwo.Value}");

            counterOne.Increment();

            PrintMyValue(counterOne);

            Console.WriteLine($"counterOne = {counterOne.Value}, counterTwo = {counterTwo.Value}");
        }
コード例 #2
0
 private static void PrintMyValue(MyCounter counter)
 {
     Console.WriteLine($"Value = {counter}");
     counter.Increment();
 }