Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp - Your Visual Blueprint For Building .NET Applications (2002) [eng].pdf
Скачиваний:
36
Добавлен:
16.08.2013
Размер:
9.71 Mб
Скачать

C#

PROGRAM THE BOOLEAN TYPE

The Boolean type lets you determine if a variable or expression meets one of two criteria: True or False. Using the Boolean type is a good way to determine

how your program functions depending on the values stored in one or more variables in your project.

The Boolean type uses the keyword bool, which is an alias of the System.Boolean type in Visual Studio .NET. You can use the System.Boolean type name as opposed to bool if you wish, but the functionality of the type name and the alias is exactly the same.

You can assign a Boolean value (that is, True or False) or a range of values to a bool keyword. For example, you can tell the bool keyword to check to see if the bool value is True where x > 5 and x < 10. If the value is between 6 and

9, the value will be true, and your project will determine what code block to execute next.

The default value of the Boolean type is False. Therefore, if you enter a bool statement and enter neither the True nor False variables in the statement, Visual C# automatically checks to see if the value in the bool statement is False.

PROGRAM THE BOOLEAN TYPE

Console

Applicatio

Properties

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Click New Project.

The New Project window

 

 

 

 

 

 

 

 

 

 

Type a name for the file.

 

appears.

 

ˇ Click OK.

 

 

Click the Console

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Application icon in the

 

 

 

 

 

 

Templates pane.

 

 

 

 

 

WORKING WITH TYPES AND INTERFACES 5

You can determine whether a particular value meets a certain condition (for example, whether a value is greater than zero) by using Boolean types as the controlling expressions in if and for statements.

TYPE THIS:

using System; class Boolean;

{

int x = 4;

public static void Main()

{

if (x!>= 0)

{

Console.WriteLine("The value of x is greater than zero.");

}

}

}

Unlike C++, which lets you convert a Boolean type to an integer type, Visual C# does not allow any Boolean type conversion.

C++ lets you convert the false state to zero and the true state to a non-zero value. If you want to know if a variable is equal to zero or not, you have to create an if statement that checks if a variable is zero or not.

RESULT:

The value of x is greater than zero.

The Class1 appears in window.

Á Delete the within the

C#

DECLARE REFERENCE TYPES

isual C# includes three reference type keywords:

Vclass, interface, and delegate. These keywords declare reference types, but they are not reference

types in and of themselves. Visual C# includes two built-in reference types: object and string. These reference types act as keywords and also the declaration of a reference type in code.

You can assign values of any type to the variables that you include in the object statement. When you convert reference types to value types and vice versa, you do so by declaring those types within the object type before you convert.

DECLARE REFERENCE TYPES

The string type lets you define strings of Unicode characters that can include words, numbers, or any Unicode character. The string can be enclosed in two forms: quotation marks and quotation marks preceded by the @ symbol. The difference the @ symbol makes is that an escape sequence — the backward slash indicates a Unicode character number — is not processed. This makes it easier to enter a filename with all of its directory information that makes use of backward slashes.

The string type acts like a value type in that you can use equality operators for comparing strings and you can use other operators for combining and accessing string characters.

Console

Applicatio

Properties

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Click New Project.

The New Project window

 

 

 

 

 

 

 

 

 

 

Type a name for the file.

 

appears.

 

ˇ Click OK.

 

 

Click the Console

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Application icon in the

 

 

 

 

 

 

Templates pane.

 

 

 

 

 

WORKING WITH TYPES AND INTERFACES 5

If you want to determine if two strings are the same, such as a user-entered password matching the stored password, you can use the equality (==) and inequality (!=) operators for testing whether two strings are the same as you would with two values in a value type declaration.

TYPE THIS:

using System; class EqualityTest;

{

int x = 4; int y = 5

public static void Main()

{

if (x != 0)

{

Console.WriteLine("The value of x is greater than zero.");

}

if (x == 0)

{

Console.WriteLine("The value of x is zero.");

}

}

}

RESULT:

The value of x is greater than zero.

Á

Соседние файлы в предмете Программирование на C++