Types of classes part-3 - Static class - DotNet and DataBase

Sunday, 15 July 2018

Types of classes part-3 - Static class




Static class:
A static class is a class that we can't instantiate or inherit. A class declared with keyword "static" called "static class" We can access the static members directly with the class name. Static class will be loaded immediately when the project get hosted into IIS.

Characteristics of a static class:
1) Static class doesn't support instance object creation.
2) It is sealed in nature.
3) We can create only one static constructor for one class.
4) static class doesn't support "protected,protected internal" access modifiers to its class members.

Syntax:
static class <static Class name>
{

}

Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StaticClass
{
    class Program
    {
        static void Main(string[] args)
        {
            staticClass.scMethod1();

        }
    }
    static class staticClass
    {
        static int a = 10;
        public static void scMethod1()
        {
            Console.WriteLine("I'm a Static class member name scMethod1");
            Console.Read();
        }
        static void scMethod2()
        {
            Console.WriteLine("I'm a Static class member name scMethod1");
            Console.Read();
        }
    }
}


No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.