• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Dotnet Stuff

  • Home
  • Articles
  • Tutorials
  • Forums
  • Career Advice
  • Jobs
  • .NET FAQs
  • News
  • Privacy Policy

Static Classes in C# and Static Class Members In C#

As a programmer, you may have used Static Classes and Static Class Members in C# or other programming languages.

Static member variables and static functions of a normal class can be accessed without creating an instance of the class. But the respective classes are not required to be static.

See  below a sample with static member variables and Static functions in C#

//non-static class containing static member variables
class NormalClass
{
    //static member variable
    private static intnumber;

    //a static member function in C#
    public static int Add(intx,inty)
    {
       number = x+y;
       return number;
    }
     
    public int Substract(intx,inty)
    {
       number = x-y;
       return number;
    }
}

As you saw in the above sample, the function Add is static and can be accessed without an instance variable of the class.We can access static members(variables and functions) of the class by using ‘.’ operator.

The function Substract is an instance function and can be accessed only through an instance of the class as below,

NormalClass.Add(5, 10);
NormalClass class1 = new NormalClass();
class1 .Substarct(5,10);

Table of Contents

  • 1 What is the Benefit of the Static class
    • 1.1 Static classes in C# Sample
    • 1.2 Summary

What is the Benefit of the Static class

What is a Static Class & What Difference will a Static Class make?

Static classes are classes declared with the static keyword.

public static class StaticSampleClass

The main difference is that A static class can not be instantiated.

For example, think about a utility class which contains only utility functions.Utility functions are stateless in nature and hence no need of a class object to access it.

Hence Utility classes are always the best candidate for static classes.This avoids unnecessary instance creation, no need of disposing and also simple to use with reduced lines of code.

Static classes in C# Sample

See an example of a static class in C# below,

static class Utility
{
    //static member variable
    private static int number;

    private int nonStatic;    //wrong- a static class can not have non static mmembers

    //C# static member function
    internal static int Add(int x,int y)
    {
       number = x+y; // can access static variables or functions.
       nonStatic = x+ y; // wrong- variable nonStatic is not accessible
       return number;
     }

    //C# static member function
    internal static int Substract(int x,int y)
    {
       return x-y;
    }

    // static member function in C#
    internal static int Divide(int x,int y)
    {
       return x/y;
    }
}

The internal static method of the static Class Utility can be accessed from outside of the class, simply as below

Utility.Add(20,10);
Utility.Substract(20,10);
Utility.Divide(20,10);

Static classes can have only static members.static methods can access only static variables or functions.

Static classes in C# are by default internal to the project.No need to explicitly mention as internal. But if you want to access static class members outside of the project you need to use the public keyword explicitly as shown be

public static class Utility
{
   publicstaticintAdd(intx,inty)
   {
      returnx+y;
   }

   publicstaticintSubstract(intx,inty)
   {
      returnx-y;
   }

   publicstaticintDivide(intx,inty)
   {
      returnx/y;
   }
}

Summary

In this post, we covered Static classes in C# and how static member variables and static member functions are declared and used in C#.Hope you find this post useful. Comment your valuable feedback below.

Happy Coding!

You may like to read Static Class Vs Singleton

Related Posts:

  • Differences Between Finalize and Dispose Methods in C# and VB.NET
    Differences Between Finalize and Dispose Methods in…
  • C# 2.0, Covariance And Contravariance In Delegates
    C# 2.0, Covariance And Contravariance In Delegates
  • C# Version History | C# Evolution and Features
    C# Version History | C# Evolution and Features

Related posts:

  1. Difference Between Static Class and Singleton Pattern in C#?
  2. How to add text in a created table in a richtextbox?
  3. JSON Tutorial – Learn JSON Quickly
  4. Properties With Multiple Access Modifiers in C# 2.0 | Asymmetric Accessor Accessibility

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?
Log In
Increase Your Leadership Skills, Influence, and Power with Top Leadership courses starting at just $11.99.

Recent Posts

  • .NET Core Version History | Release History of .NET Core
  • What is string interpolation in C#
  • What Is The Use Of Volatile Keyword In C#
  • Get System IP Address Using C#
  • SQL Server Interview Question and Answers
  • C# Fixed Size Buffer
  • OWIN in ASP.NET Core| What is OWIN in .NET?
  • C# Version History | C# Evolution and Features
  • What is ASP.NET Core Module
  • C# 3d array

Recent Forum Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?

Forums

  • .NET Framework
  • ASP.NET
  • C#
  • SQL

Recent Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?

© Copywright 2017 Dotnetstuffs All Rights Reserved