C# Interview Question #28
What is the static keyword in C#?
Classes, Constructors & Properties Junior Beginner
Quick Interview Answer
A static member belongs to the type itself rather than to an individual object instance. It can therefore be accessed through the type name without creating an object.
Detailed Explanation
A static member belongs to the type itself rather than to an individual object instance. It can therefore be accessed through the type name without creating an object.
Static fields are shared by all instances of the type. A static class cannot be instantiated and can contain only static members.
Static functionality is useful for stateless helper operations and extension methods. Mutable static state should be used carefully in server applications because it is shared and can introduce concurrency and lifetime problems.
Code Example
public static class MathHelper
{
public static int Square(int number)
{
return number * number;
}
}
int result = MathHelper.Square(5);