Home > DeveloperSection > Blogs > Static class and static members in c#

Static class and static members in c#


C# C# 
Ratings:
0 Comment(s)
 2055  View(s)
Rate this:

Static class and static members in c#

In this blog I am trying to explore the concept of static classes and static members in c#.

Static class:

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework 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 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).

 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 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 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 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 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);

            }

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick