C# 0 The Complete Reference


int . Thus, an integer literal is either of type  int



Download 4,07 Mb.
Pdf ko'rish
bet100/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   96   97   98   99   100   101   102   103   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

int

. Thus, an integer literal is either of type 



int

,

uint

,

long

, or 


ulong

,

depending upon its value. Second, floating-point literals are of type 



double

.

If C#’s default type is not what you want for a literal, you can explicitly specify its type 



by including a suffix. To specify a 

long

 literal, append an 



l

 or an 


L

. For example, 12 is an 



int

, but 12L is a 



long

. To specify an unsigned integer value, append a 



u

 or 


U.

 Thus, 100 is 

an

int

, but 100U is a 



uint

. To specify an unsigned, long integer, use 



ul

 or 


UL.

 For example, 

984375UL is of type 

ulong

.

To specify a 



float

 literal, append an 



F

 or 


f

 to the constant. For example, 10.19F is of type 



float

. Although redundant, you can specify a 



double

 literal by appending a 



D

 or 


d

. (As just 

mentioned, floating-point literals are 

double

 by default.)

To specify a 

decimal

 literal, follow its value with an 



m

 or 


M

. For example, 9.95M is a 



decimal

 literal.

www.freepdf-books.com



PART I

C h a p t e r   3 :  

D a t a   T y p e s ,   L i t e r a l s ,   a n d   V a r i a b l e s  

47

PART IPART I



Although integer literals create an 

int

,

uint

,

 long

, or


 ulong

 value by default, they can 

still be assigned to variables of type 

byte

,

 sbyte

,

 short

, or 


ushort

 as long as the value being 

assigned can be represented by the target type.

Hexadecimal Literals

As you probably know, in programming it is sometimes easier to use a number system based 

on 16 instead of 10. The base 16 number system is called 

hexadecimal

 and uses the digits 0 

through 9 plus the letters A through F, which stand for 10, 11, 12, 13, 14, and 15. For example, 

the hexadecimal number 10 is 16 in decimal. Because of the frequency with which hexadecimal 

numbers are used, C# allows you to specify integer literals in hexadecimal format. A 

hexadecimal literal must begin with 



0x

 (a 0 followed by an 



x

). Here are some examples:

count = 0xFF; // 255 in decimal

incr = 0x1a;  // 26 in decimal



Character Escape Sequences

Enclosing character literals in single quotes works for most printing characters, but a few 

characters, such as the carriage return, pose a special problem when a text editor is used. 

In addition, certain other characters, such as the single and double quotes, have special 

meaning in C#, so you cannot use them directly. For these reasons, C# provides special 

escape sequences,

 sometimes referred to as 



backslash character constants,

 shown in Table 3-2. 

These sequences are used in place of the characters they represent.

For example, this assigns 



ch

 the tab character:

ch = '\t';

The next example assigns a single quote to 



ch

:

ch = '\'';



Escape Sequence

Description

\a

Aler t (bell)



\b

Backspace

\f

Form feed



\n

New line (linefeed)

\r

Carriage return



\t

Horizontal tab

\v

Ver tical tab



\0

Null


\'

Single quote

\"

Double quote



\\

Backslash

T

ABLE


 3-2 

Character Escape Sequences

www.freepdf-books.com



48

 

P a r t   I :  



T h e   C #   L a n g u a g e

String Literals

C# supports one other type of literal: the 



string.

 A string literal is a set of characters enclosed 

by double quotes. For example,

"this is a test"

is a string. You have seen examples of strings in many of the 

WriteLine( )

 statements in the 

preceding sample programs.

In addition to normal characters, a string literal can also contain one or more of the 

escape sequences just described. For example, consider the following program. It uses 

the


\n

 and 


\t

 escape sequences.

// Demonstrate escape sequences in strings.

using System;

class StrDemo {

  static void Main() {

    Console.WriteLine("Line One\nLine Two\nLine Three");

    Console.WriteLine("One\tTwo\tThree");

    Console.WriteLine("Four\tFive\tSix");

    // Embed quotes.

    Console.WriteLine("\"Why?\", he asked.");

  }


}

The output is shown here:

Line One

Line Two


Line Three

One     Two     Three

Four    Five    Six

"Why?", he asked.

Notice how the 

\n

 escape sequence is used to generate a new line. You don’t need to use 

multiple

WriteLine( )

 statements to get multiline output. Just embed 



\n

 within a longer 

string at the points where you want the new lines to occur. Also note how a quotation mark 

is generated inside a string.

In addition to the form of string literal just described, you can also specify a 

verbatim

string literal.

 A verbatim string literal begins with an 



@

, which is followed by a quoted string. 

The contents of the quoted string are accepted without modification and can span two or 

more lines. Thus, you can include newlines, tabs, and so on, but you don’t need to use the 

escape sequences. The only exception is that to obtain a double quote (

), you must use two 

double quotes in a row (

“”

). Here is a program that demonstrates verbatim string literals:

// Demonstrate verbatim literal strings.

using System;

class Verbatim {

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   96   97   98   99   100   101   102   103   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish