9
1 Symbols

Symbols

Embed Size (px)

DESCRIPTION

Symbols. Identifiers. Syntax Identifier = (letter | '_' | '@') {letter | digit | '_'}. Unicode! Case-sensitive "@" can be used to treat keywords as identifiers -if ... keyword -@if... identifier if May contain Unicode escape sequences (e.g. \u03c0 for p) Examples someName - PowerPoint PPT Presentation

Citation preview

Page 1: Symbols

1

Symbols

Page 2: Symbols

2

IdentifiersSyntax

Identifier = (letter | '_' | '@') {letter | digit | '_'}.• Unicode!• Case-sensitive• "@" can be used to treat keywords as identifiers

- if ... keyword- @if ... identifier if

• May contain Unicode escape sequences (e.g. \u03c0 for p)

Examples

someNamesum_of3_10percent@while the identifier whilep the identifier p\u03c0 the identifier pb\u0061ck the identifier back

Page 3: Symbols

3

Keywords

abstract as base bool breakbyte case catch char checkedclass const continue decimal defaultdelegate do double else enumevent explicit extern false finallyfixed float for foreach gotoif implicit in int interfaceinternal is lock long namespacenew null object operator outoverride params private protected publicreadonly ref return sbyte sealedshort sizeof stackalloc static stringstruct switch this throw truetry typeof uint ulong uncheckedunsafe ushort using virtual voidvolatile while

77 keywords in C# in contrast to 47 keywords in Java

Page 4: Symbols

4

Naming ConventionsCasing

• Words are capitalized (e.g. ShowDialog)• First letter in upper case, except for private or local variables, constants and fields

constants upper case SIZE, MAX_VALUElocal variables lower case i, top, sumprivate fields lower case data, lastElementpublic fields upper case Width, BufferLengthproperties upper case Length, FullNameenumeration upper case Red, Blue

constantsmethods upper case Add, IndexOftypes upper case StringBuilder (predefined types in lower case: int, string)namespaces upper case System, Collections

First word

• Names of void methods should start with a verb (e.g. GetHashCode)• Other names should start with a noun (e.g. size, IndexOf, Collections)• Enumeration constants or bool members may start with an adjective (Red, Empty)

Page 5: Symbols

5

Integer NumbersSyntax

DecConstant = digit {digit} {IntSuffix}.HexConstant = "0x" hexDigit {hexDigit} {IntSuffix}.IntSuffix = 'u' | 'U' | 'l' | 'L'.

Type

without suffix: smallest type from int, uint, long, ulongsuffix u, U: smallest type from uint, ulongsuffix l, L: smallest type from long, ulong

Examples

17 int9876543210 long17L long17u uint0x3f int0x10000 long0x3fL long

Page 6: Symbols

6

Floating-point Numbers

Syntax (simplified)

RealConstant = [Digits] ["." [Digits]] [Exp] [RealSuffix].must have at least 1 digit and either ".", Exp or RealSuffixDigits = digit {digit}.Exp = ("e" | "E") ["+" | "-"] Digits.RealSuffix = "f" | "F" | "d" | "D" | "m" | "M".

Type

without suffix: doublesuffix f, F: floatsuffix d, D: doublesuffix m, M: decimal

Examples3.14 double1E-2 double.1 double10f float

Page 7: Symbols

7

Character Constants and StringsSyntax

CharConstant = ' char '.StringConstant = " {char} ".

char can be

Any character except closing quote, end-of-line or \Escape sequences

\' '\" "\\ \\0 0x0000\a 0x0007 (alert)\b 0x0008 (backspace)\f 0x000c (form feed)\n 0x000a (new line)\r 0x000d (carriage return)\t 0x0009 (horizontal tab)\v 0x000b (vertical tab)

Unicode- or hexadecimal escape sequences\u0061 a\x0061 a

Page 8: Symbols

8

Character Constants and Strings (cont.)Examples for escape sequences in strings

"file \"C:\\sample.txt\""file "C:\sample.txt""file \x0022C:\u005csample.txt\x0022"

If the string is preceded by a @

• the \ is not interpreted as a escape character• any " must be doubled• the string may span several lines

Example@"file file

""C:\sample.txt""" "C:\sample.txt"

Page 9: Symbols

9

CommentsSingle-line comments

// a comment

Delimited comments/* a comment */must not be nested

Documentation comments/// a documentation comment