Static class can't be instantiated.
- It is declared using the static keyword.
- The new operator can't be used to create an object of the static class.
- Class members are directly accessed using the class name.
- It is used to define standard operations that just need input parameters, e.g. Math.Round(7.35).
- It contains only static members.
- It is sealed.
- It can have static constructor, but can't have instance constructor.
Example
namespace MyConsoleApp
{
public static class WeightConverter
{
public static double PoundToKilogram(string weight)
{
double kilogram = Double.Parse(weight);
double pound = kilogram / 2.20462;
return pound;
}
public static double KilogramToPound(string weight)
{
double pound = Double.Parse(weight);
double kilogram = pound * 2.20462;
return kilogram;
}
}
}
Execute
namespace MyConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please choose a converter");
Console.WriteLine("1. Pound to Kilogram.");
Console.WriteLine("2. Kilogram to Pound.");
Console.Write(":");
string converter = Console.ReadLine();
double lb, kg = 0;
switch (converter)
{
case "1":
Console.Write("Enter the weight in pound: ");
kg = WeightConverter.PoundToKilogram(Console.ReadLine());
Console.WriteLine("Weight in kilogram: {0:F2}", kg);
break;
case "2":
Console.Write("Enter the weight in kilogram: ");
lb = WeightConverter.KilogramToPound(Console.ReadLine());
Console.WriteLine("Weight in pound: {0:F2}", lb);
break;
default:
Console.WriteLine("Please choose a converter.");
break;
}
}
}
}
/* OUTPUT
Please choose a converter
1. Pound to Kilogram.
2. Kilogram to Pound.
:1
Enter the weight in pound: 150
Weight in pound: 68.04
*/