1

I have a property

public int active { get; set; }

That in my db has a default value of 1. I would like to have this property default to 1 if not otherwise specified

public partial class test
{
    public int Id { get; set; }
    public string test1 { get; set; }
    public int active { get; set; }
}

I saw that in c# 6 you can do

public int active { get; set; } = 1

But I am not using c# 6 :(. Thanks for the advice. (Very, very new to c# / OOP)

pbordeaux
  • 315
  • 1
  • 4
  • 17
  • 1
    But I am not using c# ? did you mean to say: But I am not using c# 6.0 ? – Joe Feb 17 '16 at 16:34
  • 1
    In any case, use the constructor for initialization if you are pre-6.0. – Glorin Oakenfoot Feb 17 '16 at 16:34
  • 2
    So add a [default constructor](https://msdn.microsoft.com/en-us/library/ms173115.aspx) and initialize it there. As an aside, do you really want to be using `partial` classes? If you need a partial class, it's because some of the class definition was automatically generated. Is this the case? If not, using `partial` is an abuse and probably indicative of a class that has taken on too many responsibilities (and has grown too large). For hand written code, partially defining a class only obscures the intent of your code. – spender Feb 17 '16 at 16:35
  • Edited, not using c# 6, sorry – pbordeaux Feb 17 '16 at 16:35

4 Answers4

5

Just set it in the constructor:

public partial class Test
{
    public int Id { get; set; }
    public string Test1 { get; set; }
    public int Active { get; set; }

    public Test()
    {
        Active = 1;
    }
}

I think that's simpler than avoiding the automatically-implemented property just for the sake of defaulting...

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
1

Initialize it in the constructor:

public partial class Test
{
    public int Active { get; set; }

    public Test()
    {
        Active = 1;
    }
}
Alex Isayenko
  • 649
  • 8
  • 7
0

The default way to do this in pre-C#6 is quite a lot more verbose, but it's only syntax - this is equivalent:

public class Foo
{
    private int _bar = 1;
    public int Bar
    {
        get { return _bar; }
        set { _bar = value; }
    }
}
Tomas Aschan
  • 53,075
  • 51
  • 214
  • 362
0
// option 1:  private member
    public partial class test
    {
        private int _active = 1;
        public int Id { get; set; }
        public string test1 { get; set; }
        public int active 
        { 
            get {return _active; } 
            set {_active = value; } 
        }
    }

// option 2:  initialize in constructor

    public partial class test
    {
        public test()
        {
            active = 1;
        }
        public int Id { get; set; }
        public string test1 { get; set; }
        public int active { get; set; }
    }
Joe
  • 923
  • 1
  • 10
  • 17
  • Awesome, now I see the utility of get set, literally started c# this a few days ago – pbordeaux Feb 17 '16 at 16:40
  • It gets even better. Properties are very powerfull. You can do a lot more than just a simple `get; set;`, and even more than just a simple assignment, – Joe Feb 17 '16 at 16:42