封裝與物件狀態無關的純功能方法,避免不必要的實例化:
static class CompanyInfo {
public static string GetName() => "CompanyName";
public static string GetAddress() => "CompanyAddress";
}
// 呼叫
string name = CompanyInfo.GetName();
new)sealed,不可繼承public class Automobile {
public static int NumberOfWheels = 4;
public static int GasTankSize { get; } = 15;
public static void Drive() { }
public static event EventHandler RunOutOfGas;
}
// 呼叫
Automobile.Drive();
int wheels = Automobile.NumberOfWheels;