C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain instance constructors. You don’t need a private constructor to avoid instantiation if your class is static.


C# static class Example:
using System;
public static class Calculation
{
            static int square(int number)
            {
                        return number*number;
            }
            static int cube(int number)
            {
                        return number*number*number;
            }
            static void table(int number)
            {
                        for(int i=1;i<=10;i++)
                        {
                                    Console.WriteLine(number + " X "+i+" = "+number*i);
                        }
            }
            public static void Main (string[] args)
            {
                        Console.WriteLine("The Square is "+ Calculation.square(45));
                        Console.WriteLine("The Cube is "+Calculation.cube(46));
                        Calculation.table(50);
            }         
}
Output
The Square is 2025
The Cube is 97336
50 X 1 = 50
50 X 2 = 100
50 X 3 = 150
50 X 4 = 200
50 X 5 = 250
50 X 6 = 300
50 X 7 = 350
50 X 8 = 400
50 X 9 = 450
50 X 10 = 500



ADVERTISEMENT
ADVERTISEMENT