Let's say I have this code like this....
public void SayType<T,E>(Func<T, E> f)
{
var dataType = string.Empty;
if (typeof(E) == typeof(DateTime))
dataType = "date/time";
else if (typeof(E) == typeof(string))
dataType = "text";
else if (typeof(E) == typeof(int))
dataType = "whole number";
Console.WriteLine($"type identified as {dataType}");
}
Is there a way to rewrite this with pattern-matching? E.g. something like:
//Note this won't compile since the LHS of a case must be a const.
var dataType = typeof(E) switch {
typeof(DateTime) => "date/time",
typeof(string) => "text",
typeof(int) => "whole number",
};
This github proposal may be relevant but I could just be reading too much into it.