Ternary Operator C # [duplicate]

5

I would like to add one more condition in my ternary operator. Is it possible for more than two conditions? Or is there another possibility?

I need to insert the ProductCatalogDigital property, which is of the BasicLongDTO type.

Currently it looks like this:

public BasicoLongDTO Produto => ProdutoLivro != null ? new BasicoLongDTO(ProdutoSolucao.Codigo, ProdutoSolucao.Nome) : new BasicoLongDTO(ProdutoBibliotecaDigital.Codigo, ProdutoBibliotecaDigital.Nome);
    
asked by anonymous 22.02.2018 / 20:31

2 answers

3

Yes you can, I use it this way:

([expressão] ? [valor1] : ([expressão] ? [valor1] : ([expressão] ? [valor1] : [valor2])))

    
22.02.2018 / 20:35
4

Yes, it is possible.

var ternaryResult = (false ? "First Result" : false ? "Second Result" : "Last Result");

See working at DotNetFiddle .

For more information on the operation of the ?: operator, click here .

    
22.02.2018 / 20:37