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

C#

PROGRAM CONSTANT EXPRESSIONS

constant expression describes a snippet of code that Acontains a constant value that the compiler evaluates

when your project compiles. An example of a constant value is x = 5. A constant expression contains 1 of

16 types and 1 of 9 different constructs.

The type of a constant expression includes the following: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, any enumeration type, or null. Some of these types may be familiar to you, such as the int type declaring an integer.

These types will be explored in more detail in this chapter, and you can also view all of the types and their associated value ranges in the MDE window online help.

The constructs you can use in a constant expression include literal keywords (null, true, and false), references to other constant expressions in classes and structs, references to members of enumeration types, nested constant expressions, cast expressions (the conversion of an expression into a type), predefined arithmetic operators

(+, *, and /), and the ?: conditional operator that determines whether one or another value is true. You will not know the results from your constant expression until you compile and run your project.

PROGRAM CONSTANT EXPRESSIONS

Console

Applicatio

n

Properties

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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 include a constant in another constant expression.

TYPE THIS:

using System; class Zero

{

public const a = 5;

public const b = a + 10; public static void Main()

{

Console.WriteLine(b

}

}

When the compiler checks for constant expressions, it will do so even if the constant expression is nested within a non-constant construct. If the constant returns an overflow, such as a divide by zero error, then the compiler will return a compile-time error for you to resolve.

The only constant expressions that can apply to reference types are string and null because reference types do not contain actual data — only references to that data.

RESULT:

15

The Class1.cs code appears in the parent window.

Á Delete the comments within the Main method.

Type the code that specifies the constant expression and outputs the expression using the object name (Class1) and variable (x).

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The constant expressions

 

appear onscreen.

 

89

C#

SPECIFY VALUE TYPES

ou cannot create a Visual C# project without value Ytypes. Value types come in two types: struct and

enumeration.

Fourteen other value types exist besides the struct and enum types; Visual C# groups these types into simple types.

Eleven of these twelve simple types are numeric, and the remaining simple value type, bool, is a Boolean value.

These numeric types define the types of numbers that you have specified or you want the user to enter in a field.

Visual C# contains a built-in System namespace that contains all the reference information for predefined types.

The simple types act as aliases for these predefined types that the compiler uses when you compile your project. Visual C# also has two other predefined types, object and string, that are not simple types because they are used with reference types. Unlike reference types, value types cannot contain the null value.

Each value type contains an implicit constructor that tells the compiler to initialize the default value if you do not specify a value. The default values appear in the Default Values Table help page that you can access from online help in the MDE window.

SPECIFY VALUE TYPES

Properties

¤ Click New Project.

 

The New Project window

 

 

appears.

 

 

Click the Console

 

 

 

 

Application icon in the

 

 

Templates pane.

Type a name for the file.

ˇ Click OK.

WORKING WITH TYPES AND INTERFACES 5

You can display the actual value type for any C# type using the method GetType().

TYPE THIS:

RESULT:

using System;

class Type; Uint64

{

public static void Main()

{

Console.WriteLine(15500L.GetType());

}

}

The Class1.cs code appears in the parent window.

Á Delete the comments within the Main method.

Type the code to specify value type variables and output those variables.

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The values appear

 

onscreen.

 

91

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