M. Qasim M. Qasim - 4 months ago 22
C# Question

Creating new object without passing parameter

I'm reading Data Structures and Algorithms Using C# (Michael McMillan).

In chapter 3 Page 43, I found code snippet that take parameter in constructor but creating object without passing anything.

enter image description here

Is that possible?


First of all yes it is possible, but in this context I don't think so. You can create multiple constructors which would allow this situation like:

public CArray(){
   this(5); }
public CArray(int size){
   arr=new int[size] ......

To describe: The first Constructor is more or less the "default constructor" which will enter the other Constructor with the parameter size. You can also add some more Constructors.