Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created May 21, 2019 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielplawgo/afe434e9918ee9a284abe47d193d4e83 to your computer and use it in GitHub Desktop.
Save danielplawgo/afe434e9918ee9a284abe47d193d4e83 to your computer and use it in GitHub Desktop.
Nowy switch w C# 8.0
class OperationDemo
{
public void Run()
{
var expression = new Expression()
{
Arg1 = 10,
Arg2 = 5,
Operator = "/"
};
CSharp1(expression);
CSharp7(expression);
CSharp8(expression);
CSharp8Property(expression);
}
void CSharp1(Expression ex)
{
int result = 0;
switch (ex.Operator)
{
case "+":
result = ex.Arg1 + ex.Arg2;
break;
case "/":
if (ex.Arg2 == 0)
{
throw new ArgumentException("Arg2 cannot be 0");
}
result = ex.Arg1 / ex.Arg2;
break;
default:
throw new InvalidOperationException();
}
Console.WriteLine($"{ex} = {result}");
}
}
void CSharp7(object o)
{
int result = 0;
switch (o)
{
case Expression ex when ex.Operator == "+":
result = ex.Arg1 + ex.Arg2;
break;
case Expression ex when ex.Operator == "/" && ex.Arg2 == 0:
throw new ArgumentException("Arg2 cannot be 0");
case Expression ex when ex.Operator == "/":
result = ex.Arg1 / ex.Arg2;
break;
default:
throw new InvalidOperationException();
}
Console.WriteLine($"{o} = {result}");
}
void CSharp8(object o)
{
int result = o switch
{
Expression ex when ex.Operator == "+" => ex.Arg1 + ex.Arg2,
Expression ex when ex.Operator == "/" && ex.Arg2 == 0 => throw new ArgumentException("Arg2 cannot be 0"),
Expression ex when ex.Operator == "/" => ex.Arg1 / ex.Arg2,
_ => throw new InvalidOperationException()
};
Console.WriteLine($"{o} = {result}");
}
void CSharp8Property(object o)
{
int result = o switch
{
Expression { Operator: "+" } ex => ex.Arg1 + ex.Arg2,
Expression { Operator: "/", Arg2: 0 } ex => throw new ArgumentException("Arg2 cannot be 0"),
Expression { Operator: "/" } ex => ex.Arg1 / ex.Arg2,
_ => throw new InvalidOperationException()
};
Console.WriteLine($"{o} = {result}");
}
void CSharp8Property(Expression ex)
{
int result = ex switch
{
{ Operator: "+" } => ex.Arg1 + ex.Arg2,
{ Operator: "/", Arg2: 0 } => throw new ArgumentException("Arg2 cannot be 0"),
{ Operator: "/" } => ex.Arg1 / ex.Arg2,
_ => throw new InvalidOperationException()
};
Console.WriteLine($"{ex} = {result}");
}
public class Expression
{
public string Operator { get; set; }
public int Arg1 { get; set; }
public int Arg2 { get; set; }
public override string ToString()
{
return $"{Arg1} {Operator} {Arg2}";
}
}
public enum State
{
Created,
Processing,
Processed,
Canceled
}
public enum Operation
{
Process,
Deliver,
Cancel
}
class StateDemo
{
public void Run()
{
var state = State.Canceled;
var operation = Operation.Cancel;
state = (state, operation) switch
{
(State.Created, Operation.Process) => State.Processing,
(State.Processing, Operation.Deliver) => State.Processed,
(State.Canceled, _) => throw new InvalidOperationException(),
(_, Operation.Cancel) => State.Canceled,
_ => throw new InvalidOperationException()
};
Console.WriteLine(state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment