FX5UPLC视频链接:https://pan.baidu.com/s/1OOzpn4ic5VaM1TvKoJgPaA 提取码:XXOO
C#笔记
is运算符也可以自然地引入变量了,称为模式变量:
void Foo (object x)
{
if (x is string s)
Console.WriteLine (s.Length);
}
感觉比用as做类型转换要高效很多!
switch语句同样支持模式,因此我们不仅可以选择常量还可以选择类型;可以使用when子句来指定一个判断条件;或是直接选择null:
switch (x)
{
case int i:
Console.WriteLine ("It's an int! ");
break;
case string s:
Console.WriteLine (s.Length); // We can use the s variable
break;
case bool b when b == true: // Matches only when b is true
Console.WriteLine ("True");
break;
case null:
Console.WriteLine ("Nothing");
break;
}
…………………………………………………………*
异常过滤器(exception filters)可以在catch块上再添加一个条件:
string html;
try
{
html = new WebClient().DownloadString ("http://asef");
}
catch (WebException ex) when (ex.Status == WebExceptionStatus.Timeout)
{
...
}