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.
Is that possible?
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.
Is that possible?
There could a few possibilities:
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.
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.
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
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()
You must to have a Constructor with no parameter and there is some way:
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);