In C#, a static is a keyward that is used with fields, methods, properties and classes when we make these members static they are globally accessble in all over the application.
Static fields are those fields that are accessible all over the application. When you want to access a field in all over the application with out creating instances of class we make it static.so static data members are declared using static keyword and they are directly accessed by using the class name.
Here, in below example of static type field
public - Here, below public is access modifier.
static - static is keword that is used to declare and initialized static fields
int - int is data type
fruitPrice - fruitPrice is a vairable name
Here, in below example of accessing static type field
int value = Fruit.fruitPrice; - In this code, Fruit is class name that we directly used for accesssing the static field (fruitPrice) then string it into int type vairable
// Access the static field fruitPrice
// Access the static field fruitPrice
A Static method is also accessible all over the application globally. When we want that a method will be accessible in all over the application with creating the instance of class then we make it static.
Here, in below example of static method
public - Here, below public is access modifier.
static - static is keword that is used to declare and initialized static method.
int - int is data type
Add(int a, int b) - Add(int a, int b) have two int type variable that he is receving and making sum of two number.
Here, in below example of call static method
int result = MathUtils.Add(5, 3); - In this code, MathUtils is class name that we directly used for calling the static method Add(5, 3).
// Call the static method
// Call the static method
Static methods can have access modifiers (e.g., public, private) that determine their visibility outside the class
Static methods are not polymorphic. You cannot override a static method in a derived class; however, you can hide it by declaring a static method with the same name.
Inside a static method, you cannot use the this keyword since there is no instance context.
You can call static methods using the class name, or from other static methods within the same class without needing to specify the class name.
Static methods cannot access instance variables or instance methods directly. They can only interact with static members.
Static properties provide access to data that is associated with the class itself globally. They are used similarly to static fields but can have custom getter and setter logic.
Here, in below example of static data member
private - Here, below public is access modifier.
static - static is keword that is used to declare and initialized static fields
string - string is data type
_fruitName; - _fruitName; is a local variable name
Here, in below example of static properties
private - Here, below public is access modifier.
static - static is keword that is used to declare and initialized static fields
string - string is data type
FruitName; - FruitName is a name of property
get { return _fruitName;} - FruitName is a getting the value of _fruitName name variable.
set { _fruitName = value; } - FruitName is setting the value of _fruitName name variable.
Here, in below example of accessing and setting the static property
Fruit.FruitName = "Mango"; - In this code, We are setting the FruitName property
string fruitname = Fruit.FruitName; - In this code, We are getting the value of FruitName property and storing it into string _fruitname variable.
// Access and set the static property
// Access and set the static property
A non-static class can contain a zero-argument static constructor. It can be defined with the static keyword and without access modifiers like public, private, and protected.
Here, in below example of static type field
static - static is keword that is used to declare and initialized static constructor.
Fruit() - int is a name of constructor.
// static constructor
// Initialization code for static members
// instance constructor
// Initialization code for non-static members
// static constructor
// Initialization code for static members
// instance constructor
// Initialization code for non-static members
Above, the non-static class Fruit contains a static constructor and also a non-static constructor. When first time the instance of Fruit class will create then static constructor will be called only once time. But when we will create 2nd time intance of Fruit class then static constructor willl not call and only non-static constructor will call
// First static constructor and then instance constructor called
// only instance constructor called
// First static constructor and then instance constructor called
// only instance constructor called
A static constructor is defined with the static keyword and does not have any parameters.
It is called automatically before any static members are accessed or any instance of the class is created.
The static constructor is called only once per type, regardless of how many instances of the class are created.
They cannot take parameters, which means they can’t be overloaded.
If a derived class has a static constructor, the base class's static constructor is called first.
Static keyword is used to defines static class and it can not be instantiated. Set the static modifier before the class name and after the access modifier to make a class static.
Here, in below example of static classes
public - Here, below public is access modifier.
static - static is keword that is used to declare and initialized static fields
void - void it does not have any return type.
FruitColor() - FruitColor() is a static method.
Here, in below example of accessing static class method
Fruit.FruitColor(); - In this code, Fruit is static class that we directly used for accesssing the static FruitColor() method
// Usage of a static class method
// Usage of a static class method
A static class must be declared using the static keyword.
All members (methods, properties, fields) of a static class must also be static.
Static classes cannot be instantiated; you cannot create an object of a static class.
Static classes cannot contain instance members (non-static fields or methods).
A static class can contain static methods, which can be called without creating an instance.
A static class can have a static constructor for initialization purposes, which is called automatically before any static members are accessed.
Static classes are sealed class and therefore, cannot be inherited. A static class cannot inherit from other classes.
In below example, we have static Fruit class which have FruitPrice, FruitName and FruitIsInMarket static data members respectively. We have a AddFruit static method. We also have a property with named of FruitSession. We have Fruit() a static constructor. Fruit.FruitName is syntax of accessing staitc data member of static class. Fruit.AddFruit(10, 35); is syntax of calling static method inside static class.
Fruit.FruitSession = "Summer"; is syntax of assinging a value to static propery of static class.
// Access the static field
// Call the static method
// Access and set the static property
// Call the static method
// Static Fields
// Static Method
// Static Property
// Static Constructor
// Initialization code for static members
// Access the static field
// Call the static method
// Access and set the static property
// Call the static method
// Static Fields
// Static Method
// Static Property
// Static Constructor
// Initialization code for static members
When we use keyword static with class it means that this class, and it's methods, properties, fields, constructor and destructor are accessible globally in whole application.