【问题标题】:Automatic Property Values and Defaults [duplicate]自动属性值和默认值 [重复]
【发布时间】:2011-08-15 10:30:58
【问题描述】:

可能重复:
How do you give a C# Auto-Property a default value?

我在这样的类中有一个属性

public String fontWeight { get; set; }

我希望它默认为"Normal"

有没有办法以“自动”风格而不是以下方式做到这一点

public String fontWeight {
    get { return fontWeight; } 
    set { if (value!=null) { fontWeight = value; } else { fontWeight = "Normal"; } }
}

【问题讨论】:

  • 谁被默认分配正常?

标签: c# default-value


【解决方案1】:

是的,你可以。

如果您正在寻找类似的东西:

[DefaultValue("Normal")]
public String FontWeight
{
    get;
    set;
}

在 Google 上搜索“使用 .NET 进行面向方面编程”

..如果这对你来说太过分了:

private string fontWeight;
public String FontWeight {
    get
    {
        return fontWeight ?? "Normal";
    }
    set {fontWeight = value;} 
}

【讨论】:

  • 所以这是你能得到的最自动的吗?酷
  • DefaultValueAttribute 只被一些视觉设计师认可。我认为 OP 希望在构造或访问任何调用者时初始化属性值。
【解决方案2】:

不,自动属性只是一个普通的 getter 和/或 setter 以及一个支持变量。如果你想在属性中放置任何类型的逻辑,你必须使用常规的属性语法。

不过,您可以使用 ?? 运算符使其更短:

private string _fontWeight;

public String FontWeight {
  get { return _fontWeight; } 
  set { _fontWeight = value ?? "Normal"; }
}

注意setter不是用来初始化属性的,所以如果没有在构造函数中设置值(或者在变量声明中赋值),默认值还是null。您可以在 getter 中进行检查以解决此问题:

private string _fontWeight;

public String FontWeight {
  get { return _fontWeight ?? "Normal"; } 
  set { _fontWeight = value; }
}

【讨论】:

  • 不错的一个。继续忘记 ??.
  • 我知道你在哪里,如果根本没有设置,设备将不会运行并将任何东西设置为正常。需要在 get 上检查 null :)
  • 我接受这个,但我确实需要像@Jethro 所说的支持字段
  • @Joseph Le Brech:是的,常规属性没有自动成员变量,我假设您只是在示例中忽略了它。我将它添加到代码示例中,以便任何看到它的人都清楚。 :)
  • 看起来不错,谢谢。
【解决方案3】:

您将需要使用支持字段。

private string fontWeight;
public String FontWeight 
{ 
    get { String.IsNullOrEmpty(fontWeight) ? "Normal" : fontWeight;}
    set {fontWeight = String.IsNullOrEmpty(value) ? "Normal" : value;} 
}

【讨论】:

    【解决方案4】:

    您需要使用支持字段并将其初始化为默认值

    private String fontWeight = "Normal";
    public String FontWeight
    {
        get { return fontWeight; }
        set { fontWeight = value; }
    }
    

    或者,保留 auto 属性并在构造函数中调用 setter

    public constructor()
    {
        FontWeight = "Normal";
    }
    
    public String FontWeight { get; set; }
    

    【讨论】:

      【解决方案5】:

      一种方法是使用 this answer 中详述的 PostSharp 来解决类似问题。

      【讨论】:

        【解决方案6】:

        您可以使用DefaultValue 属性:

            [DefaultValue("Normal")]
            public string FontWeight { get; set; }
        

        虽然它指出

        DefaultValueAttribute 不会导致成员使用属性值自动初始化。您必须在代码中设置初始值。

        因此您可以将它与构造函数中的初始化或通过支持字段和默认处理结合使用。

        【讨论】:

          【解决方案7】:

          你需要一个像这样的变量:

          string fontWeight;
          
          public string FontWeight
          { 
              get
              {
                  if (string.IsNullOrEmpty(fontWeight))
                      fontWeight = "Normal";
          
                  return fontWeight;
              }
              set { fontWeight = value; } 
          }
          

          或使用Constructer 设置初始值:

          class FontClass
          {
              public string FontWeight { get; set; }
          
              public FontClass()
              {
                  FontWeight = "Normal";
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-03-21
            • 2011-02-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-18
            相关资源
            最近更新 更多