Friday, March 10, 2017

Lesson 3: Identifiers, Keywords and Escape Sequence


Identifiers

Identifiers are tokens used to uniquely identify such programming elements as variables, fields, classes, interfaces or methods. In c#, identifiers are case-sensitive and are used whenever we want to reference a particular part of code. C# identifiers are a combination of letters, numbers and underscores.

Keywords

C# identifiers cannot be keywords, reserved words reserved for use by C#. Such special words as class, return, int, and string are typical instances of keywords in c#.

Table below shows the keywords in any part of C# program.


Keyword in C#
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
in(generic modifier)
int
interface
internal
is
lock
long
namespace
new
null
object
operator
out
out (generic modifier)
override
params
private
protected
public
readonly
ref
return
sbyte
sealed
short
sizeof
stackalloc
static
string
struct
switch
this
throw
true
try
typeof
unit
ulong
unchecked
usafae
ushort
using
virtual
void
volatile
while

C# also has other types of keywords called Contextual Keywords. A contextual keyword is used to provide a specific meaning in the code, but it is not a reserved word in C#; as new keywords are added to the C# language, they are added as contextual keywords in order to avoid breaking programs written in earlier versions.


Contextual keywords in C#
add
alias
ascending
async
await
descending
dynamic
from
get
global
group
into
join
let
orderby
partial ﴾type﴿
partial ﴾method﴿
remove
select
set
value
var
where ﴾generic type constraint﴿
where ﴾query clause﴿
yield

Naming Identifiers

In programming, there are two naming conventions of an identifier – Camel Casting and Pascal Casting.

1. Camel Casting: It is a naming format where the second word in an identifier is begins with a capital letter, it is used mostly with variable names.
Ex. totalRevenue, totalExpense

2. Pascal Casting –a naming convention in which the first letter of the identifier is capitalized; it is used mostly to name methods, classes, namespaces and interfaces.
Ex. GetData(), SetData(), Employee, IEmployee, IO.
In C#, a valid identifier name follows the following rules:

    Rule 1An identifier must start with a letter or an underscore.
    Rule 2: After the first character, it may contain numbers, letters, connectors, etc
    Rule 3If the identifier is a keyword, it must be prefixed by @ symbol (at symbol). 

Escape Sequence

Like any c-based languages, C# contains character combinations consisting of a backslash (/) followed by a specific token, and this character combination is called escape sequence. The following table summarizes C# escape sequences.


Escape Sequence
Name
Description
\’
Inserts a single quote in string literal – e.g., Console.Write(“\’single quotes\’);
\”
Inserts a double quote in string literal – e.g., Console.Write(“\”single quotes\”);
\n
Inserts new line
\t
Inserts a horizontal tab into a string literal
\\
Inserts a backslash into a string literal; it can be quite helpful when dealing with a file or network path
\a
Triggers a system alert (beep); and for console applications, it can be an audio clue to the user.

The following program demonstrates the use of escape sequence in C#.


/*
 * Author: Temesghen Tekeste
 * Displaying multiple lines with a single statement.
 * */
using System;
public class EscapeSequenceTest
{
    // C# app begins executing inside the Main method
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello\n there!\n  This\n  is\n"
                            + " escape sequence test in C#");
        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    } // end Main


// end class

OUTPUT:









References:
 http://www.infocodify.com/csharp/csharp_identifiers

No comments:

Post a Comment