结构是几个数据组成的数据结构
1)结构是一种值类型,用来封装一组相关的变量
2)想方法传递结构时候,通过值传递的方式进行传递
3)结构的实例化可以不用new
4)结构的构造函数必须带参数
5)不能继承,继承关系为System.Object--->Sysem.ValueType
6)结构可以实现接口
7)在结构中不能初始化示例字段
8)在结构中字段被声明 const或static,需要初始化,
结构的语法
结构修饰符 struct 结构名
{
}
结构的应用实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructDemo
{
public class Program
{
public struct Rect //定义结构
{
public double width; //字段
public double height;
public Rect(double x, double y)//构造方法
{
width = x;
height = y;
}
public double Area() //方法
{
return width * height;
}
}
public static void Main(string[] args)
{
Rect rect1; //结构的实例化可以不用new
rect1.width = 5;
rect1.height = 3;
Console.WriteLine(rect1.Area());
Rect rect2 = new Rect(6, 8);//结构的实例化
Console.WriteLine(rect2.Area());
}
}
}