1.static修饰符
本页介绍 static
修饰符关键字。 static
关键字也是 using static
指令的一部分。
使用 static
修饰符可声明属于类型本身而不是属于特定对象的静态成员。 static
修饰符可用于声明 static
类。 在类、接口和结构中,可以将 static
修饰符添加到字段、方法、属性、运算符、事件和构造函数。 static
修饰符不能用于索引器或终结器。 有关详细信息,请参阅静态类和静态类成员。
从 C# 8.0 开始,可以将 static
修饰符添加到本地函数。 静态本地函数无法捕获局部变量或实例状态。
从 C# 9.0 开始,可将 static
修饰符添加到 Lambda 表达式或匿名方法。 静态Lambda 表达式或匿名方法无法捕获局部变量或实例状态。
(1).静态类
下面的类声明为 static
并且只含 static
方法:
static class CompanyEmployee
{
public static void DoSomething() { /*...*/ }
public static void DoSomethingElse() { /*...*/ }
}
常数或类型声明是隐式的 static
成员。 不能通过实例引用 static
成员。 然而,可以通过类型名称引用它。 例如,请考虑以下类:
public class MyBaseC
{
public struct MyStruct
{
public static int x = 100;
}
}
若要引用 static
成员 x
,除非可从相同范围访问该成员,否则请使用完全限定的名称 MyBaseC.MyStruct.x
Console.WriteLine(MyBaseC.MyStruct.x);
若要演示 static
成员,请考虑表示公司员工的类。 假定此类包含计数员工的方法和存储员工人数的字段。 方法和字段均不属于任何一个员工实例。 相反,它们属于全体员工这个类。 应将其声明为该类的 static
成员。
(2).静态字段和方法
此示例读取新员工的姓名和 ID,员工计数器按 1 递增,并显示新员工信息和新员工人数。 此程序从键盘读取员工的当前人数。
public class Employee4
{
public string id;
public string name;
public Employee4()
{
}
public Employee4(string name, string id)
{
this.name = name;
this.id = id;
}
public static int employeeCounter;
public static int AddEmployee()
{
return ++employeeCounter;
}
}
class MainClass : Employee4
{
static void Main()
{
Console.Write("Enter the employee's name: ");
string name = Console.ReadLine();
Console.Write("Enter the employee's ID: ");
string id = Console.ReadLine();
// Create and configure the employee object.
Employee4 e = new Employee4(name, id);
Console.Write("Enter the current number of employees: ");
string n = Console.ReadLine();
Employee4.employeeCounter = Int32.Parse(n);
Employee4.AddEmployee();
// Display the new information.
Console.WriteLine($"Name: {e.name}");
Console.WriteLine($"ID: {e.id}");
Console.WriteLine($"New Number of Employees: {Employee4.employeeCounter}");
}
}
/*
Input:
Matthias Berndt
AF643G
15
*
Sample Output:
Enter the employee's name: Matthias Berndt
Enter the employee's ID: AF643G
Enter the current number of employees: 15
Name: Matthias Berndt
ID: AF643G
New Number of Employees: 16
(3).静态初始化
此示例演示了如何使用尚未声明的 static
字段来初始化另一个 static
字段。 在向 static
字段显式赋值之后才会定义结果。
class Test
{
static int x = y;
static int y = 5;
static void Main()
{
Console.WriteLine(Test.x);
Console.WriteLine(Test.y);
Test.x = 99;
Console.WriteLine(Test.x);
}
}
2.值类型
值类型和引用类型是 C# 类型的两个主要类别。 值类型的变量包含类型的实例。 它不同于引用类型的变量,后者包含对类型实例的引用。 默认情况下,在分配中,通过将实参传递给方法并返回方法结果来复制变量值。 对于值类型变量,会复制相应的类型实例。 以下示例演示了该行为:
using System;
public struct MutablePoint
{
public int X;
public int Y;
public MutablePoint(int x, int y) => (X, Y) = (x, y);
public override string ToString() => $"({X}, {Y})";
}
public class Program
{
public static void Main()
{
var p1 = new MutablePoint(1, 2);
var p2 = p1;
p2.Y = 200;
Console.WriteLine($"{nameof(p1)} after {nameof(p2)} is modified: {p1}");
Console.WriteLine($"{nameof(p2)}: {p2}");
MutateAndDisplay(p2);
Console.WriteLine($"{nameof(p2)} after passing to a method: {p2}");
}
private static void MutateAndDisplay(MutablePoint p)
{
p.X = 100;
Console.WriteLine($"Point mutated in a method: {p}");
}
}
如前面的示例所示,对值类型变量的操作只影响存储在变量中的值类型实例。
如果值类型包含引用类型的数据成员,则在复制值类型实例时,只会复制对引用类型实例的引用。 副本和原始值类型实例都具有对同一引用类型实例的访问权限。 以下示例演示了该行为:
using System;
using System.Collections.Generic;
public struct TaggedInteger
{
public int Number;
private List<string> tags;
public TaggedInteger(int n)
{
Number = n;
tags = new List<string>();
}
public void AddTag(string tag) => tags.Add(tag);
public override string ToString() => $"{Number} [{string.Join(", ", tags)}]";
}
public class Program
{
public static void Main()
{
var n1 = new TaggedInteger(0);
n1.AddTag("A");
Console.WriteLine(n1); // output: 0 [A]
var n2 = n1;
n2.Number = 7;
n2.AddTag("B");
Console.WriteLine(n1); // output: 0 [A, B]
Console.WriteLine(n2); // output: 7 [A, B]
}
}
(3).值类型的种类以及类型约束
值类型可以是以下种类之一:
-
结构类型,用于封装数据和相关功能
-
枚举类型,由一组命名常数定义,表示一个选择或选择组合
可为 null 值类型 T?
表示其基础值类型 T
的所有值及额外的 null 值。 不能将 null
分配给值类型的变量,除非它是可为 null 的值类型。
你可使用 struct
约束指定类型参数为不可为 null 的值类型。 结构类型和枚举类型都满足 struct
约束。 从 C# 7.3 开始,你可以在基类约束中使用 System.Enum
(称为枚举约束),以指定类型参数为枚举类型。
(4).内置值类型
C# 提供以下内置值类型,也称为“简单类型”:
所有简单值都是结构类型,它们与其他结构类型的不同之处在于,它们允许特定的额外操作:
-
可以使用文字为简单类型提供值。 例如,'A'
是类型 char
的文本,2001
是类型 int
的文本。
-
可以使用 const 关键字声明简单类型的常数。 不能具有其他结构类型的常数。
-
常数表达式的操作数都是简单类型的常数,在编译时进行评估。
从 C# 7.0 开始,C# 支持值元组。 值元组是值类型,而不是简单类型
3.强制转换和类型转换
由于 C# 是在编译时静态类型化的,因此变量在声明后就无法再次声明,或无法分配另一种类型的值,除非该类型可以隐式转换为变量的类型。 例如,string
无法隐式转换为 int
。 因此,在将 i
声明为 int
后,无法将字符串“Hello”分配给它,如以下代码所示:
int i;
// error CS0029: Cannot implicitly convert type 'string' to 'int'
i = "Hello";
但有时可能需要将值复制到其他类型的变量或方法参数中。 例如,可能需要将一个整数变量传递给参数类型化为 double
的方法。 或者可能需要将类变量分配给接口类型的变量。 这些类型的操作称为类型转换。 在 C# 中,可以执行以下几种类型的转换:
(2).隐式转换
对于内置数值类型,如果要存储的值无需截断或四舍五入即可适应变量,则可以进行隐式转换。 对于整型类型,这意味着源类型的范围是目标类型范围的正确子集。 例如,long 类型的变量(64 位整数)能够存储 int(32 位整数)可存储的任何值。 在下面的示例中,编译器先将右侧的 num
值隐式转换为 long
类型,再将它赋给 bigNum
。
int num = 2147483647;
long bigNum = num;
(3).显式转换
但是,如果进行转换可能会导致信息丢失,则编译器会要求执行显式转换,显式转换也称为强制转换。 强制转换是显式告知编译器以下信息的一种方式:你打算进行转换且你知道可能会发生数据丢失,或者你知道强制转换有可能在运行时失败。 若要执行强制转换,请在要转换的值或变量前面的括号中指定要强制转换到的类型。 下面的程序将 double 强制转换为 int。如不强制转换则该程序不会进行编译。
class Test
{
static void Main()
{
double x = 1234.7;
int a;
// Cast double to int.
a = (int)x;
System.Console.WriteLine(a);
}
}
有关支持的显式数值转换的完整列表,请参阅内置数值转换一文的显式数值转换部分。
对于引用类型,如果需要从基类型转换为派生类型,则必须进行显式强制转换:
Giraffe g = new Giraffe();
// Implicit conversion to base type is safe.
Animal a = g;
// Explicit conversion is required to cast back
// to derived type. Note: This will compile but will
// throw an exception at run time if the right-side
// object is not in fact a Giraffe.
Giraffe g2 = (Giraffe)a;
引用类型之间的强制转换操作不会更改基础对象的运行时类型;它只更改用作对该对象引用的值的类型。 有关详细信息,请参阅多态性。
(4).运行时的类型转换异常
在某些引用类型转换中,编译器无法确定强制转换是否会有效。 正确进行编译的强制转换操作有可能在运行时失败。 如下面的示例所示,类型转换在运行时失败将导致引发 InvalidCastException。
class Animal
{
public void Eat() => System.Console.WriteLine("Eating.");
public override string ToString() => "I am an animal.";
}
class Reptile : Animal { }
class Mammal : Animal { }
class UnSafeCast
{
static void Main()
{
Test(new Mammal());
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
static void Test(Animal a)
{
// System.InvalidCastException at run time
// Unable to cast object of type 'Mammal' to type 'Reptile'
Reptile r = (Reptile)a;
}
}