做一点小东西的时候,临时定义了一个Struct重新整理要现实的List<>.

Struct方法执行完毕后发现了这么一个现象,以下是现象演示代码;).大家看了以后,在使用Struct的时候请注意了.

    struct MyStruct
    {
        
public int Value;
        
public void Update(int i) { Value = i; }
    }

    
class Program
    {
        
static void Main(string[] args)
        {
            MyStruct[] list 
= new MyStruct[5];

            
for (int i = 0; i < 5; i++)
                Console.Write(list[i].Value.ToString() 
+ " ");
            Console.WriteLine();

            
for (int i = 0; i < 5; i++)
                list[i].Update(i 
+ 1);

            
for (int i = 0; i < 5; i++)
                Console.Write(list[i].Value 
+ " ");
            Console.WriteLine();

            Console.ReadLine();

            List
<MyStruct> llist = new List<MyStruct>(new MyStruct[5]);

            
for (int i = 0; i < 5; i++)
                Console.Write(llist[i].Value 
+ " ");
            Console.WriteLine();

            
for (int i = 0; i < 5; i++)
                llist[i].Update(i 
+ 1);

            
for (int i = 0; i < 5; i++)
                Console.Write(llist[i].Value 
+ " ");
            Console.WriteLine();

            Console.ReadLine();
        }


运行结果:
0 0 0 0 0
1 2 3 4 5

0 0 0 0 0
0 0 0 0 0


可能是我后知后觉了.大家见笑了.

相关文章:

  • 2022-01-21
  • 2021-09-07
  • 2021-09-04
  • 2022-12-23
  • 2021-09-10
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
  • 2021-09-05
相关资源
相似解决方案