C# Static method and variables

In this chapter you will learn:
  • What is static keyword in C# programming?
  • How to use it with functions and variables?
  • How to use static method or variable in C sharp programming?

Whenever you write a function or declare a variable, it doesn’t create an instance in a memory until you create an object of the class. But if you declare any function or variable with a static modifier, it directly creates an instance in a memory and acts globally. The static modifier doesn't reference any object.

How to: It is very easy to create static modifier with variables, functions and classes. Just put static keyword before the return data type of method.

namespace Static_var_and_fun
    class number
        // Create static variable
        public static int num;
        //Create static method
        public static void power()
            Console.WriteLine("Power of {0} = {1}", num, num * num);
    class Program
        static void Main(string[] args)
            Console.Write("Enter a number\t");
            number.num = Convert.ToInt32(Console.ReadLine());

Enter a number  7
Power of 7 = 49 __


In this chapter you learned in details about static method and variables. You also learned how to use it in program. In next chapter you will learn about Main method in C#.


Share your thought