What does a question mark in C # mean? For example here
get { return name != null ? name : "NA"; } and is it still possible to use it?
What does a question mark in C # mean? For example here
get { return name != null ? name : "NA"; } and is it still possible to use it?
The conditional operator (?:) (also known as the ternary operator ) returns one of two values depending on the value of the logical expression. The conditional statement uses the following syntax
condition ? first_expression : second_expression; The condition parameter must be true or false . If the condition parameter is true , the expression first_expression is evaluated and the result of this calculation becomes the result.
If the condition parameter is false , the expression second_expression is evaluated and the result of this calculation becomes the result.
In any case, only one of the two expressions is calculated. The first_expression and second_expression parameters must be of the same type, or an implicit conversion from one type to another must exist.
Calculations, which in another case would require the refinement of the if-else construction , can be expressed using a conditional operator. For example, in the following code, the if operator is first used, and then the conditional operator to notify whether the number entered by the user is five or not:
int input = Convert.ToInt32(Console.ReadLine()); string classify; // Обычная конструкция if-else. if (input == 5) classify = "Вы ввели число 5! Молодец!"; else classify = "Это НЕ число 5! Это бяка!"; // ?: оператор. classify = (input == 5) ? "Вы ввели число 5! Молодец!" : "Это НЕ число 5! Это бяка!"; if(name != null) return name; else return "NA"; ? means if
: means otherwise
Source: https://ru.stackoverflow.com/questions/543129/
All Articles
class Person { private string name; // the name field public string Name // the Name property { get { return name; } set { name = value; } } }class Person { private string name; // the name field public string Name // the Name property { get { return name; } set { name = value; } } }- Denisok