何时使用Structure [英] when to use Structure

查看:129
本文介绍了何时使用Structure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们在c#中使用结构以及何时使用结构?

先谢谢你的帖子。

Why do we use structure and when to use structure in c#?
Thanks in Advance for your posts.

推荐答案

一个非常非常简单的谷歌搜索woudl发现你这个信息比询问要快得多这里 - 它不是一个新问题。

看看这个: http://programmers.stackexchange.com/questions/92339/when-do-you-use-a-struct-instead-of- a-class [ ^ ]很好地涵盖了它。



将来,请尝试自己做至少基础研究,并且不要浪费你的时间或我们的时间。
A very, very simple Google search woudl have found you this info a lot quicker than asking here - it''s not as if it''s a new question.
Have a look at this: http://programmers.stackexchange.com/questions/92339/when-do-you-use-a-struct-instead-of-a-class[^] which covers it pretty well.

In future, please try to do at least basic research yourself, and not waste your time or ours.


结构:



结构类型是一种值类型,可以包含构造函数,常量,字段,方法,属性,索引器,运算符,事件, d嵌套类型。结构的声明采用以下形式:



语法:



Structure:

A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. The declaration of a struct takes the following form:

Syntax:

[attributes] [modifiers] struct identifier [:interfaces] body [;]







其中:< br $>


属性(可选)

附加声明性信息。有关属性和属性类的更多信息,请参见17.属性。

修饰符(可选)

允许的修饰符是新的和四个访问修饰符。

标识符

结构名称。

接口(可选)

包含结构实现的接口的列表,全部由逗号。

正文

包含成员声明的结构体。



示例:




where:

attributes (Optional)
Additional declarative information. For more information on attributes and attribute classes, see 17. Attributes.
modifiers (Optional)
The allowed modifiers are new and the four access modifiers.
identifier
The struct name.
interfaces (Optional)
A list that contains the interfaces implemented by the struct, all separated by commas.
body
The struct body that contains member declarations.

Example:

// keyword_struct.cs
// struct declaration and initialization
using System;
public struct Point 
{
   public int x, y;

   public Point(int p1, int p2) 
   {
      x = p1;
      y = p2;    
   }
}

class MainClass 
{
   public static void Main()  
   {
      // Initialize:   
      Point myPoint = new Point();
      Point yourPoint = new Point(10,10);

      // Display results:
      Console.Write("My Point:   ");
      Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y);
      Console.Write("Your Point: ");
      Console.WriteLine("x = {0}, y = {1}", yourPoint.x, yourPoint.y);
   }
}





输出:





Output:

My Point:   x = 0, y = 0
Your Point: x = 10, y = 10


这篇关于何时使用Structure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆