C# Lang Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I still beginner in C# and i wondering what below line (*) means

public abstract class DataCollection : ICollection, ISerializable,
IDeserializationCallback
{
private const int defaultCapacity = 8;

protected DataCollection() : this(defaultCapacity)
//(*) This Line why colon after constructor, What is means?
{
}
}
 
Constructor with no parameter DataCollection() will call another constructor
with a parameter DataCollection(defaultCapacity)

Eliyahu
 
Just to give an example:

public class User(){
private int userId;
publlic int UserId{
get { return userId}
public void User() : this(1000){}
public void User(int userId){
this.userId= userId
}
}


User user1 = new User();
Response.Write(user1.UserId); //will output 1000

notice how I call the constructor with no parameters, but UserId = 1000
that's because the User() constructor called the User(int userId)
constructor....

Karl
 
Constructor chaining. This constructor is calling another constructor that
looks like so:

protected DataCollection(int capacity)
{
}


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Constructor chaining. This constructor is calling another constructor that
Extremely useful feature for saving yourself from rewriting code multiple
times!

You have the more genereic constructors (less arguments) calling the most
complicated one, passing in appropriate defaults.

Good luck! :>
 
Back
Top