Ternary Operator C#

Asked

Viewed 459 times

5

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

I need to enter the property Productocatalogodigital which is of type Basicolongdto.

Currently it is this way:

public BasicoLongDTO Produto => ProdutoLivro != null ? new BasicoLongDTO(ProdutoSolucao.Codigo, ProdutoSolucao.Nome) : new BasicoLongDTO(ProdutoBibliotecaDigital.Codigo, ProdutoBibliotecaDigital.Nome);

2 answers

4

Yes, it is possible.

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

See working on Dotnetfiddle.

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

3


You can, I use it that way:

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

  • It worked very well! It stayed this way: Productvro != null ? new Basicolongdto(Productvro.Codigo, Productvro.Name) : Productsrolution != null ? New basicolongdto(Productsproduction.Code, Productsproduction.Name) : Productobibliotecadigital != null ? New basicolongdto(Productobibliotecadigital.Code, Productobibliotecadigital.Name) : new Basicolongdto(Productocatalogodigital.Code, Productocatalogodigital.Name);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.