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

Thursday, March 9, 2017

Lesson 2: Input and Output in C#

Input and Output

WRITE A PROGRAM THAT ASKS A USER WHAT HIS/HER NAME IS AND THEN DISPLAYS THE FOLLOWING TEXT TO THE CONSOLE:
                         Hello, <Name>

Brief Notes: 

  • In C#, every code is written inside a class. A class is a blueprint for creating an object; an object is an instance of a class.
  • The execution of any C# program starts in a special method called the main method.
  • The main method has the following syntax:

                The main method in C#

  • To output text to the console we use Write or WritleLine method of the Console class; for example, 
    • Console.Write("Enter Your Name:");
    • Above code outputs the text Enter Your Name to the console.
  • To input data from the console, for instance, name, we first create a variable (a storage location in memory to hold the name) and then we use the ReadLine method of the Console class. For  example: 
    • string name = Console.ReadLine();
  • Every statement in C# ends in semicolon (;)

Note: We will discuss variables, classes and objects in the coming lessons.


Here is the complete program:
/* 
  * Author: Temesghen Tekeste 
  * email: mail.temesghen.tekeste@gmail.com 
  * */
using System;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("What is your name?");
            string name = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine("Hello, " + name);
            Console.WriteLine("Press any key to exit ...");
            Console.ReadKey();
        }
    }

}
Download Source Code



OUTPUT: