Ternary operator

I am wondering: what is the best instruction in terms of performance between these two versions:

Background = Application.Current.Resources[condition ? BackgroundName1 : BackgroundName2] as Brush;

and

Background = condition ? Application.Current.Resources[BackgroundName1] as Brush : Application.Current.Resources[BackgroundName2] as Brush;

Is there any difference? and if so, which is better?

thank

NB: BackgroundName1 and 2 are just strings

+3
source share
4 answers

The first one is shorter and more readable.

It is also easier to maintain. If you later change it to read another resource dictionary, you may forget to change the second half of the second.

The first one is also more clearly read from the same dictionary.

+5
source

: , . , , - , .

: , , , . , ; , .

+3

-! , , . , .

+2

.

0

Source: https://habr.com/ru/post/1776232/


All Articles