DEV Community

loading...
Cover image for C#  Static Class

C# Static Class

aryanabsalan profile image Aryan Absalan ・4 min read

In C#, static means something which cannot be instantiated. You cannot create an object of a static class and cannot access static members using an object.

C# classes, variables, methods, properties, operators, events, and constructors can be defined as static using the static modifier keyword.

Static Class

A static class cannot be instantiated. All members of a static class are static and are accessed via the class name directly, without creating an instance of the class.

The following code is an example of a static class, CSharpCorner. We know that all members of the class are static.
public static class CSharpCorner

{

// Static fields

public static string Name = "C# Corner";

public static string Founder = "Mahesh Chand";

public static DateTime YearFounded = new DateTime(2000, 01, 01);

public static string Location = "Downingtown, PA";

public static string Description =

"Online community of software and data developers";

public static int GetAgeOfWebsite()

{

return 1;

}

}

Static classes have the following characteristics:
Static classes cannot contain Instance Constructors.
Static classes contain only static members.
Static classes cannot be instantiated.
Static classes are sealed. That means, you cannot inherit other classes from instance classes.
You can learn more about static classes here: Static Class in C#

Static Members

A static or non-static class static constructors, properties, methods, fields, operators, and events. Static properties and static methods are the most-used static members.

Static Constructor

Static constructor can't be parameterized.
Static constructor doesn't have any access modifier because it doesn't have message passing and is used during domain processing.
Static Constructor is used to initialize static data members of the class.

Static Field

A field with the static keyword represents a static field. Static fields can be declared as follows by using the static keyword.
public static class HistoryTeacher

{

// static field

public static string Subject = "History";

}

The following code calls a static field.
Console.WriteLine(HistoryTeacher.Subject);

When we declare static data members inside a class, it can be initialized with a value as shown above. All un-initialized static fields automatically get initialized to their default values when the class is loaded for the first time.

Static Property

Static properties are used to get or set the value of static data members of a class. The following code example declares four static properties.
public static class HistoryTeacher

{

// private fields

private static string name;

private static string school;

private static int rank;

private static int years;

// static properties

public static int Years { get => years; set => years = value; }

public static int Rank { get => rank; set => rank = value; }

public static string School { get => school; set => school = value; }

public static string Name { get => name; set => name = value; }

}

The following code example sets static property values.
HistoryTeacher.Name = "Mahesh Chand";

HistoryTeacher.Rank = 2;

HistoryTeacher.School = "Garnet Valley High School";

HistoryTeacher.Years = 5;

In the same way, you can access a static property by using the class name.
Console.WriteLine(HistoryTeacher.Name);

Static Method

Static methods are shared methods. They can be called with the class name and static method name only. You cannot instantiate a static method.

Static methods only use static data members to perform calculation or processing.

The following code example declares a static method that takes two int values as method arguments.
public static class HistoryTeacher

{

// static method

public static int CalculateScore(int rank, int years)

{

return rank * years;

}

}

The following code example calls a static method.
Console.WriteLine(HistoryTeacher.CalculateScore(3, 5));

Why use static classes and static members?

Static classes are used as containers for static members. Static methods and static properties are the most-used members of a static class. All static members are called directly using the class name. Static methods do a specific job and are called directly using a type name, rather than the instance of a type.

Here is a list of few use cases of static classes.
A Math class with all static methods. Static classes are useful and provide an easy way to access its members that does not need to work differently for different objects.
Above listed CSharpCorner class. We know the value of CSharpCorner class members such as its founder, launch date, location, and description, will never change regardless of its objects.
App Configuration class that has all static settings about an app and the values of settings don’t change based on the objects or users.
A DatabaseConfig class that may have members such as database name, server name, port number, and even a connection string. We know that these values will not change for objects.
Static classes and static members are useful because they do not require instances created for each new object. That means, they consume fewer resources and no duplication of the same class or member is needed in memory.

Static members make code cleaner.

In theory, a static should give better performance compare to an instance. However, it is unnoticeable.

Complete Code Example

Here is a complete program that shows how to use static class and static members.
using System;

namespace StaticInCSharp

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(HistoryTeacher.Subject);

HistoryTeacher.Name = "Mahesh Chand";

HistoryTeacher.Rank = 2;

HistoryTeacher.School = "Garnet Valley High School";

HistoryTeacher.Years = 5;

Console.WriteLine(HistoryTeacher.Name);

Console.WriteLine(HistoryTeacher.CalculateScore(3, 5));

Console.ReadKey();

}

}

public static class HistoryTeacher

{

// static field

public static string Subject = "History";

// private fields

private static string name;

private static string school;

private static int rank;

private static int years;

// static properties

public static int Years { get => years; set => years = value; }

public static int Rank { get => rank; set => rank = value; }

public static string School { get => school; set => school = value; }

public static string Name { get => name; set => name = value; }

// static method

public static int CalculateScore(int rank, int years)

{

return rank * years;

}

}

}

Discussion

pic
Editor guide