<small id='7uMMj'></small><noframes id='7uMMj'>

      <bdo id='7uMMj'></bdo><ul id='7uMMj'></ul>

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

  2. <tfoot id='7uMMj'></tfoot>

      初始化 C# 自动属性

      Initializing C# auto-properties(初始化 C# 自动属性)
      • <bdo id='SVM5K'></bdo><ul id='SVM5K'></ul>
        <legend id='SVM5K'><style id='SVM5K'><dir id='SVM5K'><q id='SVM5K'></q></dir></style></legend>
          • <tfoot id='SVM5K'></tfoot>

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

                <tbody id='SVM5K'></tbody>
                本文介绍了初始化 C# 自动属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!



                public class foo {
                  private string mBar = "bar";
                  public string Bar {
                    get { return mBar; }
                    set { mBar = value; }
                  //... other methods, no constructor ...

                将 Bar 转换为 auto-property 似乎方便简洁,但是如何在不添加构造函数并将初始化放入其中的情况下保留初始化?

                Converting Bar to an auto-property seems convenient and concise, but how can I retain the initialization without adding a constructor and putting the initialization in there?

                public class foo2theRevengeOfFoo {
                  //private string mBar = "bar";
                  public string Bar { get; set; }
                  //... other methods, no constructor ...
                  //behavior has changed.


                You could see that adding a constructor isn't inline with the effort savings I'm supposed to be getting from auto-properties.


                Something like this would make more sense to me:

                public string Bar { get; set; } = "bar";


                更新 - 下面的答案是在 C# 6 出现之前编写的.在 C# 6 中,您可以编写:

                Update - the answer below was written before C# 6 came along. In C# 6 you can write:

                public class Foo
                    public string Bar { get; set; } = "bar";


                You can also write read-only automatically-implemented properties, which are only writable in the constructor (but can also be given a default initial value):

                public class Foo
                    public string Bar { get; }
                    public Foo(string bar)
                        Bar = bar;


                It's unfortunate that there's no way of doing this right now. You have to set the value in the constructor. (Using constructor chaining can help to avoid duplication.)


                Automatically implemented properties are handy right now, but could certainly be nicer. I don't find myself wanting this sort of initialization as often as a read-only automatically implemented property which could only be set in the constructor and would be backed by a read-only field.

                这在 C# 5 之前(包括 C# 5)并没有发生,但正计划在 C# 6 中进行 - 无论是在允许在声明点初始化,都允许只读自动实现要在构造函数主体中初始化的属性.

                This hasn't happened up until and including C# 5, but is being planned for C# 6 - both in terms of allowing initialization at the point of declaration, and allowing for read-only automatically implemented properties to be initialized in a constructor body.

                这篇关于初始化 C# 自动属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
              1. <tfoot id='aLthJ'></tfoot>

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

                        1. <small id='aLthJ'></small><noframes id='aLthJ'>