初始化 C# 自动属性

发布于 2024-07-05 23:10:49 字数 634 浏览 6 评论 0原文

我习惯于编写这样的类:

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

Converting Bar to an auto-property 看起来方便又简洁,但是如何在不添加构造函数并将初始化放在那里的情况下保留初始化呢?

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

您可以看到,添加构造函数与我应该从自动属性中节省的精力并不相符。

这样的事情对我来说更有意义:

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

I'm used to writing classes like this:

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

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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走野 2024-07-12 23:10:49

您可以通过类的构造函数来完成此操作:

public class foo {
  public foo(){
    Bar = "bar";
  }
  public string Bar {get;set;}
}

如果您有另一个构造函数(即,带有参数的构造函数)或一堆构造函数,您始终可以使用此构造函数(称为构造函数链接):

public class foo {
  private foo(){
    Bar = "bar";
    Baz = "baz";
  }
  public foo(int something) : this(){
    //do specialized initialization here
    Baz = string.Format("{0}Baz", something);
  }
  public string Bar {get; set;}
  public string Baz {get; set;}
}

如果您始终将调用链接到默认构造函数,您可以在那里设置所有默认属性初始化。 链接时,将在调用构造函数之前调用链接的构造函数,以便更专业的构造函数能够根据需要设置不同的默认值。

You can do it via the constructor of your class:

public class foo {
  public foo(){
    Bar = "bar";
  }
  public string Bar {get;set;}
}

If you've got another constructor (ie, one that takes paramters) or a bunch of constructors you can always have this (called constructor chaining):

public class foo {
  private foo(){
    Bar = "bar";
    Baz = "baz";
  }
  public foo(int something) : this(){
    //do specialized initialization here
    Baz = string.Format("{0}Baz", something);
  }
  public string Bar {get; set;}
  public string Baz {get; set;}
}

If you always chain a call to the default constructor you can have all default property initialization set there. When chaining, the chained constructor will be called before the calling constructor so that your more specialized constructors will be able to set different defaults as applicable.

纵性 2024-07-12 23:10:49

这在 C# 6.0 中将成为可能:

public int Y { get; } = 2;

This will be possible in C# 6.0:

public int Y { get; } = 2;
第七度阳光i 2024-07-12 23:10:49

在默认构造函数中(当然还有任何非默认构造函数,如果您也有的话):

public foo() {
    Bar = "bar";
}

我相信这并不比您的原始代码性能差,因为无论如何这都是幕后发生的事情。

In the default constructor (and any non-default ones if you have any too of course):

public foo() {
    Bar = "bar";
}

This is no less performant that your original code I believe, since this is what happens behind the scenes anyway.

羞稚 2024-07-12 23:10:49

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

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

您还可以编写只读的自动实现的属性,这些属性只能在构造函数中写入(但也可以指定默认初始值):

public class Foo
{
    public string Bar { get; }

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

不幸的是,没有现在这样做的方法。 您必须在构造函数中设置该值。 (使用构造函数链可以帮助避免重复。)

自动实现的属性现在很方便,但肯定会更好。 我发现自己并不像只读自动实现的属性那样经常需要这种初始化,该属性只能在构造函数中设置,并且由只读字段支持。

这种情况直到 C# 5(包括 C# 5)才发生,但正在计划在 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文