Guide to the Language


Download 2 Mb.
Pdf ko'rish
bet12/78
Sana30.04.2023
Hajmi2 Mb.
#1414515
TuriGuide
1   ...   8   9   10   11   12   13   14   15   ...   78
Bog'liq
C sharp

 Operator Precedents
In C#, expressions are normally evaluated from left to right. However, 
when an expression contains multiple operators, the precedence of those 
operators decides the order in which they are evaluated. The order of 
precedence can be seen in the following table, where the operator with the 
lower precedence will be evaluated first.
Chapter 4 OperatOrs


19
Pre
Operator
Pre
Operator
1
++ -- ! ~
7
&
2
* / %
8
^
3
+ -
9
|
4
<< >>
10
&&
5
< <= > >=
11
||
6
== !=
12
= op=
For example, logical and (&&) binds weaker than relational operators, 
which in turn bind weaker than arithmetic operators.
bool x = 2+3 > 1*4 && 5/5 == 1; // true
To make things clearer, parentheses can be used to specify which part 
of the expression will be evaluated first. Parentheses have the greatest 
precedence of all operators.
bool x = ((2+3) > (1*4)) && ((5/5) == 1); // true
Chapter 4 OperatOrs


21
© Mikael Olsson 2020 
M. Olsson, C# 8 Quick Syntax Reference
https://doi.org/10.1007/978-1-4842-5577-3_5
CHAPTER 5
Strings
The string data type is used to store string constants. They are delimited by 
double quotes.
string a = "Hello";
 String Concatenation
The concatenation operator (+) can combine strings together. It also has 
an accompanying assignment operator (+=), which appends a string to 
another and creates a new string.
string b = a + " World"; // Hello World
a += " World"; // Hello World
When one of the operands is not of a string type, the concatenation 
operator will implicitly convert the non-string type into a string, making 
the following assignment valid.
int i = 1;
string c = i + " is " + 1; // 1 is 1
The string conversion is performed implicitly using the ToString 
method. All types in .NET have this method, which provides a string 
representation of a variable or expression. As seen in the next example,
the string conversion can also be made explicitly.
string d = i.ToString() + " is " + 1.ToString(); // 1 is 1


22
Another way to compile strings is to use string interpolation. This 
feature was added in C# 6.0 and enables expressions placed inside curly 
brackets to be evaluated within a string. To perform string interpolation, a 
dollar sign ($) is placed before the string.
string s1 = "Hello";
string s2 = "World";
string s = $"{s1} {s2}"; // Hello World

Download 2 Mb.

Do'stlaringiz bilan baham:
1   ...   8   9   10   11   12   13   14   15   ...   78




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling