-
深入理解 C# 编程:枚举、文件处理、异常处理和数字相加
枚举是一个特殊的“类”,表示一组常量(不可更改/只读变量)。
要创建枚举,请使用 enum
关键字(而不是 class
或 interface
),并用逗号分隔枚举项:
|
enum Level |
|
{ |
|
Low, |
|
Medium, |
|
High |
|
} |
您可以使用点语法访问枚举项:
|
Level myVar = Level.Medium; |
|
Console.WriteLine(myVar); |
Enum
是 "enumerations
" 的缩写,意思是“特定列举”。
在类内部定义枚举也是可行的:
|
class Program |
|
{ |
|
enum Level |
|
{ |
|
Low, |
|
Medium, |
|
High |
|
} |
|
static void Main(string[] args) |
|
{ |
|
Level myVar = Level.Medium; |
|
Console.WriteLine(myVar); |
|
} |
|
} |
输出将为:
|
Medium |
枚举值
默认情况下,枚举的第一项的值为 0。第二项的值为 1,依此类推。
要从项中获取整数值,必须显式将项转换为 int
:
|
enum Months |
|
{ |
|
January, // 0 |
|
February, // 1 |
|
March, // 2 |
|
April, // 3 |
|
May, // 4 |
|
June, // 5 |
|
July // 6 |
|
} |
|
|
|
static void Main(string[] args) |
|
{ |
|
int myNum = (int) Months.April; |
|
Console.WriteLine(myNum); |
|
} |
输出将为:
|
3 |
您还可以分配自己的枚举值,接下来的项将相应地更新它们的数字:
|
enum Months |
|
{ |
|
January, // 0 |
|
February, // 1 |
|
March=6, // 6 |
|
April, // 7 |
|
May, // 8 |
|
June, // 9 |
|
July // 10 |
|
} |
|
|
|
static void Main(string[] args) |
|
{ |
|
int myNum = (int) Months.April; |
|
Console.WriteLine(myNum); |
|
} |
输出将为:
|
7 |
在 Switch 语句中使用枚举
枚举通常在 switch
语句中用于检查相应的值:
|
enum Level |
|
{ |
|
Low, |
|
Medium, |
|
High |
|
} |
|
|
|
static void Main(string[] args) |
|
{ |
|
Level myVar = Level.Medium; |
|
switch(myVar) |
|
{ |
|
case Level.Low: |
|
Console.WriteLine("低级别"); |
|
break; |
|
case Level.Medium: |
|
Console.WriteLine("中级别"); |
|
break; |
|
case Level.High: |
|
Console.WriteLine("高级别"); |
|
break; |
|
} |
|
} |
输出将为:
|
中级别 |
何时以及为什么使用枚举?
在有些情况下,当您知道值不会更改时,例如月份、日期、颜色、扑克牌等时,请使用枚举。
C# 文件
处理文件
System.IO 命名空间中的 File
类允许我们处理文件:
|
using System.IO; // 包含 System.IO 命名空间 |
|
|
|
File.SomeFileMethod(); // 使用带有方法的 File 类 |
File
类具有许多有用的方法,用于创建文件和获取有关文件的信息。例如:
方法 | 描述 |
---|---|
AppendText() |
在现有文件的末尾追加文本 |
Copy() |
复制文件 |
Create() |
创建或覆盖文件 |
Delete() |
删除文件 |
Exists() |
测试文件是否存在 |
ReadAllText() |
读取文件的内容 |
Replace() |
用另一个文件的内容替换文件的内容 |
WriteAllText() |
创建新文件并将内容写入其中。如果文件已存在,将覆盖它。 |
有关 File
方法的完整列表,请参阅 Microsoft .Net File Class 参考文档.
写入文件并读取它
在以下示例中,我们使用 WriteAllText()
方法创建一个名为 "filename.txt" 的文件,并向其中写入一些内容。然后,我们使用 ReadAllText()
方法读取文件的内容:
|
using System.IO; // 包含 System.IO 命名空间 |
|
|
|
string writeText = "Hello World!"; // 创建一个文本字符串 |
|
File.WriteAllText("filename.txt", writeText); // 创建文件并将 writeText 的内容写入其中 |
|
|
|
string readText = File.ReadAllText("filename.txt"); // 读取文件的内容 |
|
Console.WriteLine(readText); // 输出内容 |
输出将为:
|
Hello World! |
C# 异常
在执行 C#
代码时,可能会发生不同类型的错误:由程序员制造的编码错误,由于错误的输入或其他不可预见的事物而导致的错误。
当发生错误时,C#
通常会停止执行并生成错误消息。这种情况的技术术语是:C#
会引发异常(抛出错误)。
C# try 和 catch
try
语句允许您定义一块在执行过程中测试错误的代码块。
catch
语句允许您定义一块在 try
块中发生错误时执行的代码块。
try
和 catch
关键字是成对出现的:
|
try |
|
{ |
|
// 试图执行的代码块 |
|
} |
|
catch (Exception e) |
|
{ |
|
// 处理错误的代码块 |
|
} |
考虑以下示例,我们在其中创建一个包含三个整数的数组:
这将生成一个错误,因为 myNumbers[10]
不存在。
|
int[] myNumbers = {1, 2, 3}; |
|
Console.WriteLine(myNumbers[10]); // error! |
错误消息将类似于:
|
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' |
如果发生错误,我们可以使用 try...catch
来捕获错误并执行一些代码来处理它。
在以下示例中,我们在 catch
块中使用变量(e)以及内置的 Message
属性,该属性输出描述异常的消息:
|
try |
|
{ |
|
int[] myNumbers = {1, 2, 3}; |
|
Console.WriteLine(myNumbers[10]); |
|
} |
|
catch (Exception e) |
|
{ |
|
Console.WriteLine(e.Message); |
|
} |
输出将为:
|
Index was outside the bounds of the array. |
您还可以输出自己的错误消息:
|
try |
|
{ |
|
int[] myNumbers = {1, 2, 3}; |
|
Console.WriteLine(myNumbers[10]); |
|
} |
|
catch (Exception e) |
|
{ |
|
Console.WriteLine("Something went wrong."); |
|
} |
输出将为:
|
Something went wrong. |
finally 块
finally
语句允许您在 try...catch
之后执行代码,无论结果如何:
|
try |
|
{ |
|
int[] myNumbers = {1, 2, 3}; |
|
Console.WriteLine(myNumbers[10]); |
|
} |
|
catch (Exception e) |
|
{ |
|
Console.WriteLine("Something went wrong."); |
|
} |
|
finally |
|
{ |
|
Console.WriteLine("The 'try catch' is finished."); |
|
} |
输出将为:
|
Something went wrong. |
|
The 'try catch' is finished. |
throw 关键字
throw
语句允许您创建自定义错误。
throw
语句与异常类一起使用。在 C#
中有许多异常类可用:ArithmeticException
、FileNotFoundException
、IndexOutOfRangeException
、TimeOutException
等:
|
static void checkAge(int age) |
|
{ |
|
if (age < 18) |
|
{ |
|
throw new ArithmeticException("Access denied - You must be at least 18 years old."); |
|
} |
|
else |
|
{ |
|
Console.WriteLine("Access granted - You are old enough!"); |
|
} |
|
} |
|
|
|
static void Main(string[] args) |
|
{ |
|
checkAge(15); |
|
} |
程序中显示的错误消息将为:
|
System.ArithmeticException: 'Access denied - You must be at least 18 years old.' |
如果 age 为 20,则不会引发异常:
|
checkAge(20); |
输出将为:
|
Access granted - You are old enough! |
C# 如何相加两个数字
学习如何在 C#
中相加两个数字:
|
// 示例 |
|
int x = 5; |
|
int y = 6; |
|
int sum = x + y; |
|
Console.WriteLine(sum); // 打印 x + y 的和 |
最后
为了方便其他设备和平台的小伙伴观看往期文章:
出处:https://www.cnblogs.com/xiaowange/p/17995312