上下文关键字 when 在以下上下文中指定筛选条件:
- 在 try/catch 或 try/catch/finally 块的 catch 语句中。
- 在 switch 语句的 case 标签中。
- 在 switch 表达式中。
catch 语句中的 when
class Program
{
static void Main()
{
Console.WriteLine(MakeRequest().Result);
}
public static async Task<string> MakeRequest()
{
var client = new HttpClient();
var streamTask = client.GetStringAsync("https://localHost:10000");
try
{
var responseText = await streamTask;
return responseText;
}
catch (HttpRequestException e) when (e.Message.Contains("301"))
{
return "Site Moved";
}
catch (HttpRequestException e) when (e.Message.Contains("404"))
{
return "Page Not Found";
}
catch (HttpRequestException e)
{
return e.Message;
}
}
}
switch 语句中的 when
using System;
public abstract class Shape
{
public abstract double Area { get; }
public abstract double Circumference { get; }
}
public class Rectangle : Shape
{
public Rectangle(double length, double width)
{
Length = length;
Width = width;
}
public double Length { get; set; }
public double Width { get; set; }
public override double Area
{
get { return Math.Round(Length * Width,2); }
}
public override double Circumference
{
get { return (Length + Width) * 2; }
}
}
public class Square : Rectangle
{
public Square(double side) : base(side, side)
{
Side = side;
}
public double Side { get; set; }
}
public class Example
{
public static void Main()
{
Shape sh = null;
Shape[] shapes = { new Square(10), new Rectangle(5, 7),
new Rectangle(10, 10), sh, new Square(0) };
foreach (var shape in shapes)
ShowShapeInfo(shape);
}
private static void ShowShapeInfo(Object obj)
{
switch (obj)
{
case Shape shape when shape.Area == 0:
Console.WriteLine($"The shape: {shape.GetType().Name} with no dimensions");
break;
case Square sq when sq.Area > 0:
Console.WriteLine("Information about the square:");
Console.WriteLine($" Length of a side: {sq.Side}");
Console.WriteLine($" Area: {sq.Area}");
break;
case Rectangle r when r.Area > 0:
Console.WriteLine("Information about the rectangle:");
Console.WriteLine($" Dimensions: {r.Length} x {r.Width}");
Console.WriteLine($" Area: {r.Area}");
break;
case Shape shape:
Console.WriteLine($"A {shape.GetType().Name} shape");
break;
case null:
Console.WriteLine($"The {nameof(obj)} variable is uninitialized.");
break;
default:
Console.WriteLine($"The {nameof(obj)} variable does not represent a Shape.");
break;
}
}
}
// The example displays the following output:
// Information about the square:
// Length of a side: 10
// Area: 100
// Information about the rectangle:
// Dimensions: 5 x 7
// Area: 35
// Information about the rectangle:
// Dimensions: 10 x 10
// Area: 100
// The obj variable is uninitialized.
// The shape: Square with no dimensions