netwer
netwer

Reputation: 737

What way is better for init class property with default value when the create the instance of class

I have the following simple class:

public class SimpleClass{
    public int SimpleProperty {get; set;}

    public SimpleClass(){

    }
}

I need to init the property SimpleProperty by default value when I create the instance of SimpleClass. What the bast way doing this?

  1. Create the parameter in constructor and pass the default value when the create the instance of Simple Class:

    public class SimpleClass{
        public int SimpleProperty {get; set;}
    
        public SimpleClass(int simpleProperty){
            SimpleProperty = simpleProperty;
        }
    }
    
  2. or init the property in default constructor:

    public class SimpleClass{
    
        public int SimpleProperty {get; set;}
    
        public SimpleClass(){
            SimpleProperty = 0; //the example of default value
        }
    }
    

Upvotes: 3

Views: 105

Answers (4)

vgru
vgru

Reputation: 51214

Consider also making the class immutable:

public class SimpleClass
{
    // getter only, no setter
    public int SimpleProperty { get; }

    public SimpleClass(int simpleProperty)
    {
        // allowed in C#6+
        SimpleProperty = simpleProperty;
    }
}

If your properties are writable (i.e. the class is mutable), no constructor overload will really prevent you from shooting yourself in the foot.

Upvotes: 1

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

These are two different approaches. If you have a default-value you can usually omit setting a value for this property. Thus you should initialize the default-value within the constructor.

var a new SimpleClass();

Now a has the default-value you provided within the constructor.

Having a signature that expects an argument on the other hand forces the user to provide a value. This means there is no default-value at all and you can´t omit the value.

var a = SimpleClass();  // this won´t compile

By the way you forgot about the third approach: optional arguments:

SimpleClass(int value = -1)
{
    SimpleProperty = value;
}

Now you can use it as my first approach

var a = new SomeSimpleClass()

or

var a = new SimpleClass(1)

From C#6 onwards things are a bit easier as you can give your property a default-value:

int SimpleProperty { get; set; } = -1;

Anyway if your default-value is just zero you can omit it completely as zero is the default-value anyway for int.

Upvotes: 1

Fabjan
Fabjan

Reputation: 13676

Well, it very depends on what project you're working with.

If it is big enterprise project, and your property is of some object type (which make your class to depend on some other object) the first option would be much more preferable as it allows to use IoC containers, and frameworks and it also simplifies the way of how unit tests can be written as it's simple to pass a mock value inside of ctor and not so simple, if ctor is not accepting arguments.

With simple types, like int or in a simple pet project however it doesn't really matter and is up to you to decide. Don't forget, that you can use optional parameters for ctor :

public SimpleClass(int simpleProperty = 15)
{
    SimpleProperty = simpleProperty;
}

Upvotes: 2

ThePerplexedOne
ThePerplexedOne

Reputation: 2950

You can have multiple constructors. So you can actually use both.

So, if the user doesn't provide a value, then set it to 0 by default.

Upvotes: 0

Related Questions