What is Static Class and what is the use of Static Classes.

What's static classes and why we use Static classes.That's a question that always confuse beginner developers.

Static Classes are those classes that can call without creating the object of classes.Like if you have one class like FareSearch   and we have a one method like: BookMyFlight that we have declared Static  .we have declared this method static Because  with the help of this we can call BookMyFlight without creating a object of FareSearch .
I will give you one example that you can see run On Visual Studio.

using System;
class FareSearch
{
public static void BookMyFlight()
{
Console.WriteLine("I Am Static Class");
}

public void GetFlights()
{
Console.WriteLine("Air India");
}
}
class EntryPointclass
{
public static void Main()
{
FareSearch.BookMyFlight(); //you can do this bcs it is static method

//FareSearch.GetFlights();  //You Can't do this it will give an error bcs it is non-static field

}
}



















Bcs When application starts all static field loaded by machine .



Static class is useful when you want to use any field and method periodically again and again in the whole application.

Comments

Popular Posts