Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify this C# if/else syntax

Tags:

c#

I have the following statement:

serverCard.Details = !String.IsNullOrEmpty(card.Details) ? card.Details : serverCard.Details;

I want to check and see if card.Details is null or empty... if not, write the value. Is there any syntax that allows me to leave out the else conditional?

like image 735
RobVious Avatar asked Jun 03 '13 15:06

RobVious


People also ask

How do you expand and simplify?

What does 'expand and simplify' mean? In order to expand and simplify an expression, we need to multiply out the brackets and then simplify the resulting expression by collecting the like terms. Expanding brackets (or multiplying out) is the process by which we remove brackets.


2 Answers

Sure, just use a regular if:

if(!String.IsNullOrEmpty(card.Details))
    serverCard.Details = card.Details
like image 106
Servy Avatar answered Sep 30 '22 10:09

Servy


You can always use the old if statement:

if(!String.IsNullOrEmpty(card.Details))
{
  serverCard.Details = card.Details;
}

I think the ternary operator is not needed here.

like image 30
edtheprogrammerguy Avatar answered Sep 30 '22 11:09

edtheprogrammerguy