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);
            }
    
        }
    
    

  • 相关阅读:
    GIT提交修改的项目到远程仓库
    MySQL基础知识总结
    IDEA集成码云gitee
    SpringBoot整合ueditor编辑器
    word2010中统一调整表格格式
    Afinal简介和使用方法
    Java 8 安装——使用华为镜像
    微信小程序调用第三方组件
    webpack-dev-server first try
    性能测试应用领域
  • 原文地址:https://www.cnblogs.com/deerchao/p/1640134.html
Copyright © 2011-2022 走看看