Module 1-1
Module 1-1
What is C#:
C# is pronounced as "C-Sharp". It is an object-oriented programming language provided by Microsoft
that runs on .Net Framework.
By the help of C# programming language, we can develop different types of secured and robust
applications:
➢ Window applications
➢ Web applications
➢ Distributed applications
➢ Web service applications
➢ Database applications etc.
Java vs C#:
There are many differences and similarities between Java and C#. A list of top differences between
Java and C# are given below:
Parameters C# Java
Installation .NET provides a huge library of codes used by Requires JDK to run Java
C#.
Public Classes Supports multiple public classes in source Java source code can have only one
code public class.
Checked Does not support checked exceptions Supports checked and unchecked
Exceptions exceptions
Go-to statement Supports go-to statement Does not support go-to statement
Structure and Supports structures and unions. Does not support structures and
Union unions
C# History
C# is pronounced as "C-Sharp". It is an object-oriented programming language provided by Microsoft
that runs on .Net Framework.
Anders Hejlsberg is known as the founder of C# language.
C# Simple Example
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}}
C# Variable
A variable is a name of memory location. It is used to store data.
Its value can be changed and it can be reused many times. It is a way to represent memory location
through symbol so that it can be easily identified.
The basic variable type available in C# can be categorized as:
We can also provide values while declaring the variables as given below:
int i=2,j=4; //declaring 2 variable of integer type
float f=40.2;
char ch='B';
C# Data Types:
A data type specifies the type of data that a variable can store such as integer, floating, character
etc.
There are 3 types of data types in C# language.
Let's see the value data types. It size is given according to 32 bit OS.
decimal 16 byte at least -7.9 * 10?28 - 7.9 * 1028, with at least 28-digit precision
C# operators
An operator is simply a symbol that is used to perform operations. There can be many types of
operations like arithmetic, logical, bitwise etc.
There are following types of operators to perform different types of operations in C# language.
➢ Arithmetic Operators
➢ Relational Operators
➢ Logical Operators
➢ Bitwise Operators
➢ Assignment Operators
➢ Unary Operators
➢ Ternary Operators
C# Expressions
An expression in C# is a combination of operands (variables, literals (fixed values), method calls) and
operators that can be evaluated to a single value. To be precise, an expression must have at least one
operand (values like 3,5) but may not have any operator.
Let's look at the example below:
int a, b, c, sum;
sum = a + b + c;
Here, a + b + c is an expression.
For example:
int age = 21;
Int marks = 90;
In the above example, both lines above are statements.
C# Keywords
A keyword is a reserved word. You cannot use it as a variable name, constant name etc. In C# keywords
cannot be used as identifiers. However, if we want to use the keywords as identifiers, we may prefix
the keyword with @ character.
volatile while
C# Methods
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as functions.
Create a Method
A method is defined with the name of the method, followed by parentheses (). C# provides some pre-
defined methods, which you already are familiar with, such as Main(), but you can also create your
own methods to perform certain actions:
Example
Get your own C# Server Create a method inside the Program class:
class Program
{
static void MyMethod()
{
// code to be executed
}
}
Call a Method
To call (execute) a method, write the method's name followed by two parentheses () and a semicolon;
In the following example, MyMethod() is used to print a text (the action), when it is called:
C# Variable Scope
A variable scope refers to the availability of variables in certain parts of the code.
In C#, a variable has three types of scope:
➢ Class Level Scope
➢ Method Level Scope
➢ Block Level Scope
For example,
using System;
namespace VariableScope
{
class Program
{
public void method1()
{
// display variable inside method
string str = "method level";
}
public void method2()
{
// accessing str from method2()
Console.WriteLine(str);
}
static void Main(string[] args)
{
Program ps = new Program();
ps.method2();
Console.ReadLine();
}}}
C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)
Decision Making in programming is similar to decision making in real life. In programming too, a certain
block of code needs to be executed when some condition is fulfilled.
A programming language uses control statements to control the flow of execution of program based
on certain conditions. These are used to cause the flow of execution to advance and branch based on
changes to the state of a program.
If Statement:
IF-else Statement:
The C# if-else statement also tests the condition. It executes the if block if condition is true otherwise
else block is executed.
Syntax:
if(condition)
{//code if condition is true}
Else
{//code if condition is false}
C# If-else Example
using System;
public class IfExample
{
public static void Main(string[] args)
{
int num = 11;
if (num % 2 == 0)
{
Console.WriteLine("It is even number");
}
else
{
Console.WriteLine("It is odd number");
}
}
}
Compound Statements
Statements can be grouped together in braces to form a compound statement or block.
For example:
{
int i = 4;
Console.WriteLine (i);
i++;
}
C# Iterative Statements:
➢ While Loop.
➢ Do while Loop.
➢ For Loop.
C# While Loop
In C#, while loop is used to iterate a part of the program several times. If the number of iteration is
not fixed, it is recommended to use while loop than for loop.
Syntax:
while(condition)
{//code to be executed}
Output:
11
12
13
21
22
23
31
32
33
using System;
public class WhileExample
{
public static void Main(string[] args)
{
while(true)
{
Console.WriteLine("Infinitive While Loop");
}
}
}
Output:
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
ctrl+c
C# Do-While Loop
The C# do-while loop is used to iterate a part of the program several times. If the number of iterations
is not fixed and you must have to execute the loop at least once, it is recommended to use do-while
loop. The C# do-while loop is executed at least once because condition is checked after loop body.
Syntax:
Do
{//code to be executed}
while(condition);
using System;
public class DoWhileExample
{
public static void Main(string[] args)
{
int i = 1;
do
{
Console.WriteLine(i); i++;
}
while (i <= 10) ;
}}
Output:
1
2
3
4
5
6
7
8
9
10
Output:
Infinitive do-while Loop
Infinitive do-while Loop
Infinitive do-while Loop
Infinitive do-while Loop
Infinitive do-while Loop
ctrl+c
C# For Loop The C# for loop is used to iterate a part of the program several times. If the number of
iteration is fixed, it is recommended to use for loop than while or do-while loops.
The C# for loop is same as C/C++. We can initialize variable, check condition and increment/decrement
value.
Syntax:
For (initialization; condition; incr/decr)
{//code to be executed}
1 += Addition Assignment
2 -= Subtraction Assignment
3 *= Multiplication Assignment
4 /= Division Assignment
5 %= Modulo Assignment
7 |= Bitwise OR Assignment
C# Managing Errors
In every computer program released there are always problems (known as “bugs”), and these
problems can occur at any time. This can be easily solved by using a try catch block to “catch” the
exception. There are different types of exceptions which can occur, such as an overflowException or
a formatException.
C# exception handling is built upon four keywords: try, catch, finally, and throw.
➢ try − A try block identifies a block of code for which particular exceptions is activated. It is
followed by one or more catch blocks.
➢ catch − A program catches an exception with an exception handler at the place in a program
where you want to handle the problem. The catch keyword indicates the catching of an
exception.
➢ finally − The finally block is used to execute a given set of statements, whether an exception
is thrown or not thrown.
➢ throw − A program throws an exception when a problem shows up. This is done using a throw
keyword.