C# Default constructors
In this chapter you will learn:
Description
For classes, the C# compiler automatically generates a parameterless constructor if and only if you do not define any constructors.
If a class has a constructor, the parameterless constructor is no longer automatically generated.
For structs, a parameterless constructor is intrinsic to the struct; therefore, you can- not define your own.
The role of a struct's implicit parameterless constructor is to initialize each field with default values.
Example
Example for C# Implicit parameterless constructors
using System;//from w w w . j av a 2 s. c o m
class Rectangle{
private int Width;
private int Height;
}
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter: