Algorithm


In C#, the static keyword is used to define members of a class that belong to the class itself rather than instances of the class. This means that you can access static members without creating an instance of the class.

Here are the main uses of the static keyword in C#:

 

Code Examples

#1 Static Variables in C# program

Code - C# Programming

using System;

namespace StaticKeyword {

  class Student {

    // static variable
    public static string department = "Computer Science";
  }

  class Program {
    static void Main(string[] argos) {
    
      // access static variable
      Console.WriteLine("Department: " + Student.department);
     
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Department: Computer Science

#2 Static Variables Vs Instance Variables in C#

Code - C# Programming

class Student {

  // instance variable
  public string studentName;
 }

class Program {
  static void Main(string[] args) {

    Student s1 = new Student();
    Student s2 = new Student();
  }
}
Copy The Code & Try With Live Editor

#3 Code Excample- Static Variable Vs. Instance Variable in C# Programming

Code - C# Programming

using System;
 
namespace StaticKeyword {
 
  class Student {
    static public string schoolName = "DevsEnv School";
    public string studentName;
 
  }
 
    class Program {
    static void Main(string[] args) {
       
      Student s1 = new Student();
      s1.studentName = "Akash";

      // calls instance variable
      Console.WriteLine("Name: " + s1.studentName);
      // calls static variable
      Console.WriteLine("School: " + Student.schoolName);
 
      Student s2 = new Student();
      s2.studentName = "Azad";
   
       // calls instance variable
      Console.WriteLine("Name: " + s2.studentName);
      // calls static variable
      Console.WriteLine("School: " + Student.schoolName);
      
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Name: Akash
School: DevsEnv School
Name: Azad,br> School: DevsEnv School

#4 Static Methods in C# Program

Code - C# Programming

class Test {

  public static void display() {....}

}

class Program {
  static void Main(string[] args) {

    Test.display();
  }
}
Copy The Code & Try With Live Editor

#5 Static and Non-static Methods in C# Programming

Code - C# Programming

using System;
 
namespace StaticKeyword {
 
  class Test {
 
    public void display1() {
 
      Console.WriteLine("Non static method");
    }
    public static void display2() {
 
      Console.WriteLine("Static method");
    }
  }
 
  class Program {
    static void Main(string[] args) {
 
      Test t1 = new Test();
      t1.display1();
      Test.display2();    
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Non static method
Static method

#6 Static Class in C# Programming

Code - C# Programming

using System;

namespace StaticKeyword {

  static class Test {
    static int a = 5;
    static void display() {

      Console.WriteLine("Static method");
    }
  
    static void Main(string[] args) {

      // creating object of Test
      Test t1 = new Test();
      Console.WriteLine(a);
      display();
    }
  }
}
Copy The Code & Try With Live Editor

#7 Access static Members within the Class in C# programming

Code - C# Programming

using System;
 
namespace StaticKeyword {
 
  class Test {
 
    static int age = 25;
    public static void display() {
 
      Console.WriteLine("Static method");
    }
   
    static void Main(string[] args) {
 
      Console.WriteLine(age);
      display();
      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
25
Static method
Advertisements

Demonstration


C# Programming static Keyword