1. Static class
1.1 Introduction
- Static classes and class members are used to create data and functions that can be accessed without the need to create class instances.
- Static class members can be used to separate data and behavior independent of any object identifier: No matter what changes occur to the object, the data and functions will not change.
- When the class does not depend on the data or behavior of object identifiers, you can use static classes.
Features 1.2
- They only contain static members.
- They cannot be instantiated.
- They are sealed.
- They cannot contain instance Constructors (C # programming guide ).
1.3 others
- Static classes are sealed and therefore cannot be inherited.
- A static class cannot contain constructor, but can still declare a static constructor to assign an initial value or set a static state.
1.4 instance
static class CompanyInfo
{
public static string GetCompanyName() { return "CompanyName"; }
public static string GetCompanyAddress() { return "CompanyAddress"; }
//...
}
2. Static class members
2.1 Introduction
- Use the static keyword to declare a static class member before the return type of the member.
- You can call static methods, fields, attributes, or events in the class even if no instance of the class is created.
Features 2.2
- If any instance of this class is created, the instance cannot be used to access static members.
- Only one copy of static fields and events exists. Static methods and attributes can only access static fields and static events.
- Static members are usually used to represent data or calculations that do not change with the object state. For example, a mathematical library may contain static methods used to calculate the sine and cosine.
- Static members are initialized before they are accessed for the first time and before any static Constructor (for example, called.
- To access a static class member, use the class name instead of the variable name to specify the position of the Member.
2.3 instance
Code: static class member Definition
public class Automobile
{
public static int NumberOfWheels = 4;
public static int SizeOfGasTank
{
get
{
return 15;
}
}
public static void Drive() { }
public static event EventType RunOutOfGas;
//other non-static fields and properties...
}
Usage:
Automobile.Drive();
int i = Automobile.NumberOfWheels;
3. An instance that uses static and static class members
The following is an example of a static class, which contains two methods to perform back-and-forth conversion between the Celsius and Fahrenheit temperatures:
Code
public static class TemperatureConverter
{
public static double CelsiusToFahrenheit(string temperatureCelsius)
{
// Convert argument to double for calculations.
double celsius = System.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 = System.Double.Parse(temperatureFahrenheit);
// Convert Fahrenheit to Celsius.
double celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
}
class TestTemperatureConverter
{
static void Main()
{
System.Console.WriteLine("Please select the convertor direction");
System.Console.WriteLine("1. From Celsius to Fahrenheit.");
System.Console.WriteLine("2. From Fahrenheit to Celsius.");
System.Console.Write(":");
string selection = System.Console.ReadLine();
double F, C = 0;
switch (selection)
{
case "1":
System.Console.Write("Please enter the Celsius temperature: ");
F = TemperatureConverter.CelsiusToFahrenheit(System.Console.ReadLine());
System.Console.WriteLine("Temperature in Fahrenheit: {0:F2}", F);
break;
case "2":
System.Console.Write("Please enter the Fahrenheit temperature: ");
C = TemperatureConverter.FahrenheitToCelsius(System.Console.ReadLine());
System.Console.WriteLine("Temperature in Celsius: {0:F2}", C);
break;
default:
System.Console.WriteLine("Please select a convertor.");
break;
}
}
}
Input
2
98.6
Output:
Please select the convertor1. From Celsius to Fahrenheit.2. From Fahrenheit to Celsius.: 2Please enter the Fahrenheit temperature: 98.6
Temperature in Celsius: 37.00