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

C#

ADD REFERENCE TYPES

C# categorizes elements refer to data elsewhere in a program as reference types. Reference types let you access data that you need within different places

in your program. For example, you may have several blocks of code that need to refer to the boiling temperature of water.

The reference type category contains several smaller categories including declarative and built-in types.

Declarative reference type elements include classes, interfaces, and delegates. These elements contain values and code that performs certain functions such as arithmetical operations.

Built-in reference types include objects and strings. An object is a collection of data and functionality. For example,

an object can be a variable with a value assigned to it, such as x = 1.

A string is a collection of characters for displaying output on screen. With string reference types, you can compare the values of the strings using the Visual C# equality operators — the == or =! operators — or other operators such as the additive operator, the plus sign, +. For example, you can define two strings and see if they are equal as shown below:

string a = "Tigger"

string b = "is a cat."

Console.WriteLine ( a + b );

The above code block would return with the output

Tigger is a cat.

ADD REFERENCE TYPES

¤ Click New Project in the Start page.

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for your file.

ˇ Click OK.

WORKING WITH VISUAL C# BASICS 3

You can reverse the boxing process by using a process called unboxing. Unboxing converts an object to a value type. When Visual C# unboxes an object, it checks the object instance to make sure that the instance is the boxed value of the given value type (such as an integer), and then Visual C# copies the value of the instance into the value type variable.

TYPE THIS:

using System; public BoxClass

{

public static void Main()

{

int TiggerAge = 11;

object box = TiggerAge; // boxes the TiggerAge

value

int UnBoxedAge = (int)box; // Unboxes the

value

Console.WriteLine("The unboxed value is {0}", UnBoxedAge);

}

}

RESULT:

The unboxed value is

11.

 

 

 

 

 

The Class1.cs code

 

Type the code that

 

appears in the parent window.

specifies strings and

Á Delete the comments

concatenates them.

 

 

 

within the Main method.

 

 

 

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The concatenated string

 

appears on the screen.

 

43

C#

ADD OPERATORS

Your program cannot operate without operators, which are mathematical symbols that perform a wide variety of functions. These operators compare, combine, and

contrast values so your program can make choices. For example, your program can refuse to perform a particular action if a user-entered value such as a password is not the same as a stored password in a program.

C# places operators into sixteen different categories. Some operators calculate arithmetical algorithms such as number addition and subtraction. Some arithmetical operators let you control calculation overflow errors, such as divide by zero errors, that can cause a program crash.

Some operators are logical — they calculate whether a condition is true or not such as a user ID number matching the ID number on file within the program. Other operators are relational and determine whether a value is greater than, equal to, or less than another value.

Other operators assign values to variables by using the equals sign or a combination of the equals sign and another operator. For example, if you have the arguments x = 1 and x + = 6, then that is the equivalent of x = 1 + 6.

The most important operator of all is the new operator that lets you create new objects, such as classes and variables, in your program.

ADD OPERATORS

Console

Applicatio

¤ Click New Project in the

.NET

Start page.

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for your file.

ˇ Click OK.

WORKING WITH VISUAL C# BASICS 3

Visual C# gives you the ability to overload operators. You can create your own operations when one or both of the operands are of a user-defined class or struct type.

 

 

TYPE THIS:

 

 

RESULT:

 

 

 

 

 

 

using System;

 

 

Multiply Value: 30

 

 

 

class Multiply {

 

 

 

 

 

 

 

int number;

 

 

 

 

 

 

 

public Integer(int number) {

 

 

 

 

 

 

 

this.number = number; }

 

 

 

 

 

 

 

public static Multiply operator *(Multiply

x, Multiply y) {

 

 

 

 

 

 

return new Multiply(x.number * y.number); }

 

 

 

 

 

 

class Output {

 

 

 

 

 

 

public static void Main() {

 

 

 

 

 

 

Multiply a = new Multiply(3,5);

 

 

 

 

 

 

Multiply b = new Multiply(1,2);

 

 

 

 

 

 

Console.WriteLine("Multiply Value: {0}", (a * b)); }

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

The Class1.cs code appears in the parent window.

Á Delete the comments within the Main method.

Type the code that specifies integer values and combines them using four arithmetic operators.

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The concatenated string

 

appears on the screen.

 

45

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