E
E
Eugene2021-07-19 22:26:43
C++ / C#
Eugene, 2021-07-19 22:26:43

Switch expression how to use it correctly?

I need to use Switch Expression to determine which of the data types (int, byte, sbyte, long, uint) object arg is. I basically figured out how to use it:
when true in bool bl, I return arg + " is byte." etc., if false, nothing should be done, so all checks for different data types will be passed, and in the end I will return that arg is not one of the types listed above, but there is one point that I do not understand .

What do I need to write after false so that nothing is executed?

public static string SwitchExpression(object arg)
        {
            bool bl = arg is byte;
            return bl switch
            {
                true => arg + " is byte.",
                false => 
            };
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2021-07-19
@viriner

public static string SwitchExpression(object arg) => arg switch
{
  String str => "is string",
  Int32 int32 => "is int32",
  // ....
  _ => "is unexpected type"
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question