2

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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
M. Qasim
  • 53
  • 1
  • 8
  • 1
    Usually, it is a good idea to post source code as text, not as image. – Uwe Keim Sep 08 '16 at 06:55
  • To answer your question: Probably there are several overloads of the constructor. E.g. one without parameters like `public CArray()` and one with `public CArray(int size)` and maybe others, too. – Uwe Keim Sep 08 '16 at 06:55
  • Do you try optional parameter with default value? – Imran Sh Sep 08 '16 at 06:56
  • 1
    @UweKeim Got it, I'm Posting First Time. – M. Qasim Sep 08 '16 at 06:56
  • It could be a typo – Tacsiazuma Sep 08 '16 at 06:57
  • @UweKeim Image contains complete code. No Overload Method defined... – M. Qasim Sep 08 '16 at 06:58
  • @ImranShams i'm not trying, I just saw this snippet in Course book & get confused. – M. Qasim Sep 08 '16 at 06:59
  • I've found a PDF version through Google on [this URL](ftp://ftp.rau.am/_MAIN_/Books/Books%20CSharp%20New/Cambridge.University.Press.Data.Structures.and.Algorithms.Using.CSharp.Mar.2007.pdf). I've searched through all the document and found no other c'tor. Since your code is screenshot is missing a closing `}` I'm pretty sure that the whole class is _not_ printed in the book. The source code is available [here](http://users.cis.fiu.edu/~weiss/cs/). There is no `CArray` at all in the full source code. – Uwe Keim Sep 08 '16 at 07:03

5 Answers5

3

There could a few possibilities:

  • A typo
  • There is another overload

    public CArray(int size)
    {
        // Implementation.
    }
    public CArray()
    {
        // Other implementation.
    }
    
  • There could be a default value

    public CArray(int size = 8)
    {
        // Implementation.
    }
    

Even though the code provided in the question will not compile 'as is' (without one of the 3 explanations - there is a way to create an uninitialized instance with no constructor call at all using System.Runtime.Serialization.FormatterServices:

class SomeType
{
    int mVariable;

    public SomeType(int size)
    {
        mVariable = size;
    }
}

static void Main(string[] args)
{
    Type someType = typeof(SomeType);
    SomeType instance = (SomeType)FormatterServices.GetUninitializedObject(someType);
    // instance.mVariable = 0;
}

Keep in mind that this method is slow and unsafe since you may use the class in a way its developer didn't mean you to and manually override the constructor, hence it should be used for serialization purposes only and not for general initialization.

Tamir Vered
  • 10,187
  • 5
  • 45
  • 57
2

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]; 
   //other logic's must be here
}

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.

Imran Sh
  • 1,623
  • 4
  • 27
  • 50
Markus G.
  • 1,620
  • 2
  • 25
  • 49
  • yep i know there are many constructor possibility, writer should have mention other constructor too.. – M. Qasim Sep 08 '16 at 07:02
  • I hope you know it now. Please mark the best answer (in your opinion) as right and accept it. – Markus G. Sep 08 '16 at 07:03
  • This is not the syntax for [calling a constructor from another](http://stackoverflow.com/questions/308276/call-constructor-from-constructor-in-c). – Tamir Vered Sep 08 '16 at 11:15
2

For the code to be compiled CArray must have parameterless constructor; the most probable syntax is

   public CArray()
     : this(0) {
   }  

i.e. if size is not specified, let it be equal to 0

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

The default parameterless constructor is not available If you define a custom constructor with parameters in your class.

In that case if you want to keep the parameterless constructor you have to define it explicitly in your class.

So in your example, based on the class definition you provided, is not possible to create a new instance like this: new CArray()

Mauro Sampietro
  • 2,739
  • 1
  • 24
  • 50
1

You must to have a Constructor with no parameter and there is some way:

  • 1: Optional parameter
  • 2: Multiple Override

1: Optional parameter:

public class CArray {
     private int[] arr;
     private int upper;
     private int numElements;

     //Constructor with default value parameter
     public CArray(int size = 0)
     {
         //Your logic must be here
     }
}

2: Multiple Override

public class CArray {
     private int[] arr;
     private int upper;
     private int numElements;

     //Constructor with zero parameter
     public CArray()
     {
         this = new CArray(0);
     }
     public CArray(int size)
     {
         //Your logic must be here
     }
}

So at end and after define your class, you can use in two type:

var MyArray1 = new CArray();
var MyArray2 = new CArray(10);
Imran Sh
  • 1,623
  • 4
  • 27
  • 50