Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为属性设置默认值_C#_.net - Fatal编程技术网

C# 为属性设置默认值

C# 为属性设置默认值,c#,.net,C#,.net,是否可以在没有属性主体的情况下设置默认值?最好有注释 [SetTheDefaultValueTo(true)] public bool IsTrue { get; set; } [SetTheDefaultValueTo(false)] public bool IsFalse { get; set; } public void Something() { var isTrue = this.IsTrue; var isFalse = this.IsFalse; } 在类构造

是否可以在没有属性主体的情况下设置默认值?最好有注释

[SetTheDefaultValueTo(true)]
public bool IsTrue { get; set; }

[SetTheDefaultValueTo(false)]
public bool IsFalse { get; set; }

public void Something()
{
    var isTrue = this.IsTrue;
    var isFalse = this.IsFalse;
}

在类构造函数中指定默认属性值

class MyClass
{
    public MyClass()
    {
        IsTrue = true;
        IsFalse = false;
    }

    public bool IsTrue { get; set; }

    public bool IsFalse { get; set; }

    [...]

    public void Something()
    {
        var isTrue = this.IsTrue;
        var isFalse = this.IsFalse;
    }
}

不,没有内置的方法来设置带有元数据的属性的值。您可以使用某种类型的工厂来构建具有反射的类的实例,然后设置默认值。但简而言之,您需要使用构造函数(或字段设置器,它们被提升到构造函数)来设置默认值

如果您的构造函数有几个重载,您可能需要查看

使用C#6+,您可以执行以下操作

public string MyValue { get; set; } = "My Default";
// this code won't compile!
public string MyValue {
    private string _myValue;
    get { return _myValue ?? "My Default"; }
    set { _myValue = value; }
}
哦,它变得更有趣,因为人们甚至要求这样的东西

public string MyValue { get; set; } = "My Default";
// this code won't compile!
public string MyValue {
    private string _myValue;
    get { return _myValue ?? "My Default"; }
    set { _myValue = value; }
}

。。。优点是您可以控制字段的范围,使其仅在属性代码中可访问,这样您就不必担心类中的任何其他内容在不使用getter/setter的情况下处理状态。

在这个非常具体的示例中,您可以:

public bool IsFalse { get; set; }
public bool IsTrue
{
    get { return !IsFalse; }
    set { IsFalse = !value; }
}

public void Something()
{
    var isTrue = this.IsTrue;
    var isFalse = this.IsFalse;
}
但是,一般来说,没有旧线程。 看起来微软已经听说了,这个功能在.Net Framework 4.6+(C#6+)中提供 你可以像这样使用它

public string MyValue { get; set; } = "My Default";
如果您使用的是C#5及更早版本,则必须在构造函数中执行

class MyClass
{
    public MyClass()
    {
        IsTrue = true;
        IsFalse = false;
    }

    public bool IsTrue { get; set; }

    public bool IsFalse { get; set; }

    [...]

    public void Something()
    {
        var isTrue = this.IsTrue;
        var isFalse = this.IsFalse;
    }
}
但自C#6.0以来,就包括了自动属性初始值设定项的功能,语法如下:

public int myage { get; set; } = 33;

这看起来更像是他的问题而不是答案。有没有地方可以让我投票支持这项功能或类似的功能?你可以在connect.microsoft.com.eek上找到一些东西。。。我刚在CodeProject上找到这个。构造对象后,它使用反射设置默认值。我想我在这个网站的某个地方看到了另一个条目,链接到connect.microsoft.com上的报告。。。我再也找不到了。如果有人找到了这个链接,我会很乐意更新我的答案,把它包括进去。非常令人困惑——我认为你的第二个代码示例是作为一个解决方案提供的,而不是作为一个不起作用的示例。为了清晰起见,我编辑了这篇文章。