Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

IMPLEMENT A COLLECTIONS CLASS

Acollections class collects a number of elements that have a specific type, such as a set of numbers that represent the months of the year. C# provides two

methods for declaring collections classes: programming arrays and programming the built-in IEnumerator and IEnumerable interfaces.

An array is built from the System.Array base class that is built into C#. C# identifies this base class as a collections class. You can also define a class as a collections class provided that you declare the System.Collections namespace in your program and include the IEnumerator and IEnumerable interfaces within the class.

The IEnumerator and IEnumerable interfaces let you enumerate elements in your collections class. Enumerations are discussed on page 156, but as a sneak preview, enumerations assign numbers to elements in your collections class so you and your program can keep track of your elements more easily.

Like an array, you can retrieve information from a collections class using the foreach statement. The foreach statement works on a collections class the same way it works in an array — the foreach statement iterates through each element in the collections class and can return that information to another statement or method in your program such as the Console.WriteLine statement for output.

IMPLEMENT A COLLECTIONS CLASS

Project window

Console icon in the

pane.

name for the file.

.

Á Delete all code after the left brace directly below the namespace Implement code.

Type the code that establishes the array, establishes the

GetEnumerator definition, and defines part of the

Enumerator class.

USING ARRAYS 7

Like an array, you can use the foreach statement for iterating through a collections class. The following example acquires a collection in a hashtable, a predefined collection class.

 

TYPE THIS:

 

 

RESULT:

 

 

 

 

 

 

 

 

 

 

 

 

using System;

 

 

209

Stockton

 

 

using System.Collections;

 

 

559

Fresno

 

 

public class Class1

 

 

916

Sacramento

 

 

{

 

 

 

 

 

 

public static void Main(string[] args)

 

 

 

 

 

{

 

 

 

 

 

 

Hashtable areacode = new Hashtable();

 

 

 

 

 

areacode.Add("209", "Stockton");

 

 

 

 

 

areacode Add("559", "Fresno");

 

 

 

 

 

areacode Add("916", "Sacramento");

 

 

 

 

 

foreach (string code in areacode.Keys)

 

 

 

 

 

{

 

 

 

 

 

 

Console.WriteLine(code + "

" + areacode[code]);

 

 

 

 

 

}

 

 

 

 

 

 

}

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

° Type the remainder of the

 

 

 

 

· Type the MainClass

 

Enumerator class code.

code that iterates through the

 

 

 

 

collections class and outputs

 

 

 

 

its elements.

Run the program by

Save the program as the

pressing the F5 key.

filename.

The collections class

 

elements appear on the

 

screen.

 

151

C#

PROGRAM STRUCTS

The struct is a close relative of the class. A struct can have the same members of a class and can also implement interfaces. However, a struct is a value type

so it will simply process information, such as integers passed through an array, as any other value type instead of instantiating objects for each element in the array as a class would. Using structs can save memory and help your program run faster.

You create an object in the struct by using the new operator. After you create the object, C# will create the object and call the value for the object. For example, you

can create an integer object that gets its value from a method contained in a class.

Because a struct is a value type, you cannot inherit from other structs and you cannot use a struct as a base class. A struct can inherit from an object in a base class but not from any inheriting classes.

When you create and run a program with a struct, C# creates the struct on the memory stack instead of the heap. Structs use attributes for specifying the memory areas the struct accesses. C# contains several different built-in struct attributes that you can use for certain tasks.

PROGRAM STRUCTS

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

Á Delete all code after the left brace directly below the namespace Struct code.

Type the struct property values.

The struct attributes mentioned in this task are different from the value type attribute modifiers that determine the accessibility of your struct. You enter the attribute information immediately before you enter the struct declaration, and the attribute appears within closed square brackets ([]).

TYPE THIS:

Using System;

[StructLayout(LayoutKind.Union)]

struct Union

{z

// Add struct information here.

}

USING ARRAYS 7

RESULT:

This code establishes a struct that contains the

StructLayout (LayoutKind.Union) attribute.

° Type the output code and the Main method that contains the struct value.

· Run the program by

Save the program as the

pressing the F5 key.

filename.

The struct value appears

 

on the screen.

 

153

Соседние файлы в папке c#