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

C#

PROGRAM NUMERIC TYPES

umeric types let you specify the type of number you Nassign to a variable. By assigning numbers to

variables, you can perform different calculations. Three different categories of types comprise the numeric types: integral, floating-point, and decimal.

The two most common numeric types are integral and decimal because we use those two number types most often. The integral type category has the most number of types because Visual C# categorizes integer types by the range of the integer. In one case, the char type, the integer is not a number at all.

Visual C# divides the integer ranges into four main groups: byte, short, int, and long. Of these four groups, you can specify whether the integer type is signed or unsigned. A signed integer type contains negative numbers in its

range and an unsigned integer contains a number range that starts with 0.

The number of digits in each integer group provides the most obvious information about the differences between the four groups. The byte group contains numbers up to three digits, the short type contains numbers up to five digits, the int type contains numbers up to ten digits, and the long type contains numbers up to 19 digits.

The char type is an integer that represents a Unicode character set value that ranges from 0 to 65535.

PROGRAM NUMERIC 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 determine whether an integer type is signed or unsigned by adding an s or a u before the type name. Only the byte type requires an s in front (thus sbyte) so you can signify the byte as signed. The other three types — short, int, and long — require you to precede those type names so you can signify those types as unsigned.

The Unicode character set is a worldwide standard set that applies numbers to different characters for most written languages throughout the world. When you declare a char variable, you can declare the variable as a letter or with the Unicode number that applies to that letter. For example, you can include a char line with char Letter = 'X';.

You can also provide the Unicode equivalent in place of X, as in char Letter = '\u0058';.

When you enter a Unicode character number you must include the Unicode number in single quotes, precede the number with a backslash and u, and also ensure that the Unicode number has four digits.

You can convert a char value to several other integer types including ushort, int, uint, long, and ulong. However, you cannot convert other integer types (or any other numeric type) to the char type.

The Class1.cs code appears in the parent window.

Á Delete the comments within the Main method.

Type the code that adds two integral expressions and outputs the combined expression.

° Run the program by

The combined expression

pressing the F5 key.

appears onscreen.

CONTINUED

93

C#

PROGRAM NUMERIC TYPES

loating and decimal types make up the two other Fcategories of numeric types that Visual C# supports.

Visual C# offers two different floating point types: float and double. You can use the float type for very large numbers — the float range is from ±1.5 x 10 –45 to

±3.4 x 1038, and the float type rounds off numbers to seven digits. You must denote a float type by using the suffix f after the floating point value.

If you need even larger numbers, the double type gives you a far greater range — ±5.0 x 10 –324 to ±1.7 x 10308 — and it rounds off numbers to 15 or 16 digits depending on the number. Double numbers require no suffix after the value.

The decimal type does not give you the range of the floating point type — the decimal type ranges from 1.0 x

10 –28 to 7.9 x 1028 — but it does give you greater precision by rounding off numbers to 28 or 29 digits depending on the number.

You must denote a decimal type by using the suffix m after the decimal value. If you do not use the f and m suffixes for floating-point and decimal values, the value will be treated as a double-value, and your project cannot compile.

PROGRAM NUMERIC TYPES (CONTINUED)

Run the program by pressing the F5 key.

The integer and float values appear onscreen.

WORKING WITH TYPES AND INTERFACES 5

If you want to enter a Unicode character, you can do so in C#. The Unicode character set is a worldwide standard set that applies numbers to different characters for most written languages throughout the world. When you declare a char variable, you can declare the variable as a letter or with the Unicode number that applies to that letter.

 

TYPE THIS:

 

RESULT:

 

 

 

using System;

 

X

 

class Character;

 

 

 

 

 

{

 

X

 

char Letter1 = ‘X’;

 

 

 

char Letter2 = ‘\u0058’

 

 

 

public static void Main()

 

 

 

{

 

 

 

Console.WriteLine(Letter1);

 

 

 

Console.WriteLine(Letter2);

 

 

 

}

 

 

 

}

 

 

 

 

 

 

 

 

 

 

You can mix integral and floating point types in one expression. When you mix types, the integral types will convert into floating point types. However, you cannot mix decimal types with integral or floating point types. Make sure to denote the decimal with the m suffix otherwise your project will not compile.

Add code to

the

output character

 

95

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