.


:




:

































 

 

 

 





# , , . : - , , . , .

.NET Framework

.NET Framework , #. , System.IComparable CompareTo(), , . , , , . , System.Collections.ICollection , System.Collections.IEnumerator - .

 

, . , , , . , , . ( ) . # , , , .

struct . :

struct :

{

//

}

.

. (, , #, object.) , . , : , , , , . , . , (.. ). , , , . , . , abstract, virtual protected.

new , , . new, , . , - , . .

.

12.14

// .

 

using System;

 

// .

struct Book

{

public string Author;

public string Title;

public int Copyright;

 

public Book(string a, string t, int c) {

Author = a;

Title = t;

Copyright = c;

}

}

 

// Book.

class StructDemo

{

static void Main()

{

Book book1 = new Book(" ",

" C# 4.0",

2010); //

 

Book book2 = new Book(); //

Book book3; //

 

Console.WriteLine(book1.Title + " by " + book1.Author +

", (c) " + book1.Copyright);

Console.WriteLine();

 

if(book2.Title == null)

Console.WriteLine(" book2.Title .");

 

// book2.

book2.Title = " ";

book2.Author = " ";

book2.Copyright = 1932;

Console.Write(" book2 : ");

Console.WriteLine(book2.Title + " by " + book2.Author +

", (c) " + book2.Copyright);

 

Console.WriteLine();

 

// Console.WriteLine(book3.Title); // ,

//

book3.Title = " ";

 

Console.WriteLine(book3.Title); //

}

}

.

, # 4.0, () 2010

 

book2.Title .

book2 :

, , () 1932

 

, new . , new, , ( ), , . new , b3, , , .

, . . , , , . , . .

12.15

// .

 

using System;

 

// .

struct MyStruct {

public int x;

}

 

// .

class StructAssignment

{

static void Main()

{

MyStruct a;

MyStruct b;

 

a.x = 10;

b.x = 20;

 

Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);

 

a = b;

b.x = 30;

 

Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);

}

}

.

. 10, b.x 20

a.x 20, b.x 30

,

= b;

b - , .. b , , b. , b , . , .

12.16

// .

 

using System;

 

// .

class MyClass {

public int x;

}

 

// .

class ClassAssignment

{

static void Main()

{

MyClass a = new MyClass();

MyClass b = new MyClass();

 

a.x = 10;

b.x = 20;

 

Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);

 

a = b;

b.x = 30;

 

Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x);

}

}

.

. 10, b.x 20

a.x 30, b.x 30

, b , , .. , b.

: # , , ? . , , . , , . , , . , - , . , . , , , .

, . . , . . , , .

12.17

// .

 

using System;

 

// .

struct PacketHeader

{

public uint PackNum; //

public ushort PackLen; //

}

 

// PacketHeader

// .

class Transaction

{

static uint transacNum = 0;

 

PacketHeader ph; // PacketHeader

// Transaction

string accountNum;

double amount;

 

public Transaction(string acc, double val) {

//

ph.PackNum = transacNum++;

ph.PackLen = 512; //

 

accountNum = acc;

amount = val;

}

 

// .

public void sendTransaction()

{

Console.WriteLine(" #: " + ph.PackNum +

", : " + ph.PackLen +

",\n #: " + accountNum +

", : {0:C}\n", amount);

}

}

 

//

class PacketDemo

{

static void Main()

{

Transaction t = new Transaction("31243", -100.12);

Transaction t2 = new Transaction("AB4655", 345.25);

Transaction t3 = new Transaction("8475-09", 9800.00);

 

t.sendTransaction();

t2.sendTransaction();

t3.sendTransaction();

}

}

.

#: 0, : 512,

#: 31243, : $100.12

 

#: 1, : 512,

#: AB4655, : $345.25

 

#: 2, : 512,

#: 8475-09, : $9,800.12

PacketHeader , , . , PacketHeader , , . , PacketHeader , .

, ++ struct. , #. , ++ , , , . # , - .

. enum. :

enum { _ };

- , _ - , .

Apple .

enum Apple { Jonathan, GoldenDel, RedDel, Winesap,

Cortland, Mcintosh };

, . # , , . , . , , , switch for.

, , . . , Apple Jonathan , GoldenDel - 1, RedDel - 2 ..

, -. ,

Console.WriteLine(Apple.RedDel + " " +

(int)Apple.RedDel);

.

RedDel 2

, . int.

, Apple.

12.18

// .

 

using System;

 

class EnumDemo {

enum Apple { Jonathan, GoldenDel, RedDel, Winesap,

Cortland, McIntosh };

 

static void Main() {

string[] color = {

"",

"",

"",

" ",

" ",

"-"

};

 

Apple i; //

 

// i

// .

for(i = Apple.Jonathan; i <= Apple.McIntosh; i++)

Console.WriteLine(i + " " + (int)i);

 

Console.WriteLine();

 

// .

for(i = Apple.Jonathan; i <= Apple.McIntosh; i++)

Console.WriteLine(" " + i + " is " +

color[(int)i]);

}

}

.

Jonathan 0

GoldenDel 1

RedDel 2

Winsap - 3

Cortland 4

Mcintosh 5

 

Jonathan -

GoldenDel -

RedDel -

Winsap -

Cortland -

Mcintosh - -

, Apple for. Apple , , . , . , # , .

: System.Enum, System.ValueType, , , - object.





:


: 2016-12-28; !; : 386 |


:

:

- , 20 40 . - .
==> ...

1592 - | 1548 -


© 2015-2024 lektsii.org - -

: 0.069 .