Single line if statement with 2 actions

شرط یک خطی در c#

Sounds like you really want a Dictionary<int, string> or possibly a switch statement…

You can do it with the conditional operator though:

userType = user.Type == 0 ? "Admin"
         : user.Type == 1 ? "User"
         : user.Type == 2 ? "Employee"
         : "The default you didn't specify";

While you could put that in one line, I’d strongly urge you not to.

I would normally only do this for different conditions though – not just several different possible values, which is better handled in a map.

همچنین ببینید

c# Shrp Programing

split ، تقسیم و جدا کردن یک متن بر اساس یک کاراکتر

How do I split a string by a multi-character delimiter in C# ? What if …

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد.