Introduction:

In this article we will explain how to use static modifier or what is static keyword in c# with example.

Description:

In c# programming language static is a keyword or a modifier which is used to make a class or methods or a variable properties or operators or events or constructors 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 classes, variables, methods, properties, operators, events and constructors, 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.

using System;
namespace SQLTutorialsPoint
{
    class User
    {
 
        public static string name, address;
 
        public static int age;
 
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(User.name);
 
            Console.WriteLine(User.address);
 
            Console.WriteLine(User.age);
        }
    }
}

 

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


C# static modifier 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 SQLTutorialsPoint
{
    class User
    {
        //Static Variable
        public static string name, address;
        //Non Static Variable
        public int age;
        public void GetDetails()
        {
 
            Console.WriteLine("Non Static GetDetails Method");
 
        }
        public static void GetDetails1()
        {
 
            Console.WriteLine("Static GetDetails1 Method");
 
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            User u = new User();
 
            u.age = 32;
 
            u.GetDetails();
 
            User.name = "Kavi Kumar";
 
            User.address = "Mumbai";
 
            Console.WriteLine("Name: {0}, Location: {1}, Age: {2}", User.name, User.address, u.age);
 
            User.GetDetails1();
 
            Console.WriteLine("\nPress Enter Key to Exit..");
 
            Console.ReadLine();
        }
    }
}

 

As you see 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).

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

 how to use static keyword in c#

In this article we have learned, 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.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like