Usage of Keywords

Usage of "new" keyword

Used to create objects and invoke constructors.

Class1 obj  = new Class1();

It is also used to create instances of anonymous types:

var query = from cust in customers
            select new { Name = cust.Name, Address = cust.PrimaryAddress };

The new operator is also used to invoke the default constructor for value types.

int i = new int();

In the preceding statement, i is initialized to 0, which is the default value for the type int

Note : If the new operator fails to allocate memory, it throws the exception, OutOfMemoryException.

Usage of "sealed" keyword

When applied to a class, the sealed modifier prevents other classes from inheriting from it.

class A {}
sealed class B : A {}

In the above example, class B inherits from class A, but no class can inherit from class B.

You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties

Example

class X
{
    protected virtual void F() { Console.WriteLine("X.F"); }
    protected virtual void F2() { Console.WriteLine("X.F2"); }
}

class Y : X
{
    sealed protected override void F() { Console.WriteLine("Y.F"); }
    protected override void F2() { Console.WriteLine("Y.F2"); }
}

class Z : Y
{
    // Attempting to override F causes compiler error CS0239.
    // protected override void F() { Console.WriteLine("Z.F"); }

    // Overriding F2 is allowed.
    protected override void F2() { Console.WriteLine("Z.F2"); }
}

In the above example, Z inherits from Y but Z cannot override the virtual function F that is declared in X and sealed in Y.

Note : It is an error to use the abstract modifier with a sealed class, because an abstract class must be inherited by a class that provides an implementation of the abstract methods or properties.

Usage of "this" keyword

The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.

  • To qualify members hidden by similar names, for example:
public class Employee
{
    private string alias;
    private string name;

    public Employee(string name, string alias)
    {
        // Use this to qualify the members of the class 
        // instead of the constructor parameters.
        this.name = name;
        this.alias = alias;
    }
}
  • To pass an object as a parameter to other methods, for example:
CalcTax(this);

Usage of "static" keyword

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, finalizers, or types other than classes.

Example

The following class is declared as static and contains only static methods:

static class CompanyEmployee
{
    public static void DoSomething() { /*...*/ }
    public static void DoSomethingElse() { /*...*/  }
}

A constant or type declaration is implicitly a static member.

A static member cannot be referenced through an instance. Instead, it is referenced through the type name. For example, consider the following class:

public class MyBaseC
{
    public struct MyStruct
    {
        public static int x = 100;
    }
}

To refer to the static member x, use the fully qualified name, MyBaseC.MyStruct.x, unless the member is accessible from the same scope:

Console.WriteLine(MyBaseC.MyStruct.x);
  • While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.
  • It is not possible to use this to reference static methods or property accessors.
  • If the static keyword is applied to a class, all the members of the class must be static.
  • Classes and static classes may have static constructors. Static constructors are called at some point between when the program starts and the class is instantiated.