Static Variable is shared across all objects in the same class.
Static Constructor will only be executed once, it takes no parameter and no access modifier (private/public). It will executed once and first before other constructor take place. e.g.
static ClassName()
{
//doSomething
}
Readonly: one time initialization, the value can only be initialized in constructor.
const : constant value that cannot be modified, implicitly static variable
sealed : class that cannot be inherited.
abstract : As the name implies, it only provide the overall view on the class. Meaning, there is no implementation for the method. It only defines the methodName and signature. This type of class cannot be instantiated. The abstract method must belong to abstract class.
The derived class that inherited this abstract class must override the abstract method and provide the implementation for it.
virtual : This is similar to abstract which provide the derived class flexibility to modify the implementation for the virtual method. The only difference between virtual and abstract is that, for virtual case, the base class has implementation for the method whereas in abstract case, there is no implementation for the method provided. This is essential to implement polymorphism in C#, where we could have the different implementation for the same method name.
base : This refers to the base class in derived class. Since C# does not support multiple inheritance, it is not necessary to specify the name for the base class.
this : This keyword refers to the current object
new : This create a new method in derived class when the method name in both base class and derived class are the same. The method with new is not polymorphised.
interface : This provides the overall view on the method name and signature but no the detailed implementation for method. The syntax for interface does not contain access modifier (public / private), it is implicitly declared as public. The class that implement this interface do not need to add keyword override to the method that defined in interface.
interface Itest {
void test1();
void test2(int x);
}
The class that implement interface must provide implementation for all methods defined in the interface. It is possible to support more than one interface.
Interface cannot be instantiated. If A implements interface B, we could say A is B. Therefore, we could assign objA to objB. As a result, there are referring to the same reference type (A).
//interface B { ... }
//public class A : BA objA = new A();
B objB = (B) objA;//both objA and objB refer to reference type A.
If a class implements more than one interface and the method name is repeated in these interfaces. It is necessary to specify the interface name explicitly.
interface Istorage
{
void read();
void write();
}interface Isave
{
void read();
void write();
}
public class Document : Istorage , Isave
{
public void read()
{
Console.WriteLine("Read method from istorage Document");
}
void Isave.read()
{
Console.WriteLine("Read method from isave Document");
}
}
We need to specify the interface name without access modifier (public/ private) to define the implementation for read(). The first read method in this class refers to Istorage because istorage has the preceding position. Class is allowed to have multiple interface but this is not true for inheritance.