Skip to content

Instantly share code, notes, and snippets.

@mntone
Created November 27, 2016 07:46
Show Gist options
  • Select an option

  • Save mntone/61f8161c3882091a1e048bfc8580dee8 to your computer and use it in GitHub Desktop.

Select an option

Save mntone/61f8161c3882091a1e048bfc8580dee8 to your computer and use it in GitHub Desktop.
switch 文に `ValueTuple<int, int>` 以外を使って書く方法はないのだろうか?
using System;
namespace CSharp7Try
{
class Program
{
static void Main(string[] args)
{
TrySwitchFeature((x: 1, y: 2));
TrySwitchFeature((x: 7, y: 2));
TrySwitchFeature((x: 1, y: -2));
TrySwitchFeature((x: -4, y: 6));
TrySwitchFeature((x: -4, y: -2));
}
static void TrySwitchFeature((int x, int y) item)
{
switch (item)
{
case ValueTuple<int, int> i when i.Item1 >= 0 && i.Item2 >= 0:
Console.WriteLine("x >= 0 and y >= 0");
break;
case ValueTuple<int, int> i when i.Item1 >= 0:
Console.WriteLine("x >= 0");
break;
case ValueTuple<int, int> i when i.Item2 >= 0:
Console.WriteLine("y >= 0");
break;
default:
Console.WriteLine("x < 0 or y < 0");
break;
}
}
}
}
x >= 0 and y >= 0
x >= 0 and y >= 0
x >= 0
y >= 0
x < 0 or y < 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment