How to instantiate a class in C#?

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

Updated on: 22-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started