TechyMedia

A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer

NAVIGATION - SEARCH

Static Classes And Static Class Members In C#

A static class can not be instantiated.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 as static classes.This avoids unnecessary instance creation,no need to dispose and simplicity of usage with reduced lines of code.

Static members(data and functions) of a normal class can be accessed without creating an instance of the class.But here the class not mandatory to be static.

class NormalClass
{
    private static int number;

    public static int Add(int x,int y)
    {
        number = x+y;
        return number;
    }

    public int Substract(int x,int y)
    {
        number = x-y;   
        return number;
    }

}


In the given sample above function Add is static and can be accessed without crating an instance of the class.We can access static members of the class directly by using '.' operator.
But the function Substract is an instance function(can access both static and non static members) and can be accessed only through an instance of the class.Usage is

NormalClass.Add(5, 10);

NormalClass class1 = new NormalClass();

class1 .Substarct(5,10);

Now what difference static class will make?

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 as static classes.This avoids unnecessary instance creation,no need to dispose and simplicity of usage with reduced lines of code.

See an example of static class below

static class Utility
{
    private static int number;
    //private int nonStatic;    -- this is wrong
   
    internal static int Add(int x,int y)
    {
        number = x+y; // can access static variables or functions.
        //nonStatic= x+ y;    --  variable nonStatic is not accessible
        return number;
    }

    internal static int Substract(int x,int y)
    {
        return x-y;
    }
    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 class within the project 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 a static class members outside of the project you need to use the public keyword explicitly as shown be

public static class Utility
{
    public static int Add(int x,int y)
    {
        return x+y;
    }

     public static int Substract(int x,int y)
     {
         return x-y;
     }

     public static int Divide(int x,int y)
     {
         return x/y;
     }
}

Static Class Vs Singleton

 

Partial Classes

 

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading