惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
月光博客
月光博客
F
Fortinet All Blogs
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
L
LangChain Blog
博客园 - 聂微东
G
Google Developers Blog
博客园 - Franky

博客园 - deerchao

Unity 在平面上播放含透明通道的视频(Play video with alpha channel on plane in unity) 域名迁移 错误721 -- 在虚拟机中连接VPN, 显示验证用户名和密码之后出错 - deerchao 繁体编码文本文件转换为简体编码的工具 生成VB多行字符串常量的工具 64位虚拟机Guest OS安装错误:0xC0000225 在64bit Win2008上运行Asp + Access网站 工具: 删除Visual Studio项目中文件链接,并把原文件复制到相应的目录 一个代表年月的类YearMonth Tip: Resharper 中 "Unknown Comment" 问题的解决办法 Tips 测试ConnectionString是否能连接上数据库服务器 奇怪的TreeView(WinForms)自动选中问题 From C# to VB MapPath的反函数(Reversing MapPath) A fast object clone class - using Expression.Compile() jQuery.combobox, 给文本框添加下拉选项的轻量级插件 CDTray, 打开,关闭光驱的系统托盘程序 jQuery.Excel, 使用Ctrl+方向键/Home/End在input表格中移动
Struct与赋值
deerchao · 2010-01-06 · via 博客园 - deerchao
    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);
        }

    }