初始化 C# 自动属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/169220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:29:35  来源:igfitidea点击:

Initializing C# auto-properties

c#initializationautomatic-properties

提问by dlamblin

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?

将 Bar 转换为自动属性看起来既方便又简洁,但是如何在不添加构造函数并将初始化放在那里的情况下保留初始化?

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

采纳答案by Jon Skeet

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

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

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

You can alsowrite 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, andallowing for read-only automatically implemented properties to be initialized in a constructor body.

这直到并包括 C# 5 才发生,但正在计划用于 C# 6 - 允许在声明点初始化,允许在构造函数体中初始化只读自动实现的属性。

回答by Matthew Scharley

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.

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

回答by Aaron Powell

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.

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

回答by romanoza

This will be possible in C# 6.0:

这在 C# 6.0 中是可能的:

public int Y { get; } = 2;