Class1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace new一个实例
{
    public class Class1
    {
        public int i { get; set; }


    }
}            

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace new一个实例
{
    class Program
    {
        static void Main(string[] args)
        {




            //var c2=new List<Class1>();
            //Class1 c1 = new Class1();
            //for (int i = 0; i < 100; i++)
            //{
               
            //    c1.i = i;


            //    c2.Add(c1);
            //}
            //foreach (var c in c2)
            //{
            //      Console.WriteLine(c.i.ToString());
            //}




            //---------------------------------


            //var c2 = new List<Class1>();
            //Class1 c1 = new Class1();
            //for (int i = 0; i < 100; i++)
            //{


            //    c1.i = i;


            //    c2.Add(c1);
            //}
            //foreach (var c in c2)
            //{
            //    Console.WriteLine(c.i.ToString());
            //}


            Console.ReadKey();
        }
    }
}


两段代码输出结果理解list的add院里


其实add时候并不是复制了值,而是传递了地址,所以
            //var c2 = new List<Class1>();
            //Class1 c1 = new Class1();
            //for (int i = 0; i < 100; i++)
            //{


            //    c1.i = i;


            //    c2.Add(c1);
            //}
            //foreach (var c in c2)
            //{
            //    Console.WriteLine(c.i.ToString());
            //}


            Console.ReadKey();
        }
    }这里的输出结果全都是99,希望会对大家有帮助


本文转载:CSDN博客