默认值 C# 类

6

我在控制器中有一个函数,接收表单的信息。我的代码如下:

public Actionresult functionOne(string a, string b, string c = "foo" )

我尝试将这个转换成一个类,像这样:

public class bar
{
    public string a {get;set;}
    public string b {get;set;}
    public string c {get;set;}
}

并将它们作为对象接收
 public Actionresult functionOne(bar b)

此外,我试图将默认值放在“c”中,但它不起作用,我尝试了这个:
public class bar
{
    public string a {get;set;}
    public string b {get;set;}
    [System.ComponentModel.DefaultValue("foo")]
    public string c {get;set;}
}

这个没有发生任何变化,我收到的是null

我也尝试了

public class bar
{
    public string a {get;set;}
    public string b {get;set;}
    public string c 
    {
        get
        {
            return  c;
        }
        set
        {
            c="foo"; //I also tried with value
        }
    }
}

我该怎么样才能写入这个默认值?

1
在构造函数中可能要设置默认值吗? - Dave3of5
3
[DefaultValue] 属性确实没有任何实际作用;它的目的是让设计者能够确定你初始化一个属性的数值。 - C.Evenhuis
你好。DefaultValueAttribute不会自动使用属性值初始化成员。您必须在代码中设置初始值。当使用反射读取任何成员为空时,DefaultValueAttribute对于查找或设置默认值非常有用。 - Moslem Shahsavan
4个回答

37

如果你正在使用C# 6,你可以这样做:

public class Bar {
    public string a { get; set; }
    public string b { get; set; }
    public string c { get; set; } = "foo";
}
否则,您可以这样做:
public class Bar {
    public string a { get; set; }
    public string b { get; set; }
    private string _c = "foo";
    public string c
    {
        get
        {
            return _c;
        }
        set
        {
            _c = value;
        }
    }
}

15

1)使用对象的构造函数:

public class bar
{
    public bar() 
    {
       c = "foo";
    }

    public string a {get;set;}
    public string b {get;set;}
    public string c {get;set;}
}

2)使用新的自动属性默认值。请注意,这适用于C#6及更高版本:(参见链接).

public class bar
{

    public string a {get;set;}
    public string b {get;set;}
    public string c {get;set;} = "foo";
}

3) 使用后备字段

public class bar
{
    var _c = "foo";
    public string a {get;set;}
    public string b {get;set;}
    public string c {
       get {return _c;} 
       set {_c = value;}
    }
}

4) 使用空值合并运算符检查

public class bar
    {
        string _c = null;
        public string a {get;set;}
        public string b {get;set;}
        public string c {
           get {return _c ?? "foo";} 
           set {_c = value;}
        }
    }

4

你尝试过使用设置C值的默认构造函数吗?

public class Bar
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }

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

1

你可以在构造函数中分配默认值

public class bar
{
    public bar()
    {
        this.c = "foo";
    }

    public string a {get;set;}
    public string b {get;set;}
    public string c {get;set;}
}

每当创建bar对象时,构造函数将被调用,并且c将被初始化为"foo"。
稍后,当调用其他方法时,c将被更新为其值,例如:
public class bar
{
    public bar()
    {
        this.c = "foo";
    }

    public string a {get;set;}
    public string b {get;set;}
    public string c {get;set;}

    public void UpdadateValueofC(string updatedvalueofc)
    {
        this.c = updatedvalueofc;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接