What is Static and This Keyword

Static Keyword

  • The fields, properties, and methods of a class can be either instance members or static members.
  • All methods are instance methods unless you explicitly mark them with the static mark them with the static keyword.
  • Methods or fields marked by static keyword belong to classes not to the instances.
  • Sometimes to some methods we do not want to create object of class since the method is used frequently, hence we mark them as static methods, For example the Readline and Writeline methods of Console class.
  • Static methods are shared.
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A field marked as constant by const keyword is also a static field.
  • You can use the members of a static class without creating an instance of that class.
  • Static classes can be used to create extension methods.
  • Static Keyword can be applied on Classes, Operators, Variables, Events, Methods, Constructors, Properties.
  • Static keyword cannot be applied on Indexers, Destructors, Types other than classes.
  • Static is modifier that is used to declare a static member, which belongs to the type itself rather than to a specific object.

The static modifier can be used to declare static classes. In classes, interfaces, and structs. we may add the static modifier to fields. Methods, properties, operators, events and constructors.

Example:

 

 

using System;
 namespace StaticKeyword {
  class Test {
   public void display1() {

     Console.WriteLine("it is a non static method");
  }
   public static void display2() {

     Console.WriteLine("it is a static method");
    }
   }
 class Program {
  static void main(String[] agrs) {
   Test t1 = new Test();
   t1.display1();
   Test.display2();
   }
 }
}

 

STATIC:-We can define class members as static using the static keyword. When we declare a member of a class as static, it means no matter how many objects of the class are created, there is only one copy of the static member.

VOID and PUBLIC VOID:-Void is a type modifier that specifies that the method doesn’t return any value.Public − This is the access specifier that states that the method can be accesses publically.

  • A static variable is a variable with a static modifier and it gets shared among all class (i.e. type) objects.
  • The static method is method with a modifier and enables us to execute commands related to the class type, but not specific to object instances. In other words, A static method cannot access non – static class – level members.
  • A Static constructor enables us to initialize any static data or to perform a particular action that needs to be performed only once. A class can have a single constructor, that must have no access modifiers and no parameters.
  • A static class is a class that is declared static and contains only static members. A static class is basically the same as creating a class that contains only static members and a private constructor.

This Keyword

Using this keyword to refer class fields

  • This keyword in C# with programming uses and examples. – In C# programs, this keyword is used for following:
  1. To refer current class fields (instance variables).
  2. To invoke the current class method.
  3. To invoke the current class constructor(imlicity).
  4. To pass as a current class object in the constructor.
  5. To return the current class instance from the method.
  6. This keyword is used to refer class fields to avoid ambiguity between parameters of constructor or functions and class fields. For example; in below class constructor, if we don’t use this keyword then we don’t know if parameter is initialized to class field or class field is initialized to parameter.

Example:

public Class Car {
    string model;
    double price;
  public  car(string model, double price) {
          model = model;
          price = price;
     }
  }

 

  • If we use this keyword then it is clear indication that we are initializing class fields by constructor parameters.

Using this keyword to refer class method

This keyword can also be used in class method to improve readability. For example In below program we have used this keyword in highest price method to make the current object and incoming object in the parameter readable.

Example:

using System;
  namespace Keywords
   {
    public class Employee
    {
         //calling displayDetails() method of same class
      this.displayDetails();
      Console.WriteLine("Salary: Rs.50000");
     }
        void displayDetails()
         {
       Console.WriteLine("Name: ABC");
       Console.WriteLine("ID: 123ABC");
         }
    public static void Main(String[] args)
     {
    Employee emp = new Employee();
    emp.displaySalary();
   }
  }
} 

 

 

Leave a Comment