What is use of Static Keyword in C#

What is use of Static Keyword in C#

1. Static Keyword reduce the creating object of the class.
2. If we declare static keyword with a variable, so we can retrieve the data without creating object.
    And we need call variable directly using => Class.variable
3. It is also runtime variable.
4. Use for design pattern like factory pattern , singleton class and etc...

Here we have an implementation of Declaration of variable with static.
Program:-

using System;
namespace Tests1014
{
   public class Program
    {
        public static int a = 9;
        public int b = 10;
        public int sum = 0;

        public Program()
        {
            a = 90;
        }
        public void Add()
        {
         
            sum = a + b;
            Console.WriteLine("Addtion =" +sum);
        }
        public static void Main(string[] args)
        {
            Program p = new Program();
            p.Add(); // no need of creating object because it is static
            Console.WriteLine("static variable =" +Program.a);
            Console.ReadLine(); 
        }
    }
}

Output:- Addition 100
              static variable 90  // value change while declaration was 9 initialize in constructor 90 . so it                                                   is run time compile.


Here we have an implementation Method and class with static key words.

1. Method wise :- Add

>  If static key word  is declared in function suppose " public static void Add()"  then
    utilization of declare variable all should have static key words. " sum = a + b;" other wise it will          raise compile time error.

> No need of creating object. we can access directly. "Program.Add(); "
   Program is class name and Add is function name.

using System;
namespace Tests1014
{
   public class Program
    {
        public static int a = 9;  => static
        public static int b = 10; => static
        public static int sum = 0;=> static

        public static void Add()
        {         
            sum = a + b;
            Console.WriteLine("Addtion =" +sum);
        }
        public static void Main(string[] args)
        {
            Program p = new Program();
            // p.Add(); // red mark indication compile time, so no need of creating object because it is                                         static
            Program.Add();         
            Console.ReadLine(); 
        }
    }
}

Output:- 19


2. Class wise :- Program //Adding static in class name

> If we declare static key word in class , then everything should be static inside the class
   function , declare variable.
> It will not allow to create the Object of class other wise compile time error.

using System;
namespace Tests1014
{
   public static class Program
    {
        public static int a = 9;
        public static int b = 10;
        public static int sum = 0;

        public static void Add()
        {         
            sum = a + b;
            Console.WriteLine("Addtion =" +sum);
        }
        public static void Main(string[] args)
        {
           // Program p = new Program();//=> It will not allow to create the object because we are in same class and define with statci key word
            // p.Add(); // red mark indication compile time, so no need of creating object because it is static
            Program.Add();         
            Console.ReadLine(); 
        }
    }
}

Output :-19

Share this

Previous
Next Post »