Loading, please wait...

A to Z Full Forms and Acronyms

The Static keyword in C# | C# Tutorial

Dec 21, 2020 C#, C Sharp, Shibam Dhar, 5535 Views
In C# static is something which can not be instantiated. It is a modifier which can be applicable to many things such as classes, methods, variables and constructors. When a member is declared static, it can be accessed with the name of its class directly.

The Static keyword in C# | C# Tutorial

In C# static is something that can not be instantiated. It is a modifier that can be applicable to many things such as classes, methods, variables, and constructors. When a member is declared static, it can be accessed with the name of its class directly.

Static Classes

With the static keyword, we can declare a static class. Static classes can have only static methods, static constructors, and static data members.

It isn't permitted to make objects of the static class. Static classes are fixed, which implies one can't acquire a static class from another class.

Example:

using System;

static class TutorialsLink {

                public static string Topic = "Static class";

}

public class TL {

                static public void Main()

                {

                                Console.WriteLine("Topic name is : {0} ", TutorialsLink.Topic);

                }

}

Difference between static and non-static class.

A static class is defined using the static keyword, whereas non-static doesn’t require anything like that. In static class, making objects are not allowed and the data members can be directly accessed with a class name but, in non-static class, we can create as many objects we like and data members are not directly accessible. Static class does not contain an instance constructor but non-static does.

Static Variable

With a static keyword, we can declare a static variable. At the point when a variable is declared as static, at that point, a solitary duplicate of the variable is made and shared among all objects at the class level. Static variables are gotten to with the name of the class, they don't need any object for access.

Example:

using System;


class Book{

                public static string Cover_color = "Black";

}

public class TL {

                static public void Main()

                {

                                Console.WriteLine("Color of abc book  is : {0} ", Book.Cover_color);

                }

}

Static Method

A static method is declared with the help of a static keyword. With the name of the class, the static methods are accessible. Static and non-static fields can be accessed by a static method. Static fields are directly accessed by the static method without class name whereas non-static fields require objects.

Example:

using System;


class WildLife_sanc {

                static public int a = 104;

                public static void total()

                {

                                Console.WriteLine("Total number of Wildlife sanctuaries "+ " present in India is :{0}", a);

                }

}

public class TL {

                static public void Main()

                { 

                                WildLife_sanc.total();

                }

}

Static Constructors

A static constructor is declared with the help of the static keyword. Static Constructor has to be invoked only once in the class and it has been invoked during the creation of the first reference to a static member in the class. A static constructor is initialized static fields or data of the class and to be executed only once.

Example:

using System;



class Trial {

            static Trial ()

            {

                        Console.WriteLine("Example of Static Constructor");

            }

            public Trial (int j)

            {

                        Console.WriteLine("Instance Constructor " + j);

            }

            public string trial_detail(string name, string branch)

            {

                        return "Name: " + name + " Branch: " + branch;

            }

            public static void Main()

            {

                        Trial obj = new Trial (1);

                        Console.WriteLine(obj.trial_detail("Sunil", "CSE"));

                        Trial ob = new Trial (2);

                        Console.WriteLine(ob.trial_detail("Sweta", "ECE"));

            }

}
A to Z Full Forms and Acronyms