<small id='RgWm1'></small><noframes id='RgWm1'>

<legend id='RgWm1'><style id='RgWm1'><dir id='RgWm1'><q id='RgWm1'></q></dir></style></legend>

      <tfoot id='RgWm1'></tfoot>
      1. <i id='RgWm1'><tr id='RgWm1'><dt id='RgWm1'><q id='RgWm1'><span id='RgWm1'><b id='RgWm1'><form id='RgWm1'><ins id='RgWm1'></ins><ul id='RgWm1'></ul><sub id='RgWm1'></sub></form><legend id='RgWm1'></legend><bdo id='RgWm1'><pre id='RgWm1'><center id='RgWm1'></center></pre></bdo></b><th id='RgWm1'></th></span></q></dt></tr></i><div id='RgWm1'><tfoot id='RgWm1'></tfoot><dl id='RgWm1'><fieldset id='RgWm1'></fieldset></dl></div>
        • <bdo id='RgWm1'></bdo><ul id='RgWm1'></ul>

        VS2017 新的 getter/setter 语法:如何在 setter 中写多行?/

        时间:2023-05-22

        <small id='ka2MX'></small><noframes id='ka2MX'>

        <legend id='ka2MX'><style id='ka2MX'><dir id='ka2MX'><q id='ka2MX'></q></dir></style></legend>
            <tbody id='ka2MX'></tbody>

            • <tfoot id='ka2MX'></tfoot>
              <i id='ka2MX'><tr id='ka2MX'><dt id='ka2MX'><q id='ka2MX'><span id='ka2MX'><b id='ka2MX'><form id='ka2MX'><ins id='ka2MX'></ins><ul id='ka2MX'></ul><sub id='ka2MX'></sub></form><legend id='ka2MX'></legend><bdo id='ka2MX'><pre id='ka2MX'><center id='ka2MX'></center></pre></bdo></b><th id='ka2MX'></th></span></q></dt></tr></i><div id='ka2MX'><tfoot id='ka2MX'></tfoot><dl id='ka2MX'><fieldset id='ka2MX'></fieldset></dl></div>

                • <bdo id='ka2MX'></bdo><ul id='ka2MX'></ul>
                  本文介绍了VS2017 新的 getter/setter 语法:如何在 setter 中写多行?/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 .NET Framework 4.5.2,VS2017.VS2017 有了新的 getter 和 setter 语法.现在带有 getter setter 的属性如下所示:

                  I am using .NET Framework 4.5.2, VS2017. VS2017 has got new syntax for getter and setter. Now the property with getter setter looks like below:

                  public string Name { get => _name; set => _name = value; }
                  

                  我必须写下下面的属性.如何使用 lambda 表达式 set=> 编写设置器?

                  I have to write the below property. How can I write the setter with lambda expression set=> ?

                  public int EmployeeNumber
                      {
                          get => _employeeNumber;
                          set { _employeeNumber = value; OnPropertyChanged("EmployeeNumber");}
                      }
                  

                  比如这样的:

                  public int EmployeeNumber
                  {
                     get => _employeeNumber;
                     set =>{ _employeeNumber = value;OnPropertyChanged("EmployeeNumber"); }
                  }
                  

                  对于上述设置器,我得到 3 个错误:

                  For the above setter, I get 3 errors:

                  CS1525: Invalid expression term {
                  CS1002: ; expected
                  CS1014: A get or set accessor expected
                  

                  推荐答案

                  好的,让我们再看一遍.你想写

                  Ok, lets go over this again. You want to write

                  public int EmployeeNumber 
                  {
                      set 
                      { 
                          _employeeNumber = value;
                          OnPropertyChanged("EmployeeNumber");
                      } 
                  }
                  

                  像这样:

                  public int EmployeeNumber 
                  {
                      set => 
                      { 
                          _employeeNumber = value;
                          OnPropertyChanged("EmployeeNumber");
                      } 
                  }
                  

                  问题是为什么?关于表达式体函数成员的全部意义在于使事情更简洁易读,避免使用花括号、返回关键字等:

                  The question is why? The whole point about expression bodied function members is to make things more concise and readable avoiding curly braces, return keywords, etc.:

                  public int Foo => foo
                  

                  而不是,

                  public int Foo { return foo; }
                  

                  您尝试做的事情并没有使其更具可读性,并添加了两个无用的额外标记.这似乎是一笔糟糕的交易.

                  What you are attempting to do doesn't make it more readable and adds two useless extra tokens. That seems like an awful bargain.

                  作为一般规则,您不应该使用(或不能使用)=> 语法,而右侧的代码:

                  As a general rule, you shouldn't use (or can't use) the => syntax when the code on the right side:

                  1. 不返回任何内容(抛出异常是异常,双关语)
                  2. 由多个表达式组成.
                  3. 是因为它产生的副作用.

                  当然,第 3 条规则是我一个人的事,我不知道有任何关于这个问题的编码风格建议,但我倾向于避免使用这种语法,除非我处理的是没有副作用的方法.

                  Of course rule n3 is mine alone, I'm not aware of any coding style recommendations on this matter but I tend to avoid this syntax unless I'm dealing with no side effects producing methods.

                  这篇关于VS2017 新的 getter/setter 语法:如何在 setter 中写多行?/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:无法在 Visual Studio 2017 RC 中启动 IIS Express 下一篇:使用预编译的 Azure 函数库生成错误

                  相关文章

                  <small id='ukd7g'></small><noframes id='ukd7g'>

                      <bdo id='ukd7g'></bdo><ul id='ukd7g'></ul>

                      <i id='ukd7g'><tr id='ukd7g'><dt id='ukd7g'><q id='ukd7g'><span id='ukd7g'><b id='ukd7g'><form id='ukd7g'><ins id='ukd7g'></ins><ul id='ukd7g'></ul><sub id='ukd7g'></sub></form><legend id='ukd7g'></legend><bdo id='ukd7g'><pre id='ukd7g'><center id='ukd7g'></center></pre></bdo></b><th id='ukd7g'></th></span></q></dt></tr></i><div id='ukd7g'><tfoot id='ukd7g'></tfoot><dl id='ukd7g'><fieldset id='ukd7g'></fieldset></dl></div>

                    1. <tfoot id='ukd7g'></tfoot>
                    2. <legend id='ukd7g'><style id='ukd7g'><dir id='ukd7g'><q id='ukd7g'></q></dir></style></legend>