zoukankan      html  css  js  c++  java
  • Struct与赋值

        using System;
        using System.Collections.Generic;
    
        struct SomeStruct
        {
            public int SomeValue;
        }
    
        class SomeContainerClass
        {
            public SomeStruct Struct;
        }
    
        struct SomeContainerStruct
        {
            public SomeStruct Struct;
        }
    
        class Program
        {
            static void Main()
            {
                TestStruct();
                TestClass();
                TestArray();
                TestList();
    
                Console.ReadKey();
            }
    
            static SomeStruct GetDefaultValue()
            {
                return new SomeStruct { SomeValue = 100 };
            }
    
    
            private static void TestStruct()
            {
                ShowTestName("TestStruct:");
                var c = new SomeContainerStruct { Struct = GetDefaultValue() };
                ShowResult("original:", c.Struct.SomeValue);
    
                var s = c.Struct;
                s.SomeValue = 200;
                ShowResult("indirectly:", c.Struct.SomeValue);
    
                c.Struct.SomeValue = 300;
                ShowResult("directly:", c.Struct.SomeValue);
            }
    
            private static void TestClass()
            {
                ShowTestName("TestClass:");
                var c = new SomeContainerClass { Struct = GetDefaultValue() };
                ShowResult("original:", c.Struct.SomeValue);
    
                var s = c.Struct;
                s.SomeValue = 200;
                ShowResult("indirectly:", c.Struct.SomeValue);
    
                c.Struct.SomeValue = 300;
                ShowResult("directly:", c.Struct.SomeValue);
            }
    
            private static void TestArray()
            {
                ShowTestName("TestArray:");
                var c = new[] { GetDefaultValue() };
                ShowResult("original:", c[0].SomeValue);
    
                var s = c[0];
                s.SomeValue = 200;
                ShowResult("indirectly:", c[0].SomeValue);
    
                c[0].SomeValue = 300;
                ShowResult("directly:", c[0].SomeValue);
            }
    
            private static void TestList()
            {
                ShowTestName("TestList:");
                var c = new List<SomeStruct> { GetDefaultValue() };
                ShowResult("original:", c[0].SomeValue);
    
                var s = c[0];
                s.SomeValue = 200;
                ShowResult("indirectly:", c[0].SomeValue);
    
                //the follow line won't compile
                //Cannot modify the return value of 'System.Collections.Generic.List<StructTest.SomeStruct>.this[int]' because it is not a variable
                //c[0].SomeValue = 300;
                //ShowTestName("directly:", c[0].SomeValue);
            }
    
    
            static void ShowResult(string condition, int value)
            {
                Console.Write(condition);
                Console.Write("\t");
                Console.WriteLine(value);
            }
    
            static void ShowTestName(string message)
            {
                Console.WriteLine();
                Console.WriteLine(message);
            }
    
        }
    
    

  • 相关阅读:
    Redis开发与运维:SDS
    Redis开发与运维:数据迁移
    我的2019上半年
    C# 并发编程
    经典排序算法 — C# 版(上)
    图解 -- 树的汇总
    图解--队列、并发队列
    栈到CLR
    我们的数组
    算法复杂度
  • 原文地址:https://www.cnblogs.com/deerchao/p/1640134.html
Copyright © 2011-2022 走看看