代码之家  ›  专栏  ›  技术社区  ›  Rob

是否有更好的方法来解释.net中结构和类之间的行为差异?

  •  2
  • Rob  · 技术社区  · 17 年前

    下面的代码显示了我最近用来向开发新手解释结构和类的不同行为的示例。有更好的方法吗?(是的,代码使用公共字段,这纯粹是为了简洁起见)

    namespace StructsVsClasses
    {
        class Program
        {
            static void Main(string[] args)
            {
                sampleStruct struct1 = new sampleStruct();
                struct1.IntegerValue = 3;
                Console.WriteLine("struct1.IntegerValue: {0}", struct1.IntegerValue);
    
                sampleStruct struct2 = struct1;
                Console.WriteLine();
                Console.WriteLine("struct1.IntegerValue: {0}", struct1.IntegerValue);
                Console.WriteLine("struct2.IntegerValue: {0}", struct2.IntegerValue);
                struct1.IntegerValue = 5;
                Console.WriteLine();
                Console.WriteLine("struct1.IntegerValue: {0}", struct1.IntegerValue);
                Console.WriteLine("struct2.IntegerValue: {0}", struct2.IntegerValue);
    
                sampleClass class1 = new sampleClass();
                class1.IntegerValue = 3;
                Console.WriteLine();
                Console.WriteLine("class1.IntegerValue: {0}", class1.IntegerValue);
    
                sampleClass class2 = class1;
                Console.WriteLine();
                Console.WriteLine("class1.IntegerValue: {0}", class1.IntegerValue);
                Console.WriteLine("class2.IntegerValue: {0}", class2.IntegerValue);
                class1.IntegerValue = 5;
                Console.WriteLine();
                Console.WriteLine("class1.IntegerValue: {0}", class1.IntegerValue);
                Console.WriteLine("class2.IntegerValue: {0}", class2.IntegerValue);
    
                Console.ReadKey();
            }
        }
    
        struct sampleStruct
        {
            public int IntegerValue;
        }
    
        class sampleClass
        {
            public int IntegerValue;
        }
    }
    
    7 回复  |  直到 14 年前
        1
  •  4
  •   Lasse V. Karlsen    17 年前

    好吧,你的 解释 这根本不是一种解释,而是对行为的观察,这是不同的。

    如果你想解释一下区别是什么,那么你需要一段文字 解释 并且可以用代码来解释所解释的行为。

    Grimtron链接到的页面非常适合详细说明类和结构之间的所有个体差异,其中的一些片段可以作为 概述说明 ,特别阅读以下项目:

    • 存在于堆栈或堆上?
    • 继承差异?

    但我不会链接到该页面作为 解释 区别是什么。这就像试图描述汽车是什么,只是列出构成汽车的所有部件。你仍然需要了解大局才能理解汽车是什么,这样的清单无法给你答案。

    在我看来,解释是告诉你某件事是如何运作的,然后所有的细节都会自然而然地随之而来。

    例如,如果你了解值类型与引用类型背后的基本基本原理,那么该页面上的许多细节都是有意义的, 如果你仔细想想 .

    例如,一个值类型(结构体)被分配到声明它的地方,可以说是内联的。它会占用堆栈空间,或者使类在内存中变大。然而,引用类型是一个具有固定大小的指针,指向 内存中存储实际对象的其他位置 .

    根据上述解释,以下细节是有意义的:

    • 结构变量不能为null(即它总是占用必要的空间)
    • 对象引用可以为null(即指针可以指向任何东西)
    • 结构体不会给垃圾回收增加压力(垃圾回收与堆一起工作,堆是对象所在的地方 别的地方 空间)
    • 始终具有默认构造函数。因为你可以声明任何值类型变量(基本上是一种结构),而不给它一个值,所以一定有一些底层魔法可以清理这个空间(记得我说过它无论如何都会占用空间)

    其他事情,就像所有与继承相关的事情一样,需要在解释中有自己的一部分。

    等等。。。

        2
  •  1
  •   Hallgrim    16 年前

    当结构/类是另一个类的成员时,这种差异可能更容易理解。

    类示例:

    class PointClass {
      int double X;
      int double Y;
    }
    
    class Circle {
      PointClass Center = new PointClass() { X = 0, Y = 0; }
    }
    
    static void Main() {
      Circle c = new Circle();
      Console.WriteLine(c.Center.X);
      c.Center.X = 42;
      Console.WriteLine(c.Center.X);
    }
    

    输出:

    0
    42
    

    结构体示例:

    struct Point {
      int double X;
      int double Y;
    }
    
    class Circle {
      PointStruct Center = new PointStruct() { X = 0, Y = 0; }
    }
    
    static void Main() {
      Circle c = new Circle();
      Console.WriteLine(c.Center.X);
      c.Center.X = 42;
      Console.WriteLine(c.Center.X);
    }
    

    输出:

    0
    0
    
        3
  •  1
  •   Grimtron    17 年前

    我想以这种方式显示值/引用类型的差异是可以的。不过,使用控制台输出的方法可能会更干净一些。

    正如你所说,你的“某人”是开发新手,这可能不太重要,但这里有一个很好的列表,列出了C#中类和结构之间的进一步差异:

    C# struct/classes differences

        4
  •  1
  •   Ed Guiness    16 年前

    A. 结构 是一种软弱无力、毫无生气的数据排列。软弱和被动。

    A. 在一个构造函数的眨眼间爆发出来。一个充满活力的班级是现代编程世界的超级英雄。

        5
  •  0
  •   Brad Bruce    17 年前
    1. 我不明白你想用你的样品展示什么。

    2. 我向人们解释它的方式是“一个结构 使保持 东西。类别 与之相关的事物”。

        6
  •  0
  •   Rob    17 年前

    拉斯维克,

    谢谢你(撇开语义上的吹毛求疵:=)-但是,也许我没有说清楚,我只是想说清楚 显示 而不是 告诉 对于一个新开发的人来说,这样一段散文的意义大约相当于《星际迷航》式的技术胡言乱语。

    一旦我的新手对.net和编程更加熟悉/熟悉,Grimtron链接的页面和你的文本肯定会很有用,我敢肯定!

        7
  •  0
  •   Prashant Cholachagudda    16 年前

    最基本的区别是结构是值类型,类是引用类型