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

时间:2008-09-19 09:51:56

标签: c# .net

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

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 个答案:

答案 0 :(得分:4)

嗯,你的解释根本不是解释,它是对行为的观察,这是不同的。

如果您想要解释差异是什么,那么您需要一段文字解释它。并且可以使用代码来解释所解释的行为。

由Grimtron链接的页面有助于详细说明类和结构之间的所有个体差异,其中的部分将用作概述说明,特别是阅读以下内容: / p>

  • 是否存在堆栈或堆?
  • 继承差异?

但我不会将该页面作为解释链接到差异所在。这就像试图描述汽车是什么,并列出构成汽车的所有零件。你仍然需要了解大局以了解汽车是什么,这样的清单无法给你这个。

在我看来,解释是告诉你某些事情是如何起作用的,然后所有的细节都自然地从中得到。

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

例如,一个值类型(struct)被分配在它被声明的位置,可以这么说。它占用了堆栈空间,或者使内存中的类更大。但是,引用类型是一个指针,它具有固定的大小,存储实际对象的内存中的其他位置

通过以上说明,以下细节是有道理的:

  • 结构变量不能为空(即它总是占用必要的空间)
  • 对象引用可以为null(即指针可以指向任何内容)
  • 结构不会给垃圾收集增加压力(垃圾收集与堆一起工作,这是其他地方空间中对象所在的位置)
  • 始终拥有默认构造函数。既然你可以声明任何值类型变量(它基本上是一种结构),而不给它一个值,那么必须有一些潜在的魔法可以清除那个空间(记住我说无论如何都占用空间)。

其他事情,比如与继承相关的所有事情,都需要自己解释。

等等......

答案 1 :(得分:1)

我想可以通过这种方式显示价值/参考类型的差异。但是,使用控制台输出方法可能会有点清晰。

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

C# struct/classes differences

答案 2 :(得分:1)

当struct / class是另一个类的成员时,差异可能更容易理解。

课程示例:

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)

结构是一种愚蠢,毫无生气的数据排列。松弛和被动。

会在构造函数的瞬间爆炸。充满活力的课程是现代[编程]世界的超级英雄。

答案 4 :(得分:0)

  1. 我没看到你试图用你的样品展示什么。

  2. 我向人们解释的方式是“结构持有东西。一个类

    来做事。”

答案 5 :(得分:0)

lassevk,

谢谢你(语义上的挑剔:=) - 然而,也许我不是那么清楚,我试图显示而不是告诉对于刚接触开发的人来说,像这样的散文块意味着与星际迷航技术的平均比例一样多。

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

答案 6 :(得分:0)

最基本的区别是结构是值类型,类是参考类型

相关问题