I’ve been learning C# and wanted to review some open source projects to see some good written code. I found a project called Todomoo on sourceforge and there’s a part that is puzzling me:
public class Category {
// Note properties
private int id = 0;
private string name = "";
private Color colour = Color.Gray;
/// <summary>
/// Create a new category.
/// </summary>
public Category() { }
/// <summary>
/// Load a category from the database.
/// </summary>
/// <param name="Id">ID of the category</param>
public Category(int id) : base() {
Load(id);
}
Here it uses base() in one of the constructors, but the class is not a derived class. So what exactly is that for?
And why is the syntax of base() is that way and not like:
public Category(int id) {
base();
Load(id);
}
The class is a derived class – it implicitly inherits from
System.Object. It is not clear why anyone would invokebase()constructor forSystem.Object, though: it is done implicitly as well.As far as the syntax goes, my guess is that C# adopted a syntax that is close to C++ initializer lists, not to Java invocation of base constructors.