C# is a simple, type-safe, modern, general-purpose, managed and object-oriented programming language developed by Microsoft for writing .NET applications. The syntax used in C# is mostly same as those of C++. It meets all the conditions of OOPs completely like abstraction, inheritance, encapsulation, polymorphism etc.
C# has following features-
There is variety of reasons to use C# language-
Object is an instance of a class that is created dynamically. An object is a block of memory that has been allocated and configured in a class. There can be many objects in a class and can be stored in either a named variable or in an array or collection With the help of objects, we can access the member of a class using the dot.
Constructor is a special method of the class which is automatically invoked when an object of the class is created .Constructors are mainly used for object initialization and memory allocation of its class. If you do not create a constructor in the class, the compiler will automatically create a default constructor in the class to initialize all numeric fields in the class to zero and all string and object fields to null.
Syntax
[Access Modifier] ClassName([Parameters])
{
}
Constructors are of 5 types
Constructor overloading means that we can have more than one constructor with different parameters. It is simple to creating an overloaded constructor by adding additional constructors to the code. Each constructor has a unique signature.
using System;
public Class OC
{
public OC()
{
//Default constructor
}
public OC(int Marks)
{
// Constructor with single parameter
}
public OC(int Marks, string Subject)
{
// Constructor with two parameters
}
}
Static constructor is a constructor that is called before the first object of the class is created. It is called automatically before the first instance is created. It is used to initialize any static data, or to perform a particular action that needs performed once only. The user has no control on when the static constructor is executed in the program but it is definitely before the first object creation. There should not be any parameter in static constructor. It cannot be called directly. It can access only the static members of the class. Only one static constructor is allowed.
Private constructor is a constructor with "private" access modifier in a class. It is an instance constructor used in class that contain static member only. If a class has one or more private constructor and no public constructor then other classes are not allowed to create either create the object of the class or inherit it. In order to access the methods or properties in a class having private constructor, we have to assign methods or properties with "static" keyword. With private constructor, we can also have public constructor (overloaded constructor).
A copy constructor is a parameterized constructor that allows to copy the data stored in the member variables of an object of the class into another object. It is used to initialize new instance to the values of an existing instance.
Method overriding will create two or more methods with same name and same parameter in different classes but method overloading will create more than one method with same name but different parameter in same class.
Following types of comments are there in C# -
Single Line Comment (//)
// Single line comment
Multiline Comments ( /* */)
/*
Multiline comment
Line 2 comment
Last line comment
*/
XML Comments ( ///)
/// Summary1;
/// Note;
/// Summary2;