static is a modifier in C# which is applicable for the following:
It is also applicable to properties, event, and operators. To create a static member(class, variable, methods, constructor), precede its declaration with the keyword static. When a member is declared static, it can be accessed with the name of its class directly.
Static Class
A static class is declared with the help of static keyword. A static class can only contain static data members, static methods, and a static constructor. It is not allowed to create objects of the static class. Static classes are sealed, means one cannot inherit a static class from another class.
Example:
C#
// C# program to illustrate the
// concept of a static class
using System;
// Creating static class
// Using static keyword
static class Tutorial {
// Static data members of Tutorial
public static string Topic = "Static class";
}
// Driver Class
public class GFG {
// Main Method
static public void Main()
{
// Accessing the static data members of Tutorial
Console.WriteLine("Topic name is : {0} ", Tutorial.Topic);
}
}
Output:
Topic name is : Static class
Static Variable
A static variable is declared with the help of static keyword. When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. Static variables are accessed with the name of the class, they do not require any object for access.
Example:
C#
// C# program to illustrate the
// concept of static variable
using System;
class Vehicle {
// Creating static variable
// Using static keyword
public static string Model_color = "Black";
}
// Driver Class
public class GFG {
// Main Method
static public void Main()
{
// Accessing the static variable
// using its class name
Console.WriteLine("Color of XY model is : {0} ",
Vehicle.Model_color);
}
}
Output:
Color of XY model is : Black
Static Method
A static method is declared with the help of the static keyword. Static methods are accessed with the name of the class. A static method can access only static fields and methods directly, while non-static fields and methods require an instance of the class to be accessed.
Example:
C#
// C# program to illustrate the
// concept of static method
using System;
class Nparks {
static public int t = 104;
// Creating static method
// Using static keyword
public static void total()
{
Console.WriteLine("Total number of national parks"+
" present in India is :{0}", t);
}
}
// Driver Class
public class GFG {
// Main Method
static public void Main()
{
// Accessing the static method
// using its class name
Nparks.total();
}
}
Output:
Total number of national parks present in India is :104
Static Constructor
A static constructor is declared with the help of 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.
Points To Remember:
- It can’t be called directly.
- When it is executing, then the user has no control.
- It does not take access modifiers or any parameters.
- It is called automatically to initialize the class before the first instance created.
Example:
C#
// C# Program to illustrate calling
// a Static constructor
using System;
class G1 {
// It is invoked before the first
// instance constructor is run.
static G1()
{
// The following statement produces
// the first line of output,
// and the line occurs only once.
Console.WriteLine("Example of Static Constructor");
}
// Instance constructor.
public G1(int j)
{
Console.WriteLine("Instance Constructor " + j);
}
// Instance method.
public string g1_detail(string name, string branch)
{
return "Name: " + name + " Branch: " + branch;
}
// Main Method
public static void Main()
{
// Here Both Static and instance
// constructors are invoked for
// first instance
G1 obj = new G1(1);
Console.WriteLine(obj.g1_detail("Sunil", "CSE"));
// Here only instance constructor
// will be invoked
G1 ob = new G1(2);
Console.WriteLine(ob.g1_detail("Sweta", "ECE"));
}
}
Output:
Example of Static Constructor
Instance Constructor 1
Name: Sunil Branch: CSE
Instance Constructor 2
Name: Sweta Branch: ECE
Limitation of using static keyword:
- static keyword cannot be used by indexers, finalizers, or types other than classes.
- A static member is not referenced through an instance.
- In C#, it is not allowed to use this to reference static methods or property accessors.
- In C#, if static keyword is used with the class, then the static class always contain static members.
Similar Reads
short keyword in C#
Keywords are the words in a language that are used for some internal process or represent some predefined actions. short is a keyword that is used to declare a variable which can store a signed integer value from the range -32, 768 to 32, 767. It is an alias of System.Int16. Syntax: short variable_n
2 min read
sbyte Keyword in C#
Keywords are the words in a language that are used for some internal process or represent some predefined actions. SByte is a keyword that is used to declare a variable that can store a signed value between the range of -128 to +127. It is an alias of System.SByte. SByte keyword occupies 1 byte (8 b
2 min read
var keyword in C#
Keywords are the words in a language that are used for some internal process or represent some predefined actions. var is a keyword, it is used to declare an implicit type variable, that specifies the type of a variable based on initial value. Syntax: var variable_name = value; Example: Input: a = 6
2 min read
ushort keyword in C#
Keywords are the words in a language that are used for some internal process or represent some predefined actions. ushort is a keyword that is used to declare a variable which can store an unsigned integer value from the range 0 to 65,535. It is an alias of System.UInt16. Syntax: ushort variable_nam
2 min read
uint keyword in C#
Keywords are the words in a language that are used for some internal process or represent some predefined actions. uint is a keyword that is used to declare a variable which can store an integral type of value (unsigned integer) from the range of 0 to 4,294,967,295. It keyword is an alias of System.
2 min read
void Keyword in C#
Keywords are the words in a language that are used for some internal process or represent some predefined actions. void is a keyword, it is a reference type of data type and used to specify the return type of a method in C#. It is an alias of System.Void. Syntax: public void function_name([parameter
2 min read
C# | this Keyword
this keyword is used to refer to the current instance of the class. It is used to access members from the constructors, instance methods, and instance accessors. this keyword is also used to track the instance which is invoked to perform some calculation or further processing related to that instanc
3 min read
long keyword in C#
Keywords are the words in a language that are used for some internal process or represent some predefined actions. long is a keyword that is used to declare a variable which can store a signed integer value from the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. It is an alias of
2 min read
ulong keyword in C#
Keywords are the words in a language that are used for some internal process or represent some predefined actions. ulong is a keyword that is used to declare a variable which can store an unsigned integer value from the range 0 to 18,446,744,073,709,551,615. It is an alias of System.UInt64. ulong ke
2 min read
C# Keywords
Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error.Example:C#// C# Program to illustrate the
5 min read