What are default 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 ww  .jav  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.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor