C# Constructor and Properties | C# Tutorials
C# Constructor and Properties | C# Tutorials
A class constructor is a special member method of a class that is executed whenever a new object if that class is created. It is used to initialize objects. The constructor in C# has the same name as class or struct.
class TutorialsLink
{
.......
// Constructor
public TutorialsLink () {}
.......
}
// an object is created of Geek class,
// So above constructor is called
TutorialsLink obj = new TutorialsLink ();
There are a few things that are needed to keep in mind while working with constructors.
- The name of the class and constructor of the class should have the same name.
- A constructor cannot be abstract.
- One static constructor can be created inside a class.
- There is no return type for a constructor, not even void.
- A single class can have one or more than one constructors.
There can be many types of constructors but the most used are two types of constructors:
- Default constructors: It has no arguments and is invoked at the time of creating objects.
- Parameterized Constructors: It has arguments and is used to provide different values to distinct objects.
Others are:
- Copy Constructor: This constructor makes an item by replicating factors from another article. Its primary use is to instate another example to the estimations of a current occurrence.
- Private Constructor: In the event that a constructor is made with a private specifier is known as a Private Constructor. It isn't workable for different classes to get from this class and furthermore it's unrealistic to make a case for this class.
There are few things about Private Constructors which is needed to be taken care of-
-
-
- It is the implementation of a singleton class pattern.
- When we have only static members use a private constructor.
- Using a private constructor can prevent the creation of instances of that class.
-
- Static Constructor: Static Constructor must be conjured just a single time in the class and it has been summoned during the production of the principal reference to a static part in the class. A static constructor is instated static fields or information of the class and to be executed just a single time.
Example (Parameterized constructor)
// parameterized constructor.
using System;
namespace ParameterizedConstructorExample {
class TutorialsLink {
String name;
int id;
TutorialsLink (String name, int id)
{
this.name = name;
this.id = id;
}
// Main Method
public static void Main()
{
// This will invoke parameterized
// constructor.
TutorialsLink tutorialslink1 = new TutorialsLink ("GFG", 1);
Console.WriteLine("Username = " + tutorialslink1.name +
" and User-Id = " + tutorialslink1.id);
}
}
}
Properties in C#
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field.
Properties can be used as if they are public data members but they actually include special methods called accessors.
The accessor helps in getting or setting a corresponding field.
Class TutorialsLink {
private string name ;
public string Name ;
{
get { return name;}
set { name = value;}
}
}
Once the property is defined, we can use it to assign and read private members.
state void Main ( string {} arg)
{
TutorialsLink T = new TutorialsLink();
T.Name = “Sam”;
Console.WiteLine(T.Name);
}