C# static class

Leave a Comment
The C# static class is like the normal class but it cannot be instantiated. It can have only static members. The advantage of static class is that it provides you guarantee that instance of static class cannot be created.

Points to remember for C# static class

  • C# static class contains only static members.
  • C# static class cannot be instantiated.
  • C# static class is sealed.
  • C# static class cannot contain instance constructors.

C# static class example

Let's see the example of static class that contains static field and static method.
  1. using System;  
  2.    public static class MyMath  
  3.     {  
  4.         public static float PI=3.14f;   
  5.         public static int cube(int n){return n*n*n;}  
  6.     }  
  7.    class TestMyMath{  
  8.        public static void Main(string[] args)  
  9.         {  
  10.             Console.WriteLine("Value of PI is: "+MyMath.PI);  
  11.             Console.WriteLine("Cube of 3 is: " + MyMath.cube(3));  
  12.         }  
  13.     }  
Value of PI is: 3.14
Cube of 3 is: 27


Post a Comment

Note: only a member of this blog may post a comment.