Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#设置并获取快捷方式属性_C#_Properties_Get_Set - Fatal编程技术网

C#设置并获取快捷方式属性

C#设置并获取快捷方式属性,c#,properties,get,set,C#,Properties,Get,Set,我正在看一个C#上的教学视频,他们显示了一个快捷方式(键入“prop”,tab两次),它生成了这个 public int Height { get; set; } 于是他走了一条捷径,用=>代替这个。它试图将两者结合起来,但长度出现错误: class Box { private int length; private int height; private int width; private int volume; public Box(in

我正在看一个C#上的教学视频,他们显示了一个快捷方式(键入“prop”,tab两次),它生成了这个

public int Height { get; set; }
于是他走了一条捷径,用=>代替这个。它试图将两者结合起来,但长度出现错误:

    class Box
{
    private int length;
    private int height;
    private int width;
    private int volume;

    public Box(int length, int height, int width)
    {
        this.length = length;
        this.height = height;
        this.width = width;
    }


    public int Length { get => length; set => length = value; } // <-error
    public int Height { get; set; }
    public int Width { get; set; }
    public int Volume { get { return Height * Width * Length; } set { volume = value; } }

    public void DisplayInfo()
    {
        Console.WriteLine("Length is {0} and height is {1} and width is {2} so the volume is {3}", length, height, width, volume = length * height * width);
    }

}
类框
{
私有整数长度;
私人内部高度;
私有整数宽度;
私人国际卷;
公用框(整数长度、整数高度、整数宽度)
{
这个长度=长度;
高度=高度;
这个。宽度=宽度;
}

public int Length{get=>Length;set=>Length=value;}/您可以使用
=>
语法作为C#6.0中只读属性的快捷方式(您不能将它们与
集一起使用),而在C#7.0中,它们被扩展为包括代码中的
set
访问器(同样需要备份字段)

很有可能您使用的是C#6,因此在
集合
语法上出现了错误

您询问了如何缩短代码,并且由于您不需要私人支持成员(您没有修改
集合
获取
访问器中的值),最短的方法是去掉它们,只使用用户可以设置的。然后您可以使用
=>
作为
属性,因为它应该是只读的(因为它是一个计算字段):

我相信这是您描述的类的最短代码:

class Box
{
    public int Length { get; set; }
    public int Height { get; set; }
    public int Width { get; set; }
    public int Volume => Height * Width * Length;

    public Box(int length, int height, int width)
    {
        Length = length;
        Height = height;
        Width = width;
    }

    public void DisplayInfo()
    {
        Console.WriteLine("Length = {0}, Height = {1}, Width = {2}, Volume = {3}", 
            Length, Height, Width, Volume);
    }

}

您可以使用
=>
语法作为C#6.0中只读属性的快捷方式(您不能将其与
一起使用),而在C#7.0中,它们被扩展为包括代码中的
访问器(也需要支持字段)

很有可能您使用的是C#6,因此在
集合
语法上出现了错误

您询问了如何缩短代码,并且由于您不需要私人支持成员(您没有修改
集合
获取
访问器中的值),最短的方法是去掉它们,只使用用户可以设置的。然后您可以使用
=>
作为
属性,因为它应该是只读的(因为它是一个计算字段):

我相信这是您描述的类的最短代码:

class Box
{
    public int Length { get; set; }
    public int Height { get; set; }
    public int Width { get; set; }
    public int Volume => Height * Width * Length;

    public Box(int length, int height, int width)
    {
        Length = length;
        Height = height;
        Width = width;
    }

    public void DisplayInfo()
    {
        Console.WriteLine("Length = {0}, Height = {1}, Width = {2}, Volume = {3}", 
            Length, Height, Width, Volume);
    }

}

似乎对我有用:在当前示例中,您不需要私有的
长度
字段,就像
高度
宽度
一样,您可以删除私有字段,只需将自动属性与
属性名称{get;set;}
您可能也不应该为
卷设置
,因为它是一个计算字段。如果有人更改了
,您将如何调整其他大小属性?对于
,您可能只是指
公共整数卷=>高度*宽度*长度;
@错误框中的列表状态(字段)int Box.length Only赋值、调用、减量和新对象表达式可以用作语句编辑:我使用的是C#6。需要升级。似乎对我有用:在当前示例中,您不需要私有的
长度
字段,就像
高度
宽度
一样,您可以删除私有字段,只需使用使用
PropertyName{get;set;}自动创建属性
您可能也不应该为
卷设置
,因为它是一个计算字段。如果有人更改了
,您将如何调整其他大小属性?对于
,您可能只是指
公共整数卷=>高度*宽度*长度;
@错误框中的列表状态(字段)int Box.length Only赋值、调用、减量和新对象表达式可以用作语句编辑:我使用的是C#6。需要升级。整洁,这使代码更简单。谢谢。编辑:另一位用户确实提到我使用的是旧版本,这就是我出现初始错误的原因。但他被否决并删除了他的answ呃。整洁,这使代码更简单。谢谢。编辑:另一个用户确实提到我使用的是旧版本,这就是我出现初始错误的原因。但是他被否决并删除了他的答案。