Skip to content

Instantly share code, notes, and snippets.

@dck-jp
Last active August 29, 2015 14:10
Show Gist options
  • Save dck-jp/ce3c16286d535d04e84d to your computer and use it in GitHub Desktop.
Save dck-jp/ce3c16286d535d04e84d to your computer and use it in GitHub Desktop.
PatternMatch in C#
using System;
namespace ConsoleApplication
{
public abstract class Name : Exception{}
public class FirstName : Name {
public FirstName(string name) { Name = name; }
public string Name { get; private set;}
}
public class LastName : Name {
public LastName(string name) { Name = name; }
public string Name { get; private set;}
}
class Program
{
static void Main(string[] args)
{
printName(new FirstName("名前"));
printName(new LastName("苗字"));
Console.ReadKey();
}
static void printName(Name name) {
try {
throw(name);
}catch (FirstName n) {
Console.WriteLine("Your first name is " + n.Name);
}catch (LastName n) {
Console.WriteLine("Your last name is " + n.Name);
} catch (Name e) {
Console.WriteLine(e.StackTrace);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment