Q:

What is Constructor in C#?

0

What is Constructor in C#?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In C#, a constructor is a special method whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments. Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read.

Note: If you don’t provide a constructor for your class, C# creates one by default that instantiates the object and sets member variables to the default values.

The constructor in C# has the same name as class or struct. Below I am mentioning some types constructors supported by C#.

  1. Default Constructor.
  2. Parametrized Constructor.
  3. Copy Constructor.
  4. Private Constructor.
  5. Static Constructor.

An example of a constructor,

public class PersonInfoInfo
{
   private string last;
   private string first;
   
   //constructor
   public PersonInfo(string lastName, string firstName)
   {
      last = lastName;
      first = firstName;
   }
   
   // Remaining implementation of PersonInfo class.
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Interview Questions and Answers,You Need To Know

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Explain some points related to the constructor?... >>
<< What happens if the inherited interfaces have conf...