1. Page 1 of 8
The first line of the program using System; the using keyword is used to include the System namespace
in the program. A program generally has multiple using statements.
The next line has the namespace declaration. A namespace is a collection of classes. The “Hello World”
Application namespace contains the class Hello World.
The next line has a class declaration, the class Hello World contains the data and method definitions that
your program uses. Classes generally would contain more than one method. Methods define the behavior
of the class. However, the Hello World class has only one method Main.
The next line defines the Main method, which is the entry point for all C# programs. The Main method
states what the class will do when executed
The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the
program.
The Main method specifies its behavior with the statement Console.WriteLine("Hello World");
WriteLine is a method of the Console class defined in the System namespace. This statement causes the
message "Hello, World!" to be displayed on the screen.
The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press
and it prevents the screen from running and closing quickly when the program is launched from Visual
Studio .NET.
Its worth to naote the following points:
C# is case sensitive.
All statements and expression must end with a semicolon (;).
The program execution starts at the main method.
Unlike Java, file name could be different from the class name.
C# Type Conversion Methods: C# provides the following built-in type conversion methods:
2. Page 2 of 8
S.N Methods & Description
1
ToBoolean
Converts a type to a Boolean value, where possible.
2
ToByte
Converts a type to a byte.
3
ToChar
Converts a type to a single Unicode character, where possible.
4
ToDateTime
Converts a type (integer or string type) to date-time structures.
5
ToDecimal
Converts a floating point or integer type to a decimal type.
6
ToDouble
Converts a type to a double type.
7
ToInt16
Converts a type to a 16-bit integer.
8
ToInt32
Converts a type to a 32-bit integer.
9
ToInt64
Converts a type to a 64-bit integer.
10
ToSbyte
Converts a type to a signed byte type.
11
ToSingle
Converts a type to a small floating point number.
12
ToString
Converts a type to a string.
13
ToType
Converts a type to a specified type.
14
ToUInt16
Converts a type to an unsigned int type.
15
ToUInt32
Converts a type to an unsigned long type.
16
ToUInt64
Converts a type to an unsigned big integer.
3. Page 3 of 8
Accepting Values from User:
The Console class in the System namespace provides a function ReadLine() for accepting input from the user and store it into a
variable.
Arithmetic Operators:
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0
++ Increment operator increases integer value by one A++ will give 11
-- Decrement operator decreases integer value by one A-- will give 9
Relational Operators:
Operator Description Example
== Checks if the values of two operands are equal or not, if yes then condition becomes true.
(A == B)
is not
true.
!=
Checks if the values of two operands are equal or not, if values are not equal then condition
becomes true.
(A != B)
is true.
>
Checks if the value of left operand is greater than the value of right operand, if yes then
condition becomes true.
(A > B)
is not
true.
<
Checks if the value of left operand is less than the value of right operand, if yes then condition
becomes true.
(A < B)
is true.
>=
Checks if the value of left operand is greater than or equal to the value of right operand, if yes
then condition becomes true.
(A >= B)
is not
true.
<=
Checks if the value of left operand is less than or equal to the value of right operand, if yes
then condition becomes true.
(A <= B)
is true.
Logical Operators:
4. Page 4 of 8
Operator Description Example
&& Called Logical AND operator. If both the operands are non zero then condition becomes true.
(A && B)
is false.
||
Called Logical OR Operator. If any of the two operands is non zero then condition becomes
true.
(A || B)
is true.
!
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is
true then Logical NOT operator will make false.
!(A &&
B) is
true.
Conditions:
Statement Description
if statement
An if statement consists of a boolean expression followed by one or more
statements.
if...else statement
An if statement can be followed by an optional else statement, which
executes when the boolean expression is false.
nested if statements
You can use one if or else if statement inside another if or else
if statement(s).
switch statement
A switch statement allows a variable to be tested for equality against a list of
values.
nested switch statements You can use one switch statement inside another switchstatement(s).
C# - Loops: A loop statement allows us to execute a statement or group of statements multiple times .
Loop Type Description
while loop
Repeats a statement or group of statements while a given condition is true. It tests
the condition before executing the loop body.
for loop
Executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
do...while loop Like a while statement, except that it tests the condition at the end of the loop body
nested loops You can use one or more loop inside any another while, for or do..while loop.
Loop Control Statements:
5. Page 5 of 8
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that
were created in that scope are destroyed.
Control Statement Description
break statement
Terminates the loop or switch statement and transfers execution to the statement
immediately following the loop or switch.
continue statement
Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
C# - Arrays:
An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it
is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as
numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is
accessed by an index.
Declaring Arrays:
To declare an array in C#, you can use the following syntax:
datatype[] arrayName;
where,
datatype is used to specify the type of elements to be stored in the array.
[ ] specifies the rank of the array. The rank specifies the size of the array.
arrayName specifies the name of the array.
For example,
double[] balance;
Initializing an Array
Array is a reference type, so you need to use the new keyword to create an instance of the array.
For example,
double[] balance = new double[10];
6. Page 6 of 8
Code Description
abstract The abstract modifier can be used with classes, methods, properties, indexers, and events.
as The as operator is used to perform conversions between compatible types.
base The base keyword is used to access members of the base class from within a derived class
bool
The bool keyword is an alias of System.Boolean. It is used to declare variables to store the
Boolean values, true and false.
break The keyword break is used to exit out of a loop or switch block.
byte It represents an 8-bit unsigned integer whose value ranges from 0 to 255.
case case is often used in a switch statement.
catch The keyword catch is used to identify a statement or statement block for execution
char It represents a Unicode character whose from 0 to 65,535.
checked
The checked keyword is used to control the overflow-checking context for integral-type
arithmetic operations and conversions.
class The class keyword is used to declare a class.
const
The const keyword is used in field and local variable declarations to make the
variable constant
continue Its affect is to end the current loop and proceed to the next one.
decimal The decimal keyword denotes a 128-bit data type.
default The default keyword can be used in the switch statement
delegate
The delegate keyword is used to declare a delegate. A delegate is a programming construct
that is used to obtain a callable reference to a method of a class.
do
The do statement executes a statement or a block of statements repeatedly until a specified
expression evaluates to false.
double The double keyword denotes a simple type that stores 64-bit floating-point values.
else
An else clause immediately follows an if-body. It provides code to execute when
the condition is false.
enum
The enum keyword is used to declare an enumeration, a distinct type consisting of a set of
named constants called the enumerator list.
event It is used to declare an event.
explicit The explicit keyword is used to declare an explicit user-defined type conversion operator
extern
Use the extern modifier in a method declaration to indicate that the method is implemented
externally.
false The false keyword is a boolean constant value
finally The finally block is useful for cleaning up any resources allocated in the try block.
fixed Prevents relocation of a variable by the garbage collector.
float The float keyword denotes a simple type that stores 32-bit floating-point values.
for
The for loop executes a statement or a block of statements repeatedly until a specified
expression evaluates to false.
foreach
The foreach statement repeats a group of embedded statements for each element in an array
or an object collection.
7. Page 7 of 8
goto The goto statement transfers the program control directly to a labeled statement.
if
The if statement selects a statement for execution based on the value of a Boolean
expression.
implicit The implicit keyword is used to declare an implicit user-defined type conversion operator.
in The in keyword identifies the collection to enumerate in a foreach loop.
int The int keyword denotes an integral type that stores values according to the size and range
interface
The interface keyword is used to declare an interface. Interfaces provide a construct for a
programmer to create types that can have methods, properties, delegates, events, and
indexers declared, but not implemented.
internal
The internal keyword is an access modifier for types and type members. That is, it is
only visible within the assembly that implements it.
is
The is operator is used to check whether the run-time type of an object is compatible with a
given type. Using is on a null variable always returns false.
lock
The lock keyword marks a statement block as a critical section by obtaining the mutual-
exclusion lock for a given object, executing a statement, and then releasing the lock.
long
The long keyword denotes an integral type that stores values according to the size and
range.That is, it represents a 64-bit signed integer whose value ranges from -
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
namespace
The namespace keyword is used to supply a namespace for class, structure, and type
declarations.
new In C#, the new keyword can be used as an operator or as a modifier.
null
The null keyword is a literal that represents a null reference, one that does not refer to any
object.
object It represents the base class from which all other reference types derive.
operator The operator keyword is used to declare an operator in a class or struct declaration.
out
The out method parameter keyword on a method parameter causes a method to refer to the
same variable that was passed into the method
override Use the override modifier to modify a method, a property, an indexer, or an event.
params
The keyword params is used to describe when a grouping of parameters are passed to a
method, but the number of parameters are not important, as they may vary.
private
To make the field, method, or property private to its enclosing class. That is, it is
not visible outside of its class.
protected
To make the field, method, or property protected to its enclosing class. That is, it is
not visible outside of its class.
public
To make the field, method, or property public to its enclosing class. That is, it is visible from
any class.
readonly The readonly keyword is a modifier that you can use on fields.
ref
The ref keyword explicitely specifies that a variable should be passed by reference rather
than by value.
return The return statement terminates execution of the method in which it appears and returns
8. Page 8 of 8
control to the calling method.
sbyte It represents an 8-bit signed integer whose value ranges from -128 to 127.
sealed A sealed class cannot be inherited.
short It represents a 16-bit signed integer whose value ranges from -32,768 to 32,767.
sizeof The sizeof keyword returns how many bytes an object requires to be stored.
stackalloc Allocates a block of memory on the stack.
static
Use the static modifier to declare a static member, which belongs to the type itself rather than
to a specific object.
string The string type represents a string of Unicode characters.
struct
A struct type is a value type that can contain constructors, constants, fields, methods,
properties, indexers, operators, events, and nested types.
switch
The switch statement is a control statement that handles multiple selections by passing
control to one of the case statements within its body.
this
The this keyword refers to the current instance of the class. Static member functions do not
have a this pointer.
throw
The throw statement is used to signal the occurrence of an anomalous situation (exception)
during the program execution.
true In C#, the true keyword can be used as an overloaded operator or as a literal.
try
The try-catch statement consists of a try block followed by one or more catchclauses, which
specify handlers for different exceptions.
typeof The typeof operator is used to obtain the System.Type object for a type.
uint
The uint keyword denotes an integral type that stores values according to the size and range
shown in the following table.
ulong
The ulong keyword denotes an integral type that stores values according to the size and
range shown in the following table.
unchecked
The unchecked keyword is used to control the overflow-checking context for integral-
type arithmetic operations and conversions.
unsafe
The unsafe keyword denotes an unsafe context, which is required for any operation
involving pointers.
ushort
The ushort keyword denotes an integral data type that stores values according to the
size and range shown in the following table.
using The using keyword has two major uses.
virtual
The virtual keyword is used to modify a method or property declaration, in which case
the method or the property is called a virtual member.
volatile
The volatile keyword indicates that a field can be modified in the program by
something such as the operating system, the hardware, or a concurrently executing
thread.
void
When used as the return type for a method, void specifies that the method does not
return a value.
while
The while statement executes a statement or a block of statements until a specified
expression evaluates to false.