京东云服务器推荐配置、多种配置选择,满足不同业务需求!

热门推荐

⭐ 稳定入门选择

配置:2核2G/5M

适用场景:个人站点 / 轻量应用

特点:性价比高,稳定可靠

¥19/月
立即购买
流量友好

☁️ 轻量云主机

配置:2核4G 5M

适用场景:小型业务 / 博客 / 测试环境

特点:流量友好,性能均衡

¥149/年
立即购买
高性能

💻 云服务主机

配置:4核16G 8M

适用场景:中小型服务 / 多并发场景

特点:充足内存与带宽,性能强劲

¥750/年
立即购买
新人专享

🎁 新人福利

新人访问 → 点击 → 最新活动

可获取 3000元 礼券

立即领取
服务端 / C# 教程 / C# 菜鸟教程
C# 结构体(Struct)
C# FileInfoC# do...while 循环

C# 结构体(Struct)

 在C#中,struct是表示数据结构的值类型数据类型。 它可以包含参数化构造函数、静态构造函数、常量、字段、方法、属性、索引器、运算符、事件和嵌套类型。

struct 可以用于保存不需要继承的小数据值,例如坐标点,键值对和复杂的数据结构。

结构声明

使用struct关键字声明结构。默认修饰符是结构及其成员的内部修饰符。

下面的示例声明坐标图的结构。

struct Coordinate
{
    public int x;
    public int y;
}

与基本类型变量一样,可以使用或不使用 new 运算符创建struct对象。

struct Coordinate
{
    public int x;
    public int y;
}

Coordinate point = new Coordinate();
Console.WriteLine(point.x); //输出:0  
Console.WriteLine(point.y); //输出:0

上面,使用new关键字创建一个 Coordinate(坐标) 结构的对象。它调用结构的默认无参数构造函数,该构造函数将所有成员初始化为指定数据类型的默认值。

如果声明一个 struct 类型的变量而不使用 new 关键字,则它不调用任何构造函数,因此所有成员都保持未赋值状态。因此,在访问每个成员之前,必须为它们分配值,否则会产生编译时错误。

struct Coordinate
{
    public int x;
    public int y;
}

Coordinate point;
Console.Write(point.x); // 编译时错误  

point.x = 10;
point.y = 20;
Console.Write(point.x); //输出:10  
Console.Write(point.y); //输出:20

结构构造函数

结构(struct)不能包含无参数构造函数。它只能包含参数化构造函数或静态构造函数。

struct Coordinate
{
    public int x;
    public int y;

    public Coordinate(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

Coordinate point = new Coordinate(10, 20);

Console.WriteLine(point.x); //输出:10  
Console.WriteLine(point.y); //输出:20

必须在参数化构造函数中包含该结构的所有成员,并将其分配给成员;否则,如果任何成员保持未分配状态,C#编译器将给出编译时错误。

结构的方法和属性

struct(结构)可以包含属性、自动实现的属性、方法等,与类相同。

struct Coordinate
{
    public int x { get; set; }
    public int y { get; set; }

    public void SetOrigin()
    {
        this.x = 0;
        this.y = 0;
    }
}

Coordinate point = Coordinate();
point.SetOrigin();

Console.WriteLine(point.x); //输出:0  
Console.WriteLine(point.y); //输出:0

以下struct包括静态方法。

struct Coordinate
{
    public int x;
    public int y;

    public Coordinate(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public static Coordinate GetOrigin()
    {
        return new Coordinate();
    }
}

Coordinate point = Coordinate.GetOrigin();

Console.WriteLine(point.x); //输出:0  
Console.WriteLine(point.y); //输出:0

结构事件

结构可以包含事件通知订阅者有关某些操作。 下面的结构体(struct)包含一个事件。

struct Coordinate
{
    private int _x, _y;

    public int x 
    {
        get{
            return _x;
        }

        set{
            _x = value;
            CoordinatesChanged(_x);
        }
    }

    public int y
    {
        get{
            return _y;
        }

        set{
            _y = value;
            CoordinatesChanged(_y);
        }
    }

    public event Action<int> CoordinatesChanged;
}

上面的结构包含coordinateChanged事件,当 x 或 y 坐标发生变化时将引发该事件。下面的示例演示如何处理CoordinateChanged事件。

class Program
{
    static void Main(string[] args)
    {

        Coordinate point = new Coordinate();
        
        point.CoordinatesChanged += StructEventHandler;
        point.x = 10;
        point.y = 20;
    }

    static void StructEventHandler(int point)
    {
        Console.WriteLine("坐标更改为 {0}", point);
    }
}

struct是值类型,因此它比类对象快。每当您只想存储数据时,请使用struct。通常,结构适用于游戏编程。但是,与结构相比,传输类对象更容易。因此,当您通过网络或其他类传递数据时,请不要使用struct。

总结

  • struct 可以包括构造函数,常量,字段,方法,属性,索引器,运算符,事件和嵌套类型。

  • struct 不能包含无参数构造函数或析构函数。

  • struct 可以实现与类相同的接口。

  • struct 不能继承另一个结构或类,也不能作为一个类的基类。

  • struct 不能将成员指定为抽象成员,密封成员,虚拟成员或受保护成员。

C# FileInfoC# do...while 循环