6

I have a function in a controller, and I receive the information for a form. I have this code:

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

I tried to convert this to a class like

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

and receive them as a object

 public Actionresult functionOne(bar b)

Also I tried to put the defaultvalue in 'c' but is not working, I tried this:

public class bar
{
    public string a {get;set;}
    public string b {get;set;}
    [System.ComponentModel.DefaultValue("foo")]
    public string c {get;set;}
}

Nothing happened with that, I receive it as null

also I tried

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
        }
    }
}

What should I do to write this default value?

peterh
  • 11,875
  • 18
  • 85
  • 108
TiGreX
  • 1,602
  • 3
  • 26
  • 41
  • 1
    Maybe put a default value in the constructor ? – Dave3of5 Jul 31 '15 at 14:41
  • 3
    The `[DefaultValue]` attribute indeed does not do anything; it is meant for designers to be able to determine with what value you initialize a property. – C.Evenhuis Jul 31 '15 at 14:47
  • Hi. A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code. DefaultValueAttribute usefull for reflection when read any of member in reflection is null use this attribute to find or set default value. – Moslem Shahsavan Jul 31 '15 at 15:05

4 Answers4

37

If you are using C# 6 you can do this:

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

Otherwise you can do this:

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;
        }
    }
}
Anish Patel
  • 4,332
  • 1
  • 30
  • 45
15

1) Use the object's constructor:

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

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

2) Utilize the new auto property default value. Note that this is for C# 6+:

public class bar
{

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

3) Use a backing field

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) Use the Null Coalescing Operator check

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;}
        }
    }
JasonWilczak
  • 2,303
  • 2
  • 21
  • 37
4

Have you tried a default constructor which sets the value of C?

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

    public Bar()
    {
        C = "foo";
    }
}
Alex Neves
  • 616
  • 1
  • 7
  • 16
1

You can assign Default value in constructor

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

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

Whenever bar object is created, constructor will be called and c will be initialized with "foo".

Later when some other method is called c will be updated with its value like

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;
    }
}
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208