. else , , if false.
if:
if ( ) ; else ;
:
if (a > b && a > c) {
message = " : " + a;
} else if (b > c) {
message = " : " + b;
} else {
message = " : " + c;
}
switch
switch , .
switch , , . if/else, (, ), switch , , . :
switch (){
case 1:
// ;
break;
[ase 2:
// ;
break;
]
...
[default:
//, ,
//
// ;
break;
]
}
- sbyte, byte, short, ushort, int, uint, long, ulong, char string .
:
// n
if (n > 0) {
switch(n) {
case 1:
message = " ";
break;
case 2:
message = " ";
break;
case 3:
message = " ";
break;
default:
message = " ";
break;
}
} else {
message = "- 0";
}
++, switch # (fall-through). , break case, switch. , case- , . # case-, case- , :
switch(host) {
case "127.0.0.1":
case "localhost":
message = " ";
break;
default:
message = " ";
break;
}
4
- , , .
|
|
, , , .
, , , , .
, , , ( , ).
, , , , , .
, , . :
( )
( )
C# for, while do-while : foreach , , .
FOR. . - .
:
for (_; ; )
{
;
}
:
// 1..20
for (int i = 1; i < 20; i++) {
bool flag = false; // i,
for (int j = 2; j <= i / 2; j++) { // 2..i/2 ( i/2 )
if (i % j == 0) { // j i, i!=
flag = true;
break;
}
}
if (!flag) { // , i
sum += i;
}
}
While. . , - , . , , , . :
while ()
{
;
}
:
// - ,
// , n = 2^(n-1) - 1
int bits = 1;
while (2 << (bits - 1) < number) {
bits++;
}
do..while. , .. , , .
:
do
{
;
}while();
:
// , 100
int fibonachi1 = 1;
int fibonachi2 = 1;
do {
int next = fibonachi1 + fibonachi2;
fibonachi1 = fibonachi2;
fibonachi2 = next;
} while (fibonachi2 < 100);
, ++, foreach, , . :
foreach(_ in )
{
;
}
- . , . , , , . - , . , .
|
|
foreach C# , , . .
:
// -
int max, min;
max = min = data[0];
foreach(int item in data) {
if (item > max) { //
max = item;
continue; // item data,
}
if (item < min) { //
min = item;
}
}
Goto - . :
goto ,
goto case -,
goto default.
. - :
Exit: return 0;
:
goto Exit;
goto switch, goto:
switch (key)
{
case 0:
//do smth
break;
case 1:
//do smth
goto case 2;
case 2:
//do smth
goto default;
default:
//do smth
break;
}
.
#, , . , (, ) . ,
:
< >[] < >;
, . , , .
:
int[] myArr; //
string [] myStrings; //
# System.Array, , , System.Object. , . , . : , .; , .
, , . . , new.
myArr = new int[10]; // 10
myStrings = new string[50]; // 50
: 0, false, null.
:
int[] myArr = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
|
int[] myArr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] myArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
, - , , , , , .
.
( ) , , IL . . .
< >[,,...] < >;
float[,] myArr; //
//
myArr = new float[2, 3];//
//
int[,] myArr = new int[2, 3] { { 1, 2, 3 }, { 3, 4, 6 } };
int[,] myArr = new int[,] { { 1, 2, 3 }, { 3, 4, 6 } };
int[,] myArr = { { 1, 2, 3 }, { 3, 4, 6 } };
myArr[1, 2] = 100;
:
int[,] array = new int[5,10];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
array[i,j] = i + j;
}
}
C# ( ) . .
, :
< >[][] < >;
. .. . , , . , .
:
//
// particialArray[0]: 0
// particialArray[1]: 0 1
// particialArray[2]: 0 1 2
// particialArray[3]: 0 1 2 3
int[][] particialArray = new int[4][];
for (int i = 0; i < 4; i++) {
particialArray[i] = new int[i + 1];
for (int j = 0; j < i + 1; j++) {
particialArray[i][j] = j;
}
}
:
GetLength ;
GetLowerBound GetUpperBound (, 5 , 0, 4)
CopyTo , .
Clone . System.Object[]
BinarySearch ( )
Clear ( ). null, false, 0
IndexOf ( ), -1. .
LastIndexOf ( ). , -1.
Resize .
Reverse ( ).
Sort ( ). :
|
|
Sum
Average .
Contains , .
Max .
Min .
:
Length
Rank
:
int[] myArr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // 1 2 3 4 5 6 7 8 9 10
int[] tempArr = (int[])myArr1.Clone();
Array.Reverse(myArr1, 3, 4) //1 2 3 7 6 5 4 8 9 10
myArr1 = tempArr; // 1 2 3 4 5 6 7 8 9 10
int[] myArr2 = new int[20]; // 0 0 0 0 0 0 0 0...
myArr1.CopyTo(myArr2, 5); // 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 0 0 0...
Array.Clear(myArr2, 0, myArr2.GetLength(0)); 0 0 0 0 0...
Array.Resize(ref myArr2, 10);// 0 0 0 0 0 0 0 0 0 0
myArr2 = new[] { 1, 5, 3, 2, 8, 9, 6, 10, 7, 4 };
Array.Sort(myArr2); // 1 2 3 4 5 6 7 8 9 10
Array.BinarySearch(myArr2, 5); // 4
myArr2.Max(); // 10
myArr2.Min(); // 1
myArr2.Average(); //5.5
int[,] myArr3 = { { 1, 2, 3 }, { 4, 5, 6 } };
myArr3.Rank; //2
5
, , C# . System.String, . System.String . , . , new.
string <> = ;
, System.String 8 .
:
string str1 = " ";
char[] chrArr={'','','','','','','',' ','','','','','',''};
string str2 = new string(chrArr);
string str3 = new string(chrArr, 8, 6);
string str4 = new string('$', 10);
. @ , . :
string path = @"D:\Student\MyProjects\Strings\"
System.String:
. .
Length .
CopyTo char
Equals, Compare, CompareTo == . StringComparison, . , CurrentCultureIgnoreCase . Compare , . CompareTo (-1 , 0 , 1 ).
StartsWith EndsWith , () .
IndexOf LastIndexOf / / .
IndexOfAny LastIndexOfAny / .
SubString . .
Concat () . + +=.
ToLower ToUpper .
Replace / /.
Contains , / .
Insert .
Remove .
PadLeft PadRight /. , . , .
Split . . StringSplitOptions.RemoveEmptyEntries.
|
|
Join -.
TrimLeft TrimRight ( ) . Trim .
Format . , . , , , , . . , Format :
String.Format(" {, :}", );
:
1. ѻ . .
2. D . .
3. Ż . .
4. F .
.
5. G . .
6. N . .
7. P . %.
8. X .
.
:
string str = "Random string";
char first = str[0];//R
char last = str[str.Length - 1]; //g
string str2 = "RANDOM STRING";
bool testIs = str == str2; //false
bool testEquals = str.Equals(str2, StringComparison.CurrentCultureIgnoreCase); //true
int firstN = str.IndexOf('n');//2
int lastN = str.LastIndexOf('n');//11
string betweenNInclusive = str.Substring(firstN + 1, lastN - firstN - 1); //dom stri
str = str.Replace(' ', '_'); //Random_string
, C# . , . , . , string , . string- .
, . , . , ( , - ).
, , StringBuilder. String, , , . , StringBuilder . .
StringBuilder .
Append .
AppendFormat , .
Insert .
Remove .
Replace / /.
CopyTo char
ToString StringBuilder String
( string, StringBuilder):
string str = "Speed test";
for (int i = 0; i < 100000; i++) {
str += i;
}
// 39854ms
var builder = new System.Text.StringBuilder("Speed test");
for (int i = 0; i < 100000; i++) {
builder.Append(i);
}
string result = builder.ToString();
// 56ms