C# Constructors - CSharp Custom Type

CSharp examples for Custom Type:Constructor

Introduction

A class constructor is executed whenever we create new objects of that class.

A constructor has the same name as that of class without any return type.

Demo Code

using System;/* w w w  . jav  a  2  s.c o  m*/
class Line {
   private double length;   // Length of a line
   public Line() {
      Console.WriteLine("Object is being created");
   }
   public void setLength( double len ) {
      length = len;
   }
   public double getLength() {
      return length;
   }
   static void Main(string[] args) {
      Line line = new Line();
      line.setLength(6.0);
      Console.WriteLine("Length of line : {0}", line.getLength());
   }
}

Result


Related Tutorials