C# 编程中的控制流、类型转换与异常处理
在 C# 编程中,控制流语句、类型转换以及异常处理是非常重要的部分。下面将详细介绍 C# 中的各种控制流语句,包括选择语句和迭代语句,同时还会涉及到模式匹配和简化代码的技巧。
1. 运算符示例
在 C# 中,有各种不同的运算符。例如:
int age = 50;
WriteLine($"The {nameof(age)} variable uses {sizeof(int)} bytes of memory.");
还有一些常见的运算符,如成员访问运算符(变量和其成员之间的点号)和调用运算符(函数或方法名末尾的圆括号)。以下代码展示了多个运算符的使用:
int age = 50;
// How many operators in the following statement?
char firstDigit = age.ToString()[0];
// There are four operators:
// = is the assignment operator
// . is the member access operator
// () is the invocation operator
// [] is the indexer access operator
2. 选择语句
每个应用程序都需要能够从多个选项中进行选择,并沿着不同的代码路径执行。C# 中有两种选择语句:
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



