c# 结构体的两种赋值方法

首先结构体在声明时是不能赋值的

//正确
struct Time
{
	public int hour;
	public int min;
	public int sec;
};
//错误
struct Time
{
	public int hour = 1;
	public int min=2;
	public int sec=3;
};

赋值方式1
在方法中通过引用赋值

    private void Exp()
    {
        Time timer1 = new Time();
        timer1.hour = 10;
        timer1.min = 20;
        timer1.sec = 30;
    }

赋值方式2 生命对象时直接赋值

    Time timer = new Time()
    {
        hour = 10,
        min = 20,
        sec = 30
    };

你可能感兴趣的:(c#,c#)