Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

//This array contains 4 int values, which default to 0

//Here, the valid indexes are b[0], b[1], b[2], b[3] int[] b = new int[4];

//An array can be initialized directly or with the new keyword

//evens.Length will return 6

//foreach (int p in primes) iterates through the elements in primes int[] evens = { 2, 4, 6, 8, 10, 12 };

int[] primes = new int[] {2, 3, 5, 7, 11, 101, 9876543211 };

//This example shows a 2 by 2 string array

//Here, names[0,0] = "Katie" and names[1,1] = "Bianca"

string[,] names = { { "Katie", "Sydney" }, { "Edmund", "Bianca"} };

//This example shows an array of arrays.

//Here, x[0] is an int array of length three with values 1, 2, 3.

//Also, x[1][1] = 12 and x[2][4] = 25.

//Attempting to reference x[3] or x[1][2] will throw an exception int[][] x = { { 1, 2, 3 }, { 11, 12 }, { 21, 22, 23, 24, 25} };

A.4.3 MAIN

A program has to start somewhere. In C and C++ programs, the global procedure main is the defined entry point for the program. This starting point is referred to as the entry point for the program.

In C#, a class must define a static method called Main to serve as the entry point. The method must have one of the following signatures.

static void Main()

static void Main(string[] args)

static int Main()

static int Main(string[] args)

A program will return a value if the Main method returns a value. A program can receive command-line arguments by specifying an array of string objects as the only parameter to the Main method.

If two or more classes in a program contain a Main method, then the /main switch must be used with the C# compiler to specify which method to consider the entry point for the program.

A.4.4 BOXING

By definition, the object class is a reference type. However, it also serves as the ultimate base class for all types, including the built-in types. As a result, value types such as int and bool can be used wherever an object instance is required. For example, the ArrayList class represents a dynamically-sized array, and includes an Add method to add an object to the array. This method is declared as follows:

public virtual int Add(object value);

SPECIAL FEATURES

671

Within the Add method, a reference type is expected. So what happens when a value type is passed into this method? Clearly, an explicit mechanism for treating value types as a reference type is required.

This mechanism is called boxing. Boxing implicitly copies the data in a value type into an object instance allocated on the heap. For example:

//Boxing of an integer constant object obj = 123;

//Boxing of an int type. ArrayList list = new ArrayList(); int x = 32768;

list.Add(x);

A boxed value is converted back into a value type through a process called unboxing. Conceptually, boxing and unboxing happens automatically and the programmer can remain blissfully unaware of this concept. Boxed values can be treated as their unboxed equivalents. For example:

int n = 5;

object obj = 123;

if (obj is int)

n = (int) obj;

These statements are perfectly legal, and result in the value of 123 for the variable n. The reason boxing is important is because of the performance implications involved. The boxing and unboxing of values takes time, and this can seriously impact the performance of an application.

Note in particular that boxing occurs when a structure, which is a value type, is cast to an interface, which is a reference type. For this reason, care should be taken when creating structures that support one or more interfaces. In such a situation, the performance implications of boxing might warrant using a class instead of a structure.

A.4.5 DOCUMENTATION

A final topic worth mentioning in this appendix is that of automated documentation. C# supports a set of XML-style tags that can be used in comments and extracted by the compiler. Such comments must begin with a triple-slash (///) and can occur before the declaration of most types and type members.

The C# compiler supports the /doc switch to generate the XML documentation file. Details on this process and the resulting output are available in the .NET documentation.

672

APPENDIX A C# PRIMER

The following table provides a summary of the tags that are currently recognized by the compiler. An example using the <summary> tag appears in section 5.2.1 on page 134.

C# documentation tags

Tag

Purpose

 

 

<c>

Specifies text that should be marked as code.

<code>

Specifies multiple lines that should be marked as code.

<example>

Documents an example of a type or method.

<exception>

Specifies documentation for an exception class.

<include>

Includes an external file to include in the documentation.

<list>

Specifies a list of items within another tag. This supports bulleted lists,

 

numbered lists, and tables.

<para>

Starts a new paragraph within another tag.

<param>

Documents a parameter within a method or other construct.

<paramref>

Specifies text that should be marked as a parameter.

<permission>

Documents the accessibility level of a member.

<remarks>

Documents general comments about a type or type member.

<returns>

Documents the return value of a method.

<see>

Specifies a link in running text to another member or field accessible from the

 

current file.

<seealso>

Specifies a link in a See Also section to another member of field accessible form

 

the current file.

<summary>

Documents a short description of the member or type.

<value>

Documents a short description of a property.

 

 

SPECIAL FEATURES

673

A P P E N D I X B

.NET namespaces

B.1

System.Collections 675

B.8

System.Reflection 677

B.2

System.ComponentModel 675

B.9

System.Resources 677

B.3

System.Data 675

B.10

System.Security 678

B.4

System.Drawing 675

B.11

System.Threading 678

B.5

System.Globalization 676

B.12

System.Web 679

B.6

System.IO 676

B.13

System.Windows.Forms 679

B.7

System.Net 676

B.14

System.XML 679

This appendix provides an overview of some of the System namespaces provided by Microsoft in the .NET Framework, and discusses their relationship to Windows Forms applications. For a complete list of namespaces in .NET, see the .NET Framework Class Library documentation.

The System namespace contains the commonly-used types1 required by .NET programs and libraries, as well as services such as data type conversion, environment management, and mathematical operations. In particular, most of the classes mentioned in Appendix A that implement core functionality such as the built-in types, enumerations, and delegates are included in this namespace. Members of this namespace are discussed throughout the book as they are used in the sample programs.

1 The word type is used in the C# sense here, as defined in Appendix A. More generally, a type can be a class, structure, interface, enumeration, or a delegate. By definition, a namespace defines one or more types.

674

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