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

  • 相关阅读:
    2014下半年软考总结
    Java学习之Filter
    java学习之PreparedStatement
    java学习之dom4j 对 xml的读取
    Windows查看端口占用及杀掉进程
    CentOS压力测试工具Tsung安装和图形报表生成Tsung安装配置
    Tsung压力测试:Openfire
    CentOS Mysql安装配置
    nodejs、webpack
    IOS操作系统上执行monkey测试
  • 原文地址:https://www.cnblogs.com/deerchao/p/1640134.html
Copyright © 2011-2022 走看看