本文介绍了基本得到;组;问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码和C#的新手,我有一个非常基本的问题,我似乎无法通过。我正在尝试使用get;组;在对象中添加属性集的简写,但我不知道如何设置我的范围。 

Hi, i'm new to coding and C#, I have a really basic issue that i can't seem to get past. I'm trying to use the get; set; shorthand for adding a property set in an object, but i'm not sure how to set my range. 

我正在尝试缩短它(对于一个密切相关的值):

I'm trying to shorten this (for a closely related value):

private double heightOfSideWall;



            public double HeightOfSideWall

            {

                get {return heightOfSideWall; }¥b $ b               设置{if(value> 0)

                        {

                            heightOfSideWall = value;

                        }¥b $ b                    }¥b $ b            }

private double heightOfSideWall;

            public double HeightOfSideWall
            {
                get { return heightOfSideWall; }
                set { if (value > 0)
                        {
                            heightOfSideWall = value;
                        }
                    }
            }

我的努力看起来像这样,但是'get'是强调的,我想指定范围0到无穷大:

And my effort looks like this, but the 'get' is underlining, i want to specify the range 0 to infinity:

public double宽度{get;设置{if(value> 0); }

public double Width { get; set { if (value > 0) ; } }

我们非常感谢任何建议:)

Any advice would be much appreciated :)

推荐答案

public class MyObject
{
   //Auto property - compiler will generate the getter/setter implementations and define the backing field for you
   public int Id { get; set; }

   //Full property - you are responsible for the getter and setter methods (if the property needs both), you are also responsible for any backing field they need
   public string Name 
   {
      get { return _name ?? ""; }
      set { _name = value; }
   }
   private string _name;   
}

您无法在访问者级别混合使用它们。 auto属性语法等同于此。

You cannot mix and match them on an accessor level. The auto property syntax is equivalent to this.

public class MyObject
{
   public int Id { get; set; }

   //The generated code
   /*public int Id 
   {
      get { return _id; }
      set { _id = value; }
   }
   private int _id;*/
}

您应该使用满足您特定财产需求的方法。它们都生成相同的基本代码,只是提高了生产力。如果您不是简单地获取/设置支持字段,那么自动属性将不起作用,您将使用
完整语法。

You should use the approach that meets the needs for your specific property. They both generate the same basic code, it's just a productivity enhancement. If you are not simply getting/setting a backing field then an auto property won't work and you'll use the full syntax.

使用完整语法时,''经常会发现该属性只是一个声明。在这些情况下,您可以使用表达式主体而不是完整语句。再次,只是提高生产力,他们生成相同的代码。

When using the full syntax you'll often find the property is just a single statement. In these cases you can use an expression body instead of a full statement. Again, just a productivity enhancement, they generate the same code.

public class MyObject
{
   //Full property with simple accessors, use expression body
   public string Name
   {
      //Expression body in getter is the expression you want to return (without the return statement)
      get => _name ?? "";

      //Expression body in setter is just a single statement
      set => _name = value;
   }
   private string _name;
}

在你的代码中,heightOfSideWall是一个完整的属性,你可以根据需要使用表达式体来获取getter。 Width属性尝试混合auto和full,这是不允许的。由于你想做的不仅仅是获取/设置一个支持字段
,你必须使用完整的属性语法。

In your code the heightOfSideWall is a full property and you could use an expression body for the getter if you wanted. The Width property is trying to mix auto and full and that isn't allowed. Since you want to do something more than get/set a backing field you have to use the full property syntax.

public class MyObject
{
   public doublue HeightOfSideWall
   {
      get => heightOfSideWall;
      set 
      {
         if (value > 0) heightOfSideWall = value;
      }
   }
   private double heightOfSideWall;

   //You want to do more than get or set the backing field
   //so auto property syntax doesn't work
   public double Width 
   {
      get => width;
      set
      {
         if (value > 0) width = value;            
      }
   }
   private double width;
}


这篇关于基本得到;组;问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:52