0

Suppose there is a class that has n properties. While creating its objects I have to create by using following syntax

Way 1

ClassName c = new ClassName();

and assing values like

c.p1 = someValue1;
c.p2 = someValue2;
c.p3 = someValue3;
c.p4 = someValue4;

or

Way 2

ClassName c = new ClassName(someValue1,someValue2,someValue3,someValue4);

if class has a parametrized constructor like

private CalssName(DataType1 prop1,DataType2 prop2,DataType3 prop3,DataType4 prop4)
{
     p1 = prop1;
     p2 = prop2;
     p3 = prop3;
     p4 = prop4;
}

Which ways will suit for which siuation?

Imad
  • 7,126
  • 12
  • 55
  • 112

5 Answers5

2

Using either depends on the requirement.

If you want your class object to have some state (properties assigned) at the time of instantiation then use parametrized constructor. If your class doesn't have a default constructor (no parameters) then there will be no way for the user to instantiate a class without any state.

By default each class would have a default constructor (without any parameter), but once you define a parametrized constructor, there will be no default constructor unless you provide one explicitly.

Imagine if you have class like:

public class ClassName
{
    public int ID { get; set; }
    public string Name { get; set; }
    public ClassName(int id, string name)
    {
        ID = id;
        Name = name;
    }
}

Since you have provided a parametrized constructor, User can't instantiate an object without passing ID and Name values. (ignoring reflection)

ClassName obj = new ClassName(); //This will error out

This is useful in scenarios where it is compulsory for an object to have some values at the time of instantiation.

Consider the DirectoryInfo class provided by the .Net framework, you can't instantiate an object of DirectoryInfo without parameter

DirectoryInfo dirInfo = new DirectoryInfo();//This will error out

Since DirectoryInfo requires the object to have a path pointing to the directory, It would be of no use without a path, therefore it is provided with only a parametrized constructor

DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Somefolder");
Habib
  • 219,104
  • 29
  • 407
  • 436
2

I agree with Habib and adding a little more info, I find it easier for example when adding to a list:

   List<ClassName> myList = new List<ClassName>();
   myList.Add(new ClassName(someValue1,someValue2,someValue3,someValue4)); 
   myList.Add(new ClassName(someValue5,someValue6,someValue7,someValue8)); 

Instead of having to create the objects and assign them values, adding more lines of code. In both approaches the objects are created into the heap and the garbage collector will call the object.finalize when the objects are no longer necessary.

Turay Melo
  • 114
  • 5
1

Here's a great overview: http://msdn.microsoft.com/en-us/library/ms229060%28v=vs.110%29.aspx

General commentary:

  • Unless there is a reason to do otherwise, you should consider exposing a default constructor
  • You should also expose constructors that are object oriented, ie a car class should be constructable with an engine and transmission but not with a driver, as the driver is not really part of the car class. This is my interpretation of what is meant by main properties in "DO use constructor parameters as shortcuts for setting main properties"
  • You should not expose a default constructor if you want the object to be immutable (with all readonly properties)
VoteCoffee
  • 4,692
  • 1
  • 41
  • 44
0

Are those properties required for the class to function normally? Then injecting them via the constructor as in Way 2 may be best. Additionally, if you weren't aware, you're actually asking about 2 different forms of dependency injection. Way 1 is property injection, and Way 2 is constructor injection. This may aid you in seeking further resources on which way is best.

Community
  • 1
  • 1
EF0
  • 2,940
  • 4
  • 17
  • 23
0

If it does make a sense to have an object of your class without any initial values, then you may use the default constructor.

But if you find that in your program you frequently have code that looks like this:

ClassName c = new ClassName();
c.p1 = someValue1;

then this is a strong hint that you do require a constructor with parameters. The reason is that you want to avoid to leave to the programmer the possibility of forgetting initializing the object of your class (this is practically the c.p1 = someValue1 code that is doing), and enforce the initialization through the constructor. This way a lot of bugs can be avoided.

As for when you need a parameterless constructor. Sometimes it is necessary because frameworks that you may use might require one, or it might be the case where no initialization using parameters is required.

On a similar note you may also find it that sometimes you require a lot of parameters to properly initialize an object, or you might require many different constructors because there might be different ways to initialize an object (e.g.using a string, or a number). In this case you might want to have a look at the builder pattern.Builder pattern

user2465039
  • 894
  • 1
  • 11
  • 28