C# Default constructors

In this chapter you will learn:

  1. What are C# Implicit parameterless constructors
  2. Example for C# Implicit parameterless constructors

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:

  1. Constructors with named parameters
  2. Optional parameters and constructors
Home »
  C# Tutorial »
    C# Types »
      C# Constructor
C# Constructor
C# Default constructors
C# Constructor's Parameters
C# Constructor Overload
C# Static Constructors
C# Constructors and Inheritance