.


:




:

































 

 

 

 





C# 3.0 var. , , . , . , . , , LINQ. "" . #.

var, []. , , . . .

var vals = new[] { 1, 2, 3, 4, 5 };

int, . vals. , int , . , []. , , . .

, double.

var vals = new[,] { {1.1, 2.2}, {3.3, 4.4}, { 5.5, 6.6} };

vals 2x3.

. .

7.14

// .

 

using System;

 

class Jagged

{

static void Main()

{

 

var jagged = new[] {

new[] { 1, 2, 3, 4 },

new[] { 9, 8, 7 },

new[] { 11, 12, 13, 14, 15 }

};

 

for(int j = 0; j < jagged.Length; j++) {

for(int i=0; i < jagged[j].Length; i++)

Console.Write(jagged[j][i] + " ");

 

Console.WriteLine();

}

}

}

.

2 3 4

9 8 7

12 13 14 15

jagged.

var jagged = new[] {

new [] { 1, 2, 3, 4 },

new [] { 9, 8, 7 },

new [] { 11, 12, 13, 14, 15 }

};

, new[] . -, . -, , . , . , , .

, LINQ- . .

foreach

, C# foreach, . .

foreach , . C# , . foreach.

foreach ( __ in ) ;

__ , foreach. , . , . , var. , . , . , , .

foreach . , . , . , . , foreach .

, , , foreach . , , , .

foreach. , , .

7.15

// foreach.

 

using System;

 

class ForeachDemo {

static void Main() {

int sum = 0;

int[] nums = new int[10];

 

// nums.

for(int i = 0; i < 10; i++)

nums[i] = i;

 

// foreach

// .

foreach(int x in nums) {

Console.WriteLine(" : " + x);

sum += x;

}

Console.WriteLine(" : " + sum);

}

}

.

: 0

: 1

: 2

: 3

: 4

: 5

: 6

: 7

: 8

: 9

: 45

, foreach .

foreach , , , break. , nums.

7.16

// break .

 

using System;

 

class ForeachDemo

{

static void Main()

{

int sum = 0;

int[] nums = new int[10];

 

// nums.

for(int i = 0; i < 10; i++)

nums[i] = i;

 

// foreach

// .

foreach(int x in nums) {

Console.WriteLine(" : " + x);

sum += x;

if(x == 4) break; // ,

// 4

}

Console.WriteLine(" : " + sum);

}

}

.

: 0

: 1

: 2

: 3

: 4

5 : 10

, foreach .

foreach . , .

7.17

// foreach .

 

using System;

 

class ForeachDemo2

{

static void Main()

{

int sum = 0;

int[,] nums = new int[3,5];

 

// nums.

for(int i = 0; i < 3; i++)

for(int j=0; j < 5; j++)

nums[i,j] = (i+1)*(j+1);

 

// foreach

// .

foreach(int x in nums) {

Console.WriteLine(" : " + x);

sum += x;

}

Console.WriteLine(" : " + sum);

}

}

.

: 1

: 2

: 3

: 4

: 5

: 2

: 4

: 6

: 8

: 10

: 3

: 6

: 9

: 12

: 15

: 90

freach : , , , . . , , . , foreach . , .

7.18

// foreach.

 

using System;

 

class Search

{

static void Main()

{

int[] nums = new int[10];

int val;

bool found = false;

 

// nums.

for(int i = 0; i < 10; i++)

nums[i] = i;

 

val = 5;

 

// foreach

// nums.

foreach(int x in nums) {

if(x == val) {

found = true;

break;

}

}

 

if(found)

Console.WriteLine(" !");

}

}

.

!

foreach , . foreach , , .. , foreach .

string #. . . C# . , string . string C# , , .

string , , . . , :

Console.WriteLine(" C# .");

" C# . " #. , string . , .

- . , str .

string str = " C# .";

str " C# .".

string char. :

char[] charray = {'t', ' ', 's', ' t'};

string str = new string(charray);

string , , , . , string WriteLine().

7.19

// .

 

using System;

 

class StringDemo

{

static void Main()

{

 

char[] charray = {'','','',' ','','','','','','','.'};

string str1 = new string(charray);

string str2 = " .";

 

Console.WriteLine(str1);

Console.WriteLine(str2);

}

}

.

.

.

string . . 7.1. , StringComparison. , , , . , , . . , , . . ( .) , . Compare(), Equals(), IndexOf() Last IndexOf(), , , , . .

, (.. ) StringComparison.CurrentCulture. , StringComparison.Ordinal, - : StringComparison.CurrentCulturelgnoreCase StringComparison.OrdinallgnoreCase. , .

, Compare() . 7.1 static. static , , : Compare() , . , Compare() :

= string.Compare(strl, str2, );

.

, , 22, .

ToUpper() ToLower(), . , . 7.1, CultureInf, , , . , , (.. ). Culturelnf.CurrentCulture. Culturelnf System.Globalization. , , , Culturelnf.CurrentCulture .

string Length, .

7.1.

static int Compare(string strA, string strB, StringComparison comparisonType) , strA strB; , strA strB; , . comparisonType.
bool Equals(string value, StringComparison comparisonType) true, , value. comparisonType.
int IndexOf(char value) , value. . -1, .
int IndexOf(string value, StringComparison comparisonType) , value. -1, . comparisonType.

 

int LastIndexOf(char value) , value. . -1, .
int LastIndexOf(string value, StringComparison comparisonType) , value. -1, . comparisonType.
string ToLower(Culture-Info.CurrentCulture culture) . culture.
string ToUpper(Culture-Info.CurrentCulture culture) . culture.

 

, .

string str = "";

Console.WriteLine(str[0]);

"", "". , . , , , . .

==. , == , , . string. == , . ! =. . Equals(), StringComparison.CurrentCulture. , Compare() , . , Equals() .

.

7.20

// .

 

using System;

using System.Globalization;

 

class StrOps

{

static void Main()

{

string str1 = " .NET C#.";

string str2 = " .NET C#.";

string str3 = " C# .";

string strUp, strLow;

int result, idx;

 

Console.WriteLine("str1: " + str1);

 

Console.WriteLine(" str1: " + str1.Length);

 

// str1,

//

strLow = str1.ToLower(CultureInfo.CurrentCulture);

strUp = str1.ToUpper(CultureInfo.CurrentCulture);

Console.WriteLine(" str1, " +

" :\n " + strLow);

Console.WriteLine(" str1, " +

" :\n " + strUp);

 

Console.WriteLine();

 

// str1 .

Console.WriteLine(" str1 .");

for(int i=0; i < str1.Length; i++)

Console.Write(str1[i]);

Console.WriteLine("\n");

 

// .

if(str1 == str2)

Console.WriteLine("str1 == str2");

else

Console.WriteLine("str1!= str2");

 

if(str1 == str3)

Console.WriteLine("str1 == str3");

else

Console.WriteLine("str1!= str3");

 

// .

result = string.Compare(str1, str3,

StringComparison.CurrentCulture);

if(result == 0)

Console.WriteLine(" str1 str3 ");

else if(result < 0)

Console.WriteLine(" str1 str3");

else

Console.WriteLine(" str1 str3");

 

Console.WriteLine();

 

// str2.

str2 = " ";

 

// .

idx = str2.IndexOf("", StringComparison.Ordinal);

Console.WriteLine(" <>: "

+ idx);

idx = str2.LastIndexOf("", StringComparison.Ordinal);

Console.WriteLine(" <>: "

+ idx);

 

}

}

.

strl: .NET #.

strl: 41

strl, :

.net #.

strl, :

.net #.

 

strl .

.NET #.

 

strl == str2

strl!= str3

strl str3

<0>: 0

<0>: 13

, , Compare() .

result = string.Compare(strl, str3,

StringComparison.CurrentCulture);

, Compare() static, , .

+ (.. ) . , :

string strl = "";

string str2 = "";

string str3 = "";

string str4 = strl + str2 + str3;

str4 "".

: string System.String, .NET Framework, .. . , , string, System.String, . System.String .

, . .

7.21

// .

 

using System;

 

class StringArrays

{

static void Main()

{

string[] str = { "", "", "", "." };

 

Console.WriteLine(" : ");

for(int i=0; i < str.Length; i++)

Console.Write(str[i] + " ");

Console.WriteLine("\n");

 

// .

str[1] = "";

str[3] = " !";

 

Console.WriteLine(" : ");

for(int i=0; i < str.Length; i++)

Console.Write(str[i] + " ");

}

}

.

:

.

 

:

!

. . , 19 " ".

7.22

// .

 

using System;

 

class ConvertDigitsToWords

{

static void Main()

{

int num;

int nextdigit;

int numdigits;

int[] n = new int[20];

 

string[] digits = { "", "", "",

"", "", "",

"", "", "",

"" };

 

num = 1908;

 

Console.WriteLine(": " + num);

 

Console.Write(" : ");

 

nextdigit = 0;

numdigits = 0;

 

// n.

// .

do {

nextdigit = num % 10;

n[numdigits] = nextdigit;

numdigits++;

num = num / 10;

} while(num > 0);

numdigits--;

 

// .

for(; numdigits >= 0; numdigits--)

Console.Write(digits[n[numdigits]] + " ");

 

Console.WriteLine();

}

}

.

: 1908

:

digits 0 9. . , n int. n . n , , .

, string . , . . , , . , , , . "", .

, , , (.. string) , , . string .

, , : Substring(). , . , , , , . Substring():

string Substring(int _, int )

_ , - .

, Substring().

7.23

// Substring().

 

using System;

 

class SubStr

{

static void Main()

{

string orgstr = " C# .";

 

//

string substr = orgstr.Substring(5, 12);

 

Console.WriteLine("orgstr: " + orgstr);

Console.WriteLine("substr: " + substr);

}

}

.

orgstr: # .

substr:

, orgstr , substr.

: , , #, . # StringBuilder, System.Text. , . , , # string, StringBuilder.





:


: 2017-02-25; !; : 506 |


:

:

, .
==> ...

1512 - | 1342 -


© 2015-2024 lektsii.org - -

: 0.231 .