Data members, member functions,
properties and events can be declared either as static. C# includes
"static" keyword just like other programming languages such as C++,
so The Static keyword can be applied on classes, variables, methods,
properties, operators, events and constructors Only one copy of static fields
and events exists, and static methods and properties can only access static
fields and static events
The Static modifier makes an item
non-instantiable, it means the static item cannot be instantiated. If the
static modifier is applied to a class then that class cannot be instantiated
using the new keyword. If the static modifier is applied to a variable, method
or property of class then they can be accessed without creating an object of
the class
Example:-
public static class MyFirstStaticClass
{
public static int myFirstStaticVariable = 0;
public static void MyFirstStaticMethod()
{
Console.WriteLine("This is a static
method.");
}
public static int MyFirstStaticProperty { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MyFirstStaticClass.myFirstStaticVariable);
MyFirstStaticClass.MyFirstStaticMethod();
MyFirstStaticClass.MyFirstStaticProperty = 100;
Console.WriteLine(MyFirstStaticClass.MyFirstStaticProperty);
}
}
MyFirstStaticClass is a
static class with a static variable, method, and property. All the static members
can be accessed using className
Data members, member functions,
properties and events can be declared either as static. C# includes
"static" keyword just like other programming languages such as C++,
so The Static keyword can be applied on classes, variables, methods,
properties, operators, events and constructors Only one copy of static fields
and events exists, and static methods and properties can only access static
fields and static events
The Static modifier makes an item
non-instantiable, it means the static item cannot be instantiated. If the
static modifier is applied to a class then that class cannot be instantiated
using the new keyword. If the static modifier is applied to a variable, method
or property of class then they can be accessed without creating an object of
the class
Example:-
public static class MyFirstStaticClass
{
public static int myFirstStaticVariable = 0;
public static void MyFirstStaticMethod()
{
Console.WriteLine("This is a static
method.");
}
public static int MyFirstStaticProperty { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MyFirstStaticClass.myFirstStaticVariable);
MyFirstStaticClass.MyFirstStaticMethod();
MyFirstStaticClass.MyFirstStaticProperty = 100;
Console.WriteLine(MyFirstStaticClass.MyFirstStaticProperty);
}
}
|
MyFirstStaticClass is a
static class with a static variable, method, and property. All the static members
can be accessed using className