A
static class is the same as a non-static class, but a static class cannot be
instantiated. In other words, you cannot use the new keyword to create an
object of the class type. The static keyword in the C# programming language
allows you to define static classes and static members. A static class is
similar to a class that is both abstract and sealed. To declare a class as
static, you should mark it with the static keyword in the class declaration.
Syntax of Static Class
static class
classname
{
//static
data members
//static
methods
}
|
Example of Static Class
using System;
namespace StaticClassExampleNameSpace
{
public static class StaticClassExamleClass
{
public static double CelsiusToFahrenheit(string temperatureCelsius)
{
// Convert argument to double for calculations.
double celsius = Double.Parse(temperatureCelsius);
// Convert Celsius to Fahrenheit.
double fahrenheit = (celsius * 9 / 5) + 32;
return fahrenheit;
}
public static double FahrenheitToCelsius(string temperatureFahrenheit)
{
// Convert argument to double for calculations.
double fahrenheit = Double.Parse(temperatureFahrenheit);
// Convert Fahrenheit to Celsius.
double celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
}
}
|