Learning Management Platform for Written Tutorial Series.

C# Static Class

C# Static Class

Introduction

A static class is a class that cannot be initialized. This means you cannot create instance variables of the class. C# does not support global scope variables. One of the ways that you can use to define something similar to global scope is by using public static class with public static properties.

C# Static Class Syntax

static classes are declared using the static keyword. The following code demonstrates how to define a static class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpStaticClass
{
    public static class MyStaticClass
    {

    }
}

HERE,

  • they keyword static is used to declare the class as static.

Rules for Static Classes

  • A static class cannot be private, protected or sealed
  • A static class cannot implement a non static property or method
  • A static class cannot be derived from

C# Static class example (Global Variables)

The following code defines a static class with public static properties that can be used as global variables in a C# program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpStaticClass
{
    static class GlobalVariables
    {
        public static string sCompanyName = string.Empty;
        public static string sSlogan = string.Empty;
        public static string sPOBox = string.Empty;
        public static string sTown = string.Empty;
        public static string sTelephone = string.Empty;
        public static string sEmail = string.Empty;
    }
}

HERE,

  • public static string sCompanyName = string.Empty; declares a public static property that can be access by using GlobalVariables.sCompanyName

C# Static class methods

We will now add a public static method to GlobalVariables

public static void setValues()
{
    sCompanyName = "Kode Blog";
    sSlogan = @"\m/ free programming tutorials \m/";
    sPOBox = "P.O. Box 408, Babati, Tanzania";
    sTown = "Babati";
    sTelephone = "911";
    sEmail = "a-team@kode-blog.com";
}

The above method defines a public method setValues that sets the values for the public static properties. In a real world application, these values can be retrieved from the database.

How to use public static properties and methods

Open Program.cs

Modify the code to the following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpStaticClass
{
    class Program
    {
        static void Main(string[] args)
        {
            GlobalVariables.setValues();

            Console.WriteLine("Company Name: {0}", GlobalVariables.sCompanyName);
            Console.WriteLine("Slogan: {0}", GlobalVariables.sSlogan);
            Console.WriteLine("P.O. Box: {0}", GlobalVariables.sPOBox);
            Console.WriteLine("Town: {0}", GlobalVariables.sTown);
            Console.WriteLine("Telephone: {0}", GlobalVariables.sTelephone);
            Console.WriteLine("Email: {0}", GlobalVariables.sEmail);

            Console.ReadLine();
        }
    }
}

Press F5 on the keyboard

You will get the following results.

Company Name: Kode Blog
Slogan: \m/ free programming tutorials \m/
P.O. Box: P.O. Box 408, Babati, Tanzania
Town: Babati
Telephone: 911
Email: a-team@kode-blog.com

Summary

  • Static classes cannot be initialized
  • Static classes can be used to define global variables in C#
  • All methods and properties in a static class must be defined as static

Tutorial History

Tutorial version 1: Date Published 2015-08-17


...