C#中的结构

结构是几个数据组成的数据结构

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());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值