How to instantiate a class in C#?

CsharpProgrammingServer Side Programming

Use the new operator to instantiate a class in C#.

Let’s say our class is Line. Instantiation will create a new object as shown below −

Line line = new Line();

Using the object, you can now call the method −


Let us see the example −


 Live Demo

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
         Console.WriteLine("Length of line : {0}", line.getLength());


Object is being created
Length of line : 6
Published on 31-Aug-2018 08:08:58