Click here to Skip to main content
16,013,516 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
given int? a=0;
if 'a' is null then display 'pls enter any number',
otherwise display result as 'a=10'.
can anybody do this above as lambda expression?

What I have tried:

C#
int? a=0;
if(a==null)
{
i=0;
}
else
{
i=10
}

This is normal statement. How to write this in lambda expression?
Posted
Updated 14-Jun-16 10:31am
v2
Comments
Kornfeld Eliyahu Peter 14-Jun-16 14:11pm    
a = (a == null) ? 0 : 10
phil.o 14-Jun-16 16:20pm    
This is not a lambda expression :) This is a conditional assignment, or ternary operator.
Kornfeld Eliyahu Peter 14-Jun-16 16:33pm    
Yes. And I wrote it as a first step toward a wasted lambda expression...and as the last step away from that if...
phil.o 15-Jun-16 2:11am    
There was no pun intended in my reply; I just wanted to make sure that OP does not consider as a lambda expression something which is not.
Philippe Mori 14-Jun-16 14:50pm    
Uses code block and indentation for your code.

1 solution

Not sure what you want to achieve, but it seems you want to "translate" your code do lambda...

C#
int i = a==null ? 0 : 10;
if (i == 0)
    Console.WriteLine("Please enter any number!");


Note: your code has no sense, becuase you loose original value of a when it stores not null value.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900