Last active
August 29, 2015 14:10
-
-
Save dck-jp/ce3c16286d535d04e84d to your computer and use it in GitHub Desktop.
PatternMatch in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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