zoukankan      html  css  js  c++  java
  • foreach 引发的值类型与引用类型思考

    用都知道的一句话概括:“引用类型在堆上,栈上只保存引用;值类型即可存放于栈上也可存放于堆上,值类型变量直接存储值本身”。

    class Program
    {
        static void Main(string[] args)
        {
            var list = new List<Person>()
            {
                new Person() {Name = "xfh"},
                new Person() {Age = 26}
            };
            foreach (var l in list)
            {
                if (l.Age != 0) continue;
                l.Age = 24;
                Console.WriteLine(l.Age);
            }
        }
    }
    
    class Person
    {
        public string Name { set; get; }
        public int Age { set; get; }
    }

    若把上述代码中的Person类型由class改为struct则会出现编译错误:
    Foreach iteration variable 'l' is immutable.Cannot modify struct member when accessed strucy is not classified as a variable.

    对于foreach,MSDN上有句话:
    The foreach statement is a wrapper around the enumerator,which only allows reading from,not writing to,the collection.

    Person为class时内存分配示意图:

     
    如上图,list集合中存储的是Person实例的地址,所以代码中的操作并不会使集合发生改变。

    Person为struct时内存分配示意图:

     

    如上图,list集合中存储的是Person实例,所以代码中的操作会使集合发生改变而引发错误。

    【延申】数组的内存分配

    数组元素可分为引用类型和值类型两种,其内存分配与上图中的list集合类似。

    Stackoverflow上讨论数组中存放值类型元素时内存如何分配的几句话:

    Object are always allocated on the heap.
    The heap only contains objects.
    Value types are either allocated on the stack, or part of an object on the heap.
    An array is an object.
    An array can only contain value types.
    An object reference is a value type.

    推荐阅读

    Arrays, heap and stack and value types

    The ‘in’-modifier and the readonly structs in C#

  • 相关阅读:
    laravel 5.5 仓库模式 文件之间接口与实现操作
    php 无线分类 根据子级找父级
    php 无限极分类,根据父级 找子级
    laravel5.4 中 dd和dump的区别。
    laravel hash密码生成和密码验证
    oracle建表详细信息
    关于组件的认识
    java的Thread Dump诊断工具
    weblogic连接池
    详解Oracle数据字典
  • 原文地址:https://www.cnblogs.com/Cwj-XFH/p/9224777.html
Copyright © 2011-2022 走看看