Connect with us

Hi, what are you looking for?

Programming

C# For Intermediate – Constructors

C# For Intermediate - Constructors
C# For Intermediate - Constructors

In this tutorial, we’ll learn about constructors and constructor types in C# programming also we’ll repeat OOP programming and class logic in this tutorial.

This tutorial part is the other part of the OOP programming training, if you are reading the first part, you can read it here. For a better experience for mobile readers, use your phone horizontally.

Constructors In C#

The constructors are special method that is executed at the time the class is called, it can take arguments just like methods, except when the class is called, it cannot return a value like methods.

It is mostly used when calling the class to assign values within it. However, you can adjust the constructors as you wish.

class Example
{
  public Example()
  {
    // Code
  }
}

As you can see above, the syntax of the constructor is given, since constructors do not return any values, they do not take additions such as int, void, string. Also, the names of the constructors must be the same as the class.

Types Of Constructers

We’ll learn 4 types of constructors which are Default Constructor, Parameterized Constructor, Copy Constructor, Private Constructor.

Default Constructors

Default Constructors do not have arguments. The default constructor initializes all numeric fields to zero and all string and object fields to null inside a class.

In short, they are hollow constructors and their intended use is to inform the programmer whether the object is called or not.

using System;

class Example
{
  // variables
  int x;
  string y;
  bool z;

  // constructors 
  public Example()
  {
   Console.WriteLine("Started");
  }
}

class Program
{
  public static void Main(string[] args)
  {
   // calling object
   Example ex = new Example();

   Console.WriteLine(ex.x);
   Console.WriteLine(ex.y);
   Console.WriteLine(ex.z);
  }
}
Output:
Started
0
None
False

As you can see, the constructor runs first and writes “started” on the screen, then when we call the values int default 0 appears as the string is empty, I filled it as None, bool will return as False.

Parameterized Constructors

The constructors used with at least 1 parameter are called the Parameterized Constructor. These constructors are used to assign values to the data used in the class when the object is initialized.

using System;

class Example
{
 public string name;
 public int age;

 // Parameterized Constructor
 public Example(string Name,int Age)
 {
   // variable = paramater
   this.name = Name;
   this.age = Age;
 }
}

class Program
{
 public static void Main(string[] args)
 {
   Example ex = new Example("Alex",9);
    
   Console.WriteLine(ex.name);
   Console.WriteLine(ex.age);
 }
}
Output:
Alex
9

As seen above, the constructor takes 2 parameters and gives these parameters to the variables in the class, and the output on the screen becomes the values determined by the programmer.

Copy Constructors

This constructor creates an object by copying variables from another object. Its main use is to initialize a new instance to the values of an existing instance.

using System;

public class CopyExample
{
  public string name;
  public int age;

  public CopyExample(CopyExample c)
  {
    this.name = c.name;
    this.age = c.age;
  }

  public CopyExample(string Name , int Age)
  {
    this.name = Name;
    this.age = Age;
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    CopyExample c1 = new CopyExample("David", 12);
    CopyExample c2 = new CopyExample(c1);

    Console.WriteLine(c2.name + c2.age);
  }
}
Output:
David
12

Private Constructors

It is not possible for other classes to derive from this class and also it’s not possible to create an instance of this class.

using System;

public class Spesific
{
  public static int age = 10;

  private Spesific()
  {
  }

  public static int GrowUp()
  {
    return age++;
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Console.WriteLine(Spesific.GrowUp());
  }
}
Output:
11
Static Constructors

The static constructers is executed only once, initializes static fields or data in the class, and can also be used for one-time operations.

  • It can’t be called directly.
  • When it is executing then the user has no control.
  • It does not take access modifiers or any parameters.
  • It is called automatically to initialize the class before the first instance created.
using System;

public class StaticExample
{
  public int x = 0;
  static StaticExample()
  {
    Console.WriteLine("Static Constructor");
  }

  public StaticExample(int ObjectID)
  {
      Console.WriteLine("ObjectId:" + ObjectID);
  }
}

public class Program
{
  static void Main(string[] args)
  {
    StaticExample obj1 = new StaticExample(1);
    StaticExample obj2 = new StaticExample(2);
  }
}
Static Constructor
ObjectId:1
ObjectId:2

When the static constructor class is called, it is executed only once, while parameterized constructors are executed as much as the derived object.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Programming

This article is prepared to explain concepts that will be important for programming beginners such as data types, variables, heap and stack, and operators....

Programming

This article was created for those who want to use functional programming in their career or who are learning new programming paradigms. We will...

Programming

Functions are pieces of code that combine the integrity of the code used to perform a task into a single structure. If you are...

Programming

Computers can solve an event in different ways, we call these ways algorithms, each algorithm works differently from the others and has different processing...