Example: Nested ternary operator
(item.employee).HasValue && item.dept!= 0) ? 'ok' : 'failed';
(Ename != null || Deptno!= null) ? "OK" : "No"
===========================================
int x = 2, y = 10;
string result = x > y ? "x is greater than y" : x < y ?
"x is less than y" : x == y ?
"x is equal to y" : "No result";
Example:2
int i = 5;
string result = i % 2 == 0 ? "a"
: i % 3 == 0 ? "b"
: i % 5 == 0 ? "c"
: i % 7 == 0 ? "d"
: "e";
Example:3
Nesting inside the then:
i % 2 == 0 ? i % 4 == 0 ? "even steven" : "even" : "odd";
// Formatted:
i % 2 == 0 ?
i % 4 == 0 ? "even steven"
: "even"
: "odd";
// Translated:
if i % 2 == 0 then
if i % 4 == 0 then "even steven"
else "even"
else "odd";
Fizz buzz, nesting inside both:
i % 2 == 0 ? i % 3 == 0 ? "fizzbuzz" : "fizz" : i % 3 == 0 ? "buzz" : i.ToString();
// Formatted (note: This case is too complicated. You won't get hired if you write it like this.):
i % 2 == 0 ?
i % 3 == 0 ? "fizzbuzz"
: "fizz"
: i % 3 == 0 ? "buzz"
: i.ToString();
// Translated:
if i % 3 == 0 then
if i % 5 == 0 then "fizzbuzz"
else "fizz"
else if i % 5 == 0 then "buzz"
else i.ToString();orif i % 2 == 0 then
if i % 3 == 0 then “fizzbuzz”
else “fizz”
else if i % 3 == 0 then “buzz”
else i.ToString();
================================
string result = status == 1 ? "Approved" : status == 2 ? "Reject" : "Pending";
switch (status)
{
case 1: result = "Approved"; break;
case 2: result = "Reject"; break;
case 3: result = "Pending"; break;
default: result = "ERROR"; break;
}
<td class="@(EmployeeModel.Any()?
(!string.IsNullOrEmpty(EmployeeModel.EName)?
"greenStyle" :
"whiteStyle")
:"whiteStyle")">
<td class="@(EmployeeModel.Any()?
(!string.IsNullOrEmpty(EmployeeModel.EName)
||!string.IsNullOrEmpty(EmployeeModel.LName)?
"greenStyle" :
"whiteStyle")
:"whiteStyle")">
No comments:
Post a Comment