struct在C#中被用来定义结构,它是一种比类小的数据类型。和类一样都是创建对象的模板,可以有自己的数据以及处理和访问数据的方法。

struct的用法:

    struct FurnitureSize
{
public double Height;
public double Length { get; set; }
public double Width;
static FurnitureSize()
{
//nothing
}
public FurnitureSize(double paramLength, double paramWidth, double paramHeight = 2.2)
{
this.Height = paramHeight;
this.Length = paramLength;
this.Width = paramWidth;
}
public double Volume
{
get
{
return Height* Length* Width;
}
}
public double Area()
{
return Volume/ Height;
}
}

从上边的代码片段中可以看出,struct和class几乎相同,那么struct和class除了关键字不同外,还有哪些不同呢?下面一一例举。

1.struct不能创建自己的无参构造函数(除了静态的无参构造函数)。而类则可以。

如果我给上边的FurnitureSize增加一个无参非静态构造函数的话就会被IntelliSense提示不能创建无参构造函数。

而类的话就可以创建自己的无参构造器(这样做没有意义),IntelliSense检查语法通过。

    class ConstructorTest
{
public int int1;
public string str1;
public Boolean bool1;
//static ConstructorTest() { }
public ConstructorTest()
{ }
public ConstructorTest(int paramInt,string paramString,Boolean paramBoolean)
{
Console.WriteLine("Constructor opened.");
}
}

为什么会这样?这是因为.Net运行库实现的时候如果碰到是Class类型的时候,会先判断你是否重载了构造函数,如果重载了新的构造函数并且又没有声明无参构造函数的话,

你想创建一个这个类的实例就只能使用新的有参构造函数。比如如果我在class ConstructorTest里没有定义无参构造函数的话,我想创建ConstructorTest的实例只能使用

ConstructorTest dct = new ConstructorTest(,"",false);

而struct不一样,.Net运行库运行struct时,不管你定义多少个有参构造函数它都会执行一遍无参构造函数,即使你没有显示的定义有参构造函数。

2.在struct里不能在声明的时候赋初值,而类可以。

类给字段赋初值:语法检查通过。

因为运行struct的时候运行库无论如何都会跑一遍struct的无参构造器,而在无参构造器里会给字段赋初值,数值型默认为0,引用型初始化为null。即使在struct里提供了字段的初始值也不能绕过默认构造函数。所以在struct里不能为字段显式的赋初始值。

3.结构是值类型,类是引用类型

结构和其他简单类型一样被分配在栈中,所以在实例化struct对象或者给struct里的字段赋值得时候可以像下边这样。

             FurnitureSize fs = new FurnitureSize();
fs.Height = 2.2;
fs.Width = 1.8;

也可以像这样:

             FurnitureSize fstmp;
fstmp.Height = 2.0;
fstmp.Width = 1.7;

而类的话只能先实例化对象,然后给对象的字段赋值。

             ConstructorTest dct = new ConstructorTest();
dct.int1 = ;

像这样赋值是不对的:

4.除了Object,它不能继承别的结构。

结构的继承链:结构派生于System.ValueType--->System.Object。不能为结构提供其他基类,每个类都派生于ValueType。

struct适合用于小的数据结构,最好是它来定义简单类型的结合体。在为struct分配内存的时候速度非常快,在结构作用域消失的时候删除也非常快,因为它是一种值类型。当把struct作为参数传递给方法时,应该把它作为ref参数传递,以避免性能损失。

struct和class的不同以及struct的应用场景的更多相关文章

  1. golang struct 转map 及 map[string]*Struct 初始化和遍历

    package main import ( "encoding/json" "errors" "fmt" "reflect&quo ...

  2. 字节流与数据类型的相互转换---使用struct模块

    字节流与数据类型的相互转换---使用struct模块 http://blog.csdn.net/Sunboy_2050/article/details/5974029 Python是一门非常简洁的语言 ...

  3. 漫谈C语言结构体struct、公用体union空间占用

    先用代码说话: #include<stdio.h> union union_data0{ int a ;//本身占用4个字节 char b ;//本身占用1个字节 int c ; }; u ...

  4. [转载]彻底弄清struct和typedef struct

    struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...

  5. C++11的enum class & enum struct和enum

    C++11的enum class & enum struct和enum C++标准文档--n2347(学习笔记) 链接:http://www.open-std.org/jtc1/sc22/wg ...

  6. 【Go入门教程4】struct类型(struct的匿名字段)

    struct Go语言中,也和C或者其他语言一样,我们可以声明新的类型,作为其它类型的属性或字段的容器.例如,我们可以创建一个自定义类型person代表一个人的实体.这个实体拥有属性:姓名和年龄.这样 ...

  7. struct和typedef struct彻底明白了

    struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...

  8. Python使用struct处理二进制

    有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最重 ...

  9. [C语言]关于struct和typedef struct

    在C中定义一个结构体类型要用typedef: *************************************************************************** t ...

随机推荐

  1. 本地yum仓库的搭建

    . 1.直接断开网络,模拟生产内网环境 2.将原先的网络yum仓库全部移动到 backup目录下 3.创建本地yum仓库  local_yum.repo vi /etc/yum.repos.d/loc ...

  2. OpenJDK中java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException解决办法

    http://www.cnblogs.com/xusweeter/p/9667801.html

  3. BZOJ1588 [HNOI2002]营业额统计 splay模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [Submit][S ...

  4. (26)C#WebService

    一.创建webservice 二.发布webservice 1.正式发布 (1)配置IIS 自己在局域网用的话,只需1,2,3 三步 1:网站的名称,将来IIS里有多个网站时可以方便区分 2:文件的本 ...

  5. Python与数据库[1] -> 数据库接口/DB-API[2] -> SQL Server 适配器

    SQL_Server适配器 / SQL_Server Adapter 1 环境配置 / Environment Configuration 安装SQL_Server的Python适配器包 pip in ...

  6. A/B Testing with Practice in Python (Part One)

    I learned A/B testing from a Youtube vedio. The link is https://www.youtube.com/watch?v=Bu7OqjYk0jM. ...

  7. The expression being assigned to optional parameter `v2' must be a constant or default value

    今天写代码的时候遇到一个问题以前没有遇到过的问题,就是当我给一个对象参数赋值默认值的时候,报错了,代码如下 public void ShowOrHiddenKuang(bool isShow,Vect ...

  8. Android Developer -- Bluetooth篇 开发实例之三 管理连接

    Managing a Connection When you have successfully connected two (or more) devices, each one will have ...

  9. Spring/Spring MVC/Spring Boot实现跨域

    说明:Spring MVC和Spring Boot其实用的都是同一套. CORS介绍请看这里:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Acc ...

  10. Stub, Mock and Proxy Testing

    Table of Contents Stubs, Mocks, and Proxies Stub, Mock, and Proxy Testing with Testimonial Mock test ...