Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
static
classes

What is a static class in C#?

Educative Answers Team

In C#, a static class is created with the keyword static; static classes cannot be inherited, they only contain static data members and static methods.

1. Static data members

If a variable is static, then the variable is assigned memory once. All the objects of the class have shared access to the same variable.

A static data member can be created by adding the static keyword before the variable during declaration.

Syntax

The general syntax for declaring a static data member is:

public static int dataMember;
svg viewer

2. Static methods

Static methods exist in static classes and are declared using the static keyword. These methods are used to access static data members. They cannot access non-static data members.

Syntax

public static void staticMethod()
{
  this.dataMember++;      
}

As you can see, the staticMethod can only access and alter the static variable dataMember.

Static class overall Syntax

static class Educative
{
  static int dataMember;
  public static void staticMethod();
}

RELATED TAGS

c#
static
classes
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring