Use Of Static keyword in C#(sharp)

Static is a keyword or a modifier that can be used in C# to prevent a class, method, or variable property from being instantiated, meaning that we cannot create the objects we define with a static modifier.

With a type name, you may immediately access the static members we declared. If a class property, function, or variable is given the static modifier, let’s say that we can use the class name to directly access those static members rather than making an object of the class in order to access those properties.

C# Static Variables

The example that follows shows how to define a class with static attributes that may be accessed directly using the type rather than a specific object name.

class student
{
public static string name, location;
public static int age;
}

If you look at the sample above, you will see that we defined variables with the static keyword and that we can directly access those variables by using type names like student.name, student.location, and student.age.

The C# programming language’s direct access to variables using a type name is demonstrated in the example that follows.

Console.WriteLine(student.name);
Console.WriteLine(student.location);
Console.WriteLine(student.age);

If you look at the statements above, you will see that we are accessing our static properties using the class name rather than the class instance.

we can use the static modifier with classes, methods, properties, fields in C#, . However, we cannot use it with indexers, finalizers, or types that are not classes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{

    public  class student
    {
        public static string name, location;
        public int age;
        // Nom-Static Method
        public void print()
        {
            Console.WriteLine("Non Static Method");
        }
        // Static Method
        public static void print1()
        {
            Console.WriteLine("Static Method");
        }

    }

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

            student std = new student();
            std.age = 32;
            std.print();
            student.name = "Vishal patel";
            student.location = "USA";
            Console.WriteLine("Name: {0}, Location: {1}, Age: {2}", student.name, student.location, std.age);
            
            student.print1();
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();

        }
    }

}

As you can see from the example above, we developed a class called “student” that has static and non-static variables and methods. Here, we are using an instance of the student class to access non-static variables and methods, but static fields and methods can be accessed directly by the class name (student).

the following image show the Non- static  and static variable and method in example.

  • Non-static

  • static

while you run above program.you will get following result.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories