C# Static Keyword

In c#, static is a keyword or a modifier which is used to make a class or methods or a variable properties as not instantiable that means we cannot instantiate the items which we declared with a static modifier.

 

The static members which we declared can be accessed directly with a type name. Suppose if we apply a static modifier to a class property or to a method or variable, then we can access those static members directly with a class name, instead of creating an object of a class to access those properties.

C# Static Variables

Following is the example of defining a class with static properties and those can be access directly with a type instead of specific object name.

 

class User

{

public static string name, location;

public static int age;

}

If you observe above example, we defined a variables with static keyword and we can access those variables directly with a type name like User.name or User.location and User.age.

 

Following is the example of accessing the variables directly with a type name in c# programming language.

 

Console.WriteLine(User.name);

Console.WriteLine(User.location);

Console.WriteLine(User.age);

If you observe above statements, we are accessing our static properties directly by using class name instead of with class instance.

 

Generally, in c# the instance of class will contain a separate copy of all instance fields so the memory consumption will increase automatically, but if we use static modifier there is only one copy of each field so automatically the memory will be managed efficiently.

 

In c#, we can use static modifier with classes, methods, properties, constructors, operators, fields and with events but it cannot be used with indexers, finalizers or types other than classes.

C# Static Keyword Example

Following is the example of creating a class by including both static and non-static variables & methods. Here we can access non-static variables and methods by creating an instance of the class, but it’s not possible for us to access the static fields with an instance of class so the static variables and methods can be accessed directly with the class name.

 

using System;

 

namespace Tutlane

{

class User

{

// Static Variables

public static string name, location;

//Non Static Variable

public int age;

// Non Static Method

public void Details()

{

Console.WriteLine("Non Static Method");

}

// Static Method

public static void Details1()

{

Console.WriteLine("Static Method");

}

}

class Program

{

static void Main(string[] args)

{

User u = new User();

u.age = 32;

u.Details();

User.name = "Suresh Dasari";

User.location = "Hyderabad";

Console.WriteLine("Name: {0}, Location: {1}, Age: {2}", User.name, User.location, u.age);

User.Details1();

Console.WriteLine("\nPress Any Key to Exit..");

Console.ReadLine();

}

}

}

If you observe above example, we created a class called “User” with static and non-static variables & methods. Here we are accessing non-static variables and methods with an instance of User class and static fields & methods are able to access directly with the class name (User).

 

Following diagram will illustrate more details about how static and non-static variables & methods can be accessed in our c# application.

 

Difference between static and non-static fields access in c#

 

If you observe above diagram, it clearly says that non-static fields and methods can be accessed only with an instance of class and the static fields & methods can be accessed directly with the class name.

 

When we run above c# program, we will get the result like as shown below.

 

C# Static Keyword Example Result

 

This is how we can use static keyword in our c# applications to make a class or methods or a variable properties as not instantiable based on our requirements.