Working with Static Class in C#

In the static class, we access members directly

Example

//static class

    static class Class1

    {

        public static double Area1(double Width, double height)

        {

            return Width * Height;

        }

    }

    //normal class

    class Class2

    {

        public double Area2(double Width, double height)

        {

            return Width * Height;

        }

    }

 
How to use both classes.

class Program

{

    static void Main()

    {

        Class2 cc = new Class2();

        double x = cc.Area2(10, 10);

        double y = Class1.Area(10, 10); //DIrectly with class name no object

        console.writeline(x.ToString());

        console.writeline(y.ToString());

    }

}

 

Note: A static class cannot have non-static members. All methods, fields and properties in it must also be static.

Ebook Download
View all
Learn
View all