Full width home advertisement

Post Page Advertisement [Top]

Constructors in C#


A class constructor is a special member function of a class that is executed whenever we create new objects of that class.

A constructor will have exact same name as the class and it does not have any return type. Following example explains the concept of constructor:

using System;

namespace LineApplication

{

   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();    

         // set line length

         line.setLength(6.0);

         Console.WriteLine("Length of line : {0}", line.getLength());

         Console.ReadKey();

      }

   }

}

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib