0% found this document useful (0 votes)
24 views

Syntax C

C# syntax is based on C and C++. It uses identifiers for names, keywords for special compiler terms, literals for embedded data values, and punctuation/operators to structure code. Comments can be single-line or multiline, and types define value blueprints. The example program declares an int variable x, assigns it the result of a multiplication, and prints x to the console.

Uploaded by

Md. Kamrul Hoque
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Syntax C

C# syntax is based on C and C++. It uses identifiers for names, keywords for special compiler terms, literals for embedded data values, and punctuation/operators to structure code. Comments can be single-line or multiline, and types define value blueprints. The example program declares an int variable x, assigns it the result of a multiplication, and prints x to the console.

Uploaded by

Md. Kamrul Hoque
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Syntax C# syntax is inspired by C and C++ syntax.

In this section, we describe C#’s elements of syntax,


using the following program: using System; int x = 12 * 30; Console.WriteLine (x); Identifiers and
Keywords Identifiers are names that programmers choose for their classes, methods, variables, and so
on. Here are the identifiers in our example program, in the order in which they appear: System x Console
WriteLine An identifier must be a whole word, essentially made up of Unicode characters starting with a
letter or underscore. C# identifiers are case sensitive. By convention, parameters, local variables, and
private fields should be in camel case (e.g., myVaria ble), and all other identifiers should be in Pascal case
(e.g., MyMethod). Keywords are names that mean something special to the compiler. There are two
keywords in our example program, using and int. Most keywords are reserved, which means that you
can’t use them as identifiers. Here is the full list of C# reserved keywords: abstract as base bool break
byte case catch char checked class const continue decimal default delegate do double else enum event
explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long
namespace new null object operator out override params private protected public readonly record ref
return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong
unchecked unsafe ushort using virtual void volatile while 28 | Chapter 2: C# Language Basics If you really
want to use an identifier that clashes with a reserved keyword, you can do so by qualifying it with the @
prefix; for instance: int using = 123; // Illegal int @using = 123; // Legal The @ symbol doesn’t form part
of the identifier itself. So, @myVariable is the same as myVariable. Contextual keywords Some keywords
are contextual, meaning that you also can use them as identifiers— without an @ symbol: add alias and
ascending async await by descending dynamic equals from get global group init into join let managed
nameof nint not notnull nuint on or orderby partial remove select set unmanaged value var with when
where yield With contextual keywords, ambiguity cannot arise within the context in which they are used.
Literals, Punctuators, and Operators Literals are primitive pieces of data lexically embedded into the
program. The literals we used in our example program are 12 and 30. Punctuators help demarcate the
structure of the program. An example is the semi‐ colon, which terminates a statement. Statements can
wrap multiple lines: Console.WriteLine (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10); An operator transforms and
combines expressions. Most operators in C# are deno‐ ted with a symbol, such as the multiplication
operator, *. We discuss operators in more detail later in this chapter. These are the operators we used in
our example program: = * . () A period denotes a member of something (or a decimal point with numeric
literals). Parentheses are used when declaring or calling a method; empty parentheses are used when
the method accepts no arguments. (Parentheses also have other purposes that you’ll see later in this
chapter.) An equals sign performs assignment. (The double equals sign, ==, performs equality
comparison, as you’ll see later.) Syntax | 29 C# Language Basics Comments C# offers two different styles
of source-code documentation: single-line comments and multiline comments. A single-line comment
begins with a double forward slash and continues until the end of the line; for example: int x = 3; //
Comment about assigning 3 to x A multiline comment begins with /* and ends with */; for example: int x
= 3; /* This is a comment that spans two lines */ Comments can embed XML documentation tags, which
we explain in “XML Docu‐ mentation” on page 252. Type Basics A type defines the blueprint for a value.
In this example, we use two literals of type int with values 12 and 30. We also declare a variable of type
int whose name is x: int x = 12 * 30; Console.WriteLine (x); Because most of the code listings in this book
require types from the System namespace, we will omit “using System” from now on, unless we’re
illustrating a concept relating to namespaces. A variable denotes a storage location that can contain
different values over time. In contrast, a constant always represents the same value (more on this later):
const int y = 360; All values in C# are instances of a type. The meaning of a value and the set of possible
values a variable can have are determined by its type.

You might also like