---
title: "Static class and static members in c#"  
description: "In this blog I am trying to explore the concept of static classes and static members in c#.Static class:A class can be declaredstatic, indicating that"  
author: "shreesh chandra shukla"  
published: 2013-07-15  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/554/static-class-and-static-members-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 8 minutes  

---

# Static class and static members in c#

In this blog I am trying to [explore the concept](https://answers.mindstick.com/qa/112684/how-do-americans-explore-the-concept-of-hygge-and-coziness-in-their-homes) of static classes and static members in c#.

Static class:

A class can be declared [static](http://msdn.microsoft.com/en-us/library/98f28cdx(v=vs.80).aspx), indicating that it contains only static members. It is not possible to create instances of a static class using the [new](http://msdn.microsoft.com/en-us/library/51y09td4(v=vs.80).aspx) keyword. Static classes are loaded automatically by the .NET Framework [common language runtime](https://www.mindstick.com/articles/16/common-language-runtime) (CLR) when the program or namespace containing the class is loaded.

Use a static class to contain methods that are not associated with a particular object. For example, it is a common [requirement](https://yourviews.mindstick.com/view/85169/becoming-an-influencer-requirement-of-skills-and-knowledge) to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

##### The main features of a static class are:

1. They only contain static members.

2. They cannot be instantiated.

3. They are sealed.

4. They cannot contain [Instance Constructors (parameterize constructor)](http://msdn.microsoft.com/en-us/library/k6sa6h87(v=vs.80).aspx).

##### Creating static class

##### \

```
static class MyMath
         {// staic membber of class             static int num1;             static int num2;            public static int add()            {                return (num1 + num2);            }            public static int getSquare(int n)            {                 return (n * n);            }
        }
```

Illustrating static class with Example:

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace example{    class Program    {        static void Main(string[] args)        {            int result;            // calling staic method myMath static class            // so no need to create object of static class            result = MyMath.getSquare(5);            Console.WriteLine("result =" + result);            result = MyMath.getqueb(5);            Console.WriteLine("result =" + result);            result = MyMath.findMax(5,10);            Console.WriteLine("result =" + result);            result = MyMath.add();            Console.WriteLine("result =" + result);            Console.ReadKey();            //MyMath m = new MyMath();// you can not create object of static class
        }         static class MyMath         {// staic membber of class             static int num1;             static int num2;             /// <summary>             /// however you could't creaate object of static class             /// but you can create static constructor for class variabl initialisation
             /// this is called when prior to first time variable is being used.
             /// </summary>
             static MyMath()             {                 num1 = 10;                 num2 = 20;             }
             /// <summary>             /// paramerise constructor are prohibited in static clall             /// this constructure casued error at compile time.             /// </summary>             /// <param name="s"></param>             /// <param name="t"></param>
           /* static MyMath(int s, int t)             {                 num1 = s;                 num2 = t;             }*/             public static int add()            {
                return (num1 + num2);            }            public static int getSquare(int n)            {
                return (n * n);
            }            public static int getqueb(int n)            {                return (n * n * n);
            }            public static int findMax(int n, int m)            {                int max;                max = (n > m ? n : m);                return (max);            }        }    }}
```

In static class all member must be static. That is why [static method](https://www.mindstick.com/blog/440/creating-static-methods-in-c-sharp) always use static class variable. Static class instance could not be created. So in order to use static member of class you must use the [class Name](https://www.mindstick.com/forum/12871/how-to-get-class-name) to call/use members.

Like: <class name>.<member name>

Because static class couldn’t be initialized (objet not created), so static class cannot define any parameterize constructor. However you can [define static](https://answers.mindstick.com/qa/97003/define-static-ip-and-dynamic-ip) parameter less constructor to initialize the class variable, and this static constructor is called by CLR prior to call/use the constructor code(code written inside the static constructor).

##### Static members in non static class

You can also use static member in [non static](https://www.mindstick.com/interview/2733/can-you-access-non-static-variable-in-static-context) class, but you can create object of that non static class as you did in all other classes. However you can use static member of class without creating object of that class, by using the <classname.memername>.

Static method always use static member of class non static member never used by the static class regardless they resides in static class or non static class.

##### Example

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace example{    class Program    {        static void Main(string[] args)        {        // we can create object of this class because class is non static        //it doesn’t matter whether it contains static or non static member          MyMath m = new MyMath()            int result;            // calling static method myMath static class            // so no need to create object of static class            result = MyMath.getSquare(5);            Console.WriteLine("result =" + result);            result = MyMath.getqueb(5);            Console.WriteLine("result =" + result);            result = MyMath.findMax(5,10);            Console.WriteLine("result =" + result);            result = MyMath.add();            Console.WriteLine("result =" + result);            Console.ReadKey();
        }         class MyMath         {// static member of class shared by all instances of class             static int num1;             static int num2;              public MyMath()             {                 num1 = 10;                 num2 = 20;             }// static member uses static class variable num1 and num2// if they are not static they will not be used in add method.             public static int add()            {                return (num1 + num2);            }            public static int getSquare(int n)            {                 return (n * n);            }            public static int getqueb(int n)            {
                return (n * n * n);            }            public static int findMax(int n, int m)            {                int max;                max = (n > m ? n : m);
                return (max);            }
        }
    }
}
```

---

Original Source: https://www.mindstick.com/blog/554/static-class-and-static-members-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
