.


:




:

































 

 

 

 





, . Person, FirstName LastName ToString () Object:

public class Person

{

public string FirstName { get; set; }

public string LastName { get; set; }

public override string ToString()

{

return String.Format("{0} {1}", FirstName, LastName);

}

}

Person :

Person[] myPersons = new Person[2];

, , . , , NullReferenceException.

6.15.

, 0:

new Person { FirstName="Ayrton", LastName="Senna" };

new Person { FirstName="Michael", LastName="Schumacher" };

. 6.2 , Person. myPersons . Person, . . Person, .

 

 

6.2 - , Person

 

 

int, :

Person[] myPersons2 =

{

new Person { FirstName="Ayrton", LastName="Senna"},

new Person { FirstName="Michael", LastName=Schumacher"}

};

 

( ) . . . 6.3 , . 1, 2 3, -7, 8 9.

6.3 -

#, . ( ). , :

int[,] twodim = new int[3,3];

twodim[0, 0] = 1;

twodim[0, 1] = 2;

twodim[0, 2] = 3;

twodim[1, 0] = 4;

twodim[l, 1] = 5;

twodim[1, 2] = 6;

twodim[2, 0] = 7;

twodim[2, 1].= 8;

twodim[2, 2] = 9;

.

, . , , .

int [,] twodim = {

{1, 2, 3),

{4, 5, 6},

{7, 8, 9}

};

. - .

int[,,] threedim = {

{ { 1, 2 }, { 3, 4 } },

{ { 5, 6 }, { 7, 8 } },

{ { 9, 10 }, { 11, 12 } }

};

Console.WriteLine(threedim[0, 1, 1]);

(, 3x3 ), (jagged) . .

. 6.4 . , , - , - .

 

. 6.4. -

. , . , , , . :

int[] [ ] jagged = new int[3] [ ];

jagged[0] = new int [2] { 1, 2 };

jagged[l] = new int[6] { 3, 4, 5, 6, 7, 8 };

jagged[2] = new int[3] { 9, 10, 11 };

for. for , , for - :

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

{

for (int element = 0; element < jagged[row].Length; element++)

{

Console.WriteLine(": {0}, : {1}, : {2}",

row, element, jagged[row][element]);

}

}

:

: 0, : 0, : 1

: 0, : 0, : 2

: 1, : 0, : 3

: 1, : 1, : 4

: 1, : 2, : 6

: 1, : 3, : 1

: 1, : 4, : 7

: 1, : 5, : 8

: 2, : 0, : 9

: 2, : 1, : 10

: 2, : 2, : 11

Array

- C# Array. C# , Array. , , Array, #., Length foreach. GetEnumerator() Array.

Array : LongLength , , , Rank . Array, .

Array , - . C# Createlnstance (). , , Createlnstance() .

int 5. Createlnstance() , . SetValue(), - GetValue().

Array intArray1 = Array.Createlnstance (typeof (int), 5);

for (int i = 0;- i < 5; i++)

{

intArray1.SetValue(33, i);

}

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

{

Console.WriteLine(intArray1.GetValue(i));

}

, int[]:

int[] intArray2 = (int[])intArray1;

Createlnstance() , , 0. 2x3 . 1, - 10.

int[] lengths = {2, 3};

int[] lowerBounds = {1, 10};

Array racers = Array.Createlnstance(typeof(Person), lengths,

lowerBounds);

SetValue(), , :

racers.SetValue (new Person

{

FirstName = "Alain",

LastName = "Prost"

}, 1, 10);

racers.SetValue(new Person

{

FirstName = "Emerson",

LastName = "Fittipaldi"

}, 1, 11);

racers.SetValue(new Person {

FirstName = "Ayrton",

LastName = "Senna"

}, 1, 12);

racers.SetValue(new Person

{

FirstName = "Ralf",

LastName = "Schumacher"

), 2, 10);

racers.SetValue(new Person

{

FirstName = "Fernando",

LastName = "Alonso"

}, 2, 11);

racers.SetValue(new Person

{

FirstName = "Jenson",

LastName = "Button"

}, 2, 12);

0, #. , :

Person[,] racers2 = (Person[,])racers;

Person first = racers2[l, 10];

Person last = racers2[2, 12];

- , , . ICloneable. Clone(), , (shallow) .

, , , . 6.5.

int[] intArray1 = {1, 2);

int[] intArray2 = (int[])intArrayl.Clone();

, , .

 

6.5 -

. 6.6 beatles beatlesClone, Clone() beatles. Person, beatles beatlesClone, . , beatlesClone, , beatles.

Person[] beatles = {

new Person {FirstName="John", LastName=''Lennon"},

new Person { FirstName="Paul", LastName="McCartney"}

};

Person[] beatlesClone = (Person[])beatles.Clone();

Clone() Array.(), . Clone() () : Clone() , () .

, , .

 

6.6 -

 

 

Array (Quicksort) . Sort() IComparable. , System.String System.Int32, IComparable, , .

names, string, :

string [] names = {

"Christina Aguillera",

"Shakira",

"Beyonce",

"Gwen Stefani"

};

Array.Sort(names);

foreach (var name in names)

{

Console.WriteLine(name);

}

:

Beyonce

Christina Aguillera

Gwen Stefani

Shakira

, IComparable. CompareTo(), 0, , 0, , , 0, , .

Person, IComparable< Person>. LastName. LastName string, String IComparable, CompareTo(). LastName, FirstName.

public class Person: IComparable<Person>

{

public int CompareTo(Person other)

{

if (other == null) throw new ArgumentNullException("other");

int result = this.LastName.CompareTo(other.LastName);

if (result == 0)

{

result = this.FirstName.CompareTo(other.FirstName);

}

return result;

}

//...

Person (LastName):

Person[] persons = {

new Person { FirstName="Daraon", LastName="Hill"),

new Person { FirstName="Niki", LastName="Lauda"),

new Person { FirstName="Ayrton", LastName="Senna"),

new Person { FirstName="Graham", LastName=Hill)

};

Array.Sort(persons);

foreach (var p in persons)

{

Console.WriteLine(p);

}

Person :

Damon Hill

Graham Hill

Niki Lauda

Ayrton Senna

Person - , , , IComparer IComparer<T>. Compare(). , . IComparer . Compare() , . , () IComparable.

PersonComparer IComparer<Person> Person First Name, LastName. PersonCompareType , PersonComparer:FirstName LastName. PersonComparer, PersonCompareType. Compare() switch LastName, FirstName.

public enum PersonCompareType

{

FirstName,

LastName

}

public class PersonComparer: IComparer<Person>

{

private PersonCompareType compareType;

public PersonComparer(PersonCompareType compareType)

{

this.compareType = compareType;

}

public int Compare(Person x, Person y)

{

if(x == null) throw new ArgumentNullException("x");

if(y == null) throw new ArgumentNullException("y");

 

switch (compareType)

{

case PersonCompareType.Firstname:

return x.Firstname.CompareTo(y.FirstName);

case PersonCompareType.Lastname:

return x.Lastname.CompareTo(y.LastName);

default:

throw new ArgumentException(

" ");

}

}

}

PersonComparer Array.Sort().

, :

Array.Sort(persons, new PersonComparer(PersonCompareType.Firstname));

foreach (var p in persons)

{

Console.WriteLine(p);

}

, :

Ayrton Senna

Damon Hill

Graham Hill

Niki Lauda

Array Sort, . , , , IComparable IComparer. 6.8.





:


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


:

:

- , - .
==> ...

1461 - | 1377 -


© 2015-2024 lektsii.org - -

: 0.071 .