+ 1

What is the meaning of this constructor? And why is it a constructor?

public List(int capacity=defaultCapacity) { items = new T [capacity] ; }

17th Nov 2017, 10:01 PM
Đ’Đ°Đ»Đ”Ń€ĐžŃ
Đ’Đ°Đ»Đ”Ń€ĐžŃ - avatar
1 Answer
+ 12
A constructor was used the perform initial setup or logic need to be performed when the object was created/instantiated and have the same name as its class. Just like methods, constructor can be overloaded with different parameters like the example you've given — It use an optional parameter to setup the object. Therefore the following statements will trigger the constructor above:- var listA = new List(); var listB = new List(123); P/S: This is an oversimplification as the defualt implementation of List in C# include generics.
17th Nov 2017, 10:14 PM
Zephyr Koo
Zephyr Koo - avatar