Static in C#


In C#, data members, member functions, properties and events can be declared either as static or non-static.  

Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. 

 

Static members are often used to represent data or calculations that do not change in response to object state.


Static can be used in following ways:

  1. Static data members

  2. Static constructor

  3. Static Properties

  4. Static methods

Static Data Members

A C# class can contain both static and non-static members. When we declare a member with the help of the keyword static, it becomes a static member. 

A static member belongs to the class rather than to the objects of the class. Hence static data members are also known as class members and non-static members are known as instance members.

Static members and preloaded in memory while instance members are post loaded in memory.

You can't use this keyword with static, as it will not initialize. Static members are shared level and are not initiated.

Note:
Indexers in C# can't declared as static.

Static fields can be declared as follows by using the keyword static.

  class MyClass
    {
        public static int age;
        public static string name = "George";
    }

 

When we declare static data members inside a class, it can be initialized with a value as shown above. All un-initialized static fields automatically get initialized to their default values when the class is loaded first time.

Static constructor

Static constructor can't be parameterized.

Static constructor doesn't have any access modifier because it doesn't have message passing and used during domain processing.

Static Constructor is used to initialize static data members of the class.

Static Properties

Static properties are used to get or set the value of static data members of a class.

Here is a practical demonstration of static property:

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

namespace properties_static
{
    class Program
    {
        public class PropertyClass
        {
            static string co_name;
            // Static Property
            public static string _co_name
            {
                get
                {
                    return co_name;
                }

                set
                {
                    co_name = value;
                }
            }
 
        }

        static void Main(string[] args)
        {
            PropertyClass._co_name = "George";
            Console.WriteLine(PropertyClass._co_name);
            Console.ReadLine();

        }
    }
}

Static methods

Static methods are shared methods. They can be called with the class name and static method name only. No need of instance to call static methods. 

Static methods only use static data members to perform calculation or processing.

Practical demonstration of static constructor and static methods.

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

namespace static_eg
{
    class Program
    {
        public class test
        {
            static string name;
            static int age;

            static test()
            {
                Console.WriteLine("Using static constructor to initialize static data members");
                name = "John Sena";
                age = 23;
            }
            public static void display()
            {

                Console.WriteLine("Using static function");
                Console.WriteLine(name);
                Console.WriteLine(age);
            }

        }
        static void Main(string[] args)
        {
            test.display();

            Console.ReadLine();
        }
    }
}

Static can be used at more places. Will be discussing in my later articles. Till than Cheers! Waiting for your feedback.

Up Next
    Ebook Download
    View all
    Learn
    View all