以下是一个拒绝接受null值的属性.
请问在最新的c#8.0中,是否还有更加简洁的写法.
最好能在不需要_xxx变量的情况下达到目的,就像普通的属性只用写{ get; set; }一样.谢谢        private string _xxx;        public string xxx
        {
            get => _xxx;
            set => _xxx = value ?? throw new ArgumentNullException();
        }

解决方案 »

  1.   

    你是说这样吗?
    public string message { get; set; }
      

  2.   

    属性:public int age{get;set;}    //自动属性   public int age{} 这种编译不通过,get和set至少有一个,分别代表,取值和赋值变量:public int age; PS:老版本(.Net2.0之前)的属性写法:  private int age;  public int Age  {    get { return age; }    set { age = value; }
      }网上找的差异     
      

  3.   

    属性:public int age{get;set;}    //自动属性
    变量:public int age;
    這兩個那種比較好啊
      

  4.   

    属性:public int age{get;set;}    //自动属性
    变量:public int age;
    這兩個那種比較好啊
    他俩是一个意思。  只是2.0以前 不支持 属性 有默认值。  必须用第二种。 现在都4.0往上了 甚至都core了。  
    我一直都是public int age{get;set;}  这么写的
      

  5.   

    属性:public int age{get;set;}    //自动属性
    变量:public int age;
    這兩個那種比較好啊
    他俩是一个意思。  只是2.0以前 不支持 属性 有默认值。  必须用第二种。 现在都4.0往上了 甚至都core了。  
    我一直都是public int age{get;set;}  这么写的
    我一直都是這樣用public int age=0
      

  6.   

    属性:public int age{get;set;}    //自动属性
    变量:public int age;
    這兩個那種比較好啊
    他俩是一个意思。  只是2.0以前 不支持 属性 有默认值。  必须用第二种。 现在都4.0往上了 甚至都core了。  
    我一直都是public int age{get;set;}  这么写的
    我一直都是這樣用public int age=0public int age=0 是定义变量  可以赋值 取值   不是私有public int age{get;set;}  同上 不同根据 get 或者 set  赋值取值。 比如只能取值public int age{get;}  private int age;
    public int Age
    {
      get { return age; }  set { age = value; } 
    }
    变量是 私有的 别人不可以使用    属性是公共的,  这样可以保护私有变量    而且可以在set 或者 get的时候  控制赋值内容。
    比如 age只能为 0-20之间    这时候set 里可以继续学逻辑要求。   或者 取的时候 你要返回加密后的东西 就要在get里写算法
    public int age{get;set;}   是下面的简写。 没有任何逻辑 只是赋值 取值。 
    个人理解 。看你喜欢用那种
      

  7.   


    最简单的还是 public string Name {get;set;} 这种public string Name {get;private set;}
      

  8.   

    楼歪了啊, 问题的关键是要实现拒绝null值的情况下尽可能简写