2008-09-19 35 views
2

下面的代码显示了一个我最近用来解释结构和类对开发新品牌的不同行为的示例。有更好的方法吗? (是的 - 代码使用公共领域 - 这纯粹是为了简洁)有没有更好的方法来解释.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; 
    } 
} 
+0

** ** NB我总结类和结构之间的区别[这里](http://stackoverflow.com/a/20515034/1016343) – Matt 2013-12-23 09:58:58

回答

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 
+1

这是一个非常简单的例子,但它确实没有太大的区别。这显示了语法,但并不能解释为什么你会(或必须)使用其中一个或另一个。 – 2009-04-14 12:31:11

1

我想这是确定以尽可能值/引用类型而言这种方式示区别。不过,使用控制台输出方法可能会稍微清晰些。

至于你说你的“某人”是新的发展,这可能不是太重要,但在C#这里类和结构之间的进一步差异一个很好的列表:

C# struct/classes differences

4

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

如果你想解释一下是什么区别,那么你需要一段文字解释它。所解释的行为可以通过代码进行编辑。

通过Grimtron链接到页面有利于详述了类和结构之间的所有的个体差异,并且将它的一部分将用作概述解释,尤其是阅读以下条款:

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

但我不会链接到该页面作为解释为什么区别。这就像试图描述一辆汽车是什么样的,只是列出组成汽车的所有零件。你仍然需要了解大局,了解一辆车是什么,而这样的列表将无法给你。

在我看来,一个解释是告诉你事情是如何工作的,然后所有的细节自然地遵循。例如,如果您了解值类型与参考类型背后的基本基本原理,那么该页面上的许多细节都是有意义的,如果您仔细考虑,请参阅

例如,一个值类型(结构)被分配到它声明的位置,内联,也就是说。它占用堆栈空间,或者使内存中的类更大。但是,引用类型是指针,其大小固定为,其他实际对象存储在其他位置

利用上述的说明中,以下详细信息是有意义的:(即它总是占据的必要空间)

  • 结构体变量不能为空
  • 一个对象引用可以为空(即。指针可以指向什么)
  • 一个结构不增加压力,垃圾回收(垃圾收集的工作原理与堆,这是在对象住在其他空间的地方)
  • 始终有一个默认的构造函数。既然你可以声明任何值类型的变量(它基本上是一种结构体),但没有给它一个值,必须有一些基本的魔法来清除该空间(记住我说它占用空间无论如何)

其他事情,像所有与继承有关的事情,都需要他们自己参与解释。

等等......

0
  1. 我没有看到你想与你的样品显示什么。

  2. 我解释给人们的方式是“A结构持有的东西。一类东西吧。”

+1

这是典型的用法,但是没有任何东西阻止你创建一个拥有东西的结构类和一个可以处理它的结构。 – 2008-09-19 10:21:53

0

lassevk,

感谢您的(语义吹毛求疵旁白:=) - 但是,也许我是不明确的,因为我可以,我试图显示而不是告诉对于一个刚刚接触发展的人来说,这样的散文块就意味着与“星际迷航”技术可能性的平均水平差不多。

Grimtron链接到的页面和您的文本一定会有用,一旦我的新手更熟悉/舒适与.net和一般编程,我敢肯定!

1

A 结构是一种无懈可击的数据安排。松弛和被动。

A class在构造函数的眨眼中爆炸成行动。充满活力的班级是现代[编程]世界的超级英雄。

0

非常基本的区别是结构是值类型和类是引用类型

+0

Prashant - 对于那些刚刚接触发展的人来说,这并不意味着*任何东西*这是这个问题的重点=) – Rob 2009-04-16 09:29:42

相关问题