I want to know syntax sugar in C#.
var name = side=="BUY" ? "LONG" : "SHORT";
->It's very simple.
But there is possibility of the value except "BUY" and "SELL" in side. The following is redundant. Please tell me the simple expression.
var name;
if (side == "BUY")
name="LONG";
else if(side="SELL")
name="SHORT";
else
throw Exception();
Here are few shorter ways to throw Exception (all case sensitive):
string name1 = side == "BUY" ? "LONG" : side == "SELL" ? "SHORT" : throw new Exception();
string name2 = new[] { "LONG", "SHORT" }[Array.IndexOf(new[] { "BUY", "SELL" }, side)]; // System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
string name3 = new Dictionary<string, string> { { "BUY", "LONG" }, { "SELL", "SHORT" } }[side]; // System.Collections.Generic.KeyNotFoundException: 'The given key was not present in the dictionary.'