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

Module 1-1

The document provides an introduction to the C# programming language, which is part of the .NET Framework, detailing its features, data types, operators, and control structures. It compares C# with Java, outlines variable types and scopes, and explains decision-making and iterative statements in C#. Additionally, it includes examples of basic C# syntax and methods, emphasizing the language's object-oriented nature and versatility in application development.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Module 1-1

The document provides an introduction to the C# programming language, which is part of the .NET Framework, detailing its features, data types, operators, and control structures. It compares C# with Java, outlines variable types and scopes, and explains decision-making and iterative statements in C#. Additionally, it includes examples of basic C# syntax and methods, emphasizing the language's object-oriented nature and versatility in application development.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Introduction to Dot Net Framework for Application Development

Introducing Microsoft Visual C# and Microsoft Visual Studio 2015:


Welcome to C#:
C# is a programming language of .Net Framework.

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

Programming Object-oriented, component-oriented, Object-oriented


Paradigm functional

Installation .NET provides a huge library of codes used by Requires JDK to run Java
C#.

Application Web and game development Complex web-based applications

Tools Visual Studio, Mono Develop Eclipse, NetBeans, Intelli J IDEA.

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

Platform Cross-platform and supports both Windows Platform-independent but needs


Dependency and Unix-based systems JVM for its execution.

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:

Variable Type Example

Decimal types decimal

Boolean types True or false value, as assigned

Integral types int, char, byte, short, long

Floating point types float and double

Nullable types Nullable data types

The example of declaring variable is given below:


int i, j;
double d;
float f;
char ch;
Here, i, j, d, f, ch are variables and int, double, float, char are data types.

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.

Types Data Types

Value Data Type short, int, char, float, double etc

Reference Data Type String, Class, Object and Interface

Pointer Data Type Pointers

Value Data Type:


The value data types are integer-based and floating-point based. C# language supports both signed
and unsigned literals.
There are 2 types of value data type in C# language.
1) Predefined Data Types - such as Integer, Boolean, Float, etc.
2) User defined Data Types - such as Structure, Enumerations, etc.

Let's see the value data types. It size is given according to 32 bit OS.

Data Types Memory Size Range

char 1 byte -128 to 127

signed char 1 byte -128 to 127

unsigned char 1 byte 0 to 127

short 2 byte -32,768 to 32,767

signed short 2 byte -32,768 to 32,767


unsigned short 2 byte 0 to 65,535

int 4 byte -2,147,483,648 to -2,147,483,647

signed int 4 byte -2,147,483,648 to -2,147,483,647

unsigned int 4 byte 0 to 4,294,967,295

long 8 byte ?9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

signed long 8 byte ?9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

unsigned long 8 byte 0 - 18,446,744,073,709,551,615

float 4 byte 1.5 * 10-45 - 3.4 * 1038, 7-digit precision

double 8 byte 5.0 * 10-324 - 1.7 * 10308, 15-digit precision

decimal 16 byte at least -7.9 * 10?28 - 7.9 * 1028, with at least 28-digit precision

Reference Data Type:


The reference data types do not contain the actual data stored in a variable, but they contain a
reference to the variables.
If the data is changed by one of the variables, the other variable automatically reflects this change in
value.
There are 2 types of reference data type in C# language.
1) Predefined Types - such as Objects, String.
2) User defined Types - such as Classes, Interface.

Pointer Data Type


The pointer in C# language is a variable, it is also known as locator or indicator that points to an address
of a value.

Symbols used in pointer:

Symbol Name Description

& (ampersand sign) Address operator Determine the address of a variable.

* (asterisk sign) Indirection operator Access the value of an address.


Declaring a pointer
The pointer in C# language can be declared using * (asterisk symbol).
int * a; //pointer to int
char * c; //pointer to char

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.

if (age>=18 && age<58)


Console.WriteLine("Eligible to work");
Here, (age>=18 && age<58) is an expression that returns a boolean value. "Eligible to work" is also an
expression.
C# Statements
A statement is a basic unit of execution of a program. A program consists of multiple statements.

For example:
int age = 21;
Int marks = 90;
In the above example, both lines above are statements.

There are different types of statements in C#.


➢ Declaration Statement
➢ Expression Statement

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.

A list of Reserved Keywords available in C# programming language is given below:

abstract base as bool break catch case

byte char checked class const continue decimal

private protected public return readonly ref sbyte

explicit extern false finally fixed float for

foreach goto if implicit in in (generic modifier) int

ulong ushort unchecked using unsafe virtual void

null object operator out out (generic modifier) override params

default delegate do double else enum event

sealed short sizeof stackalloc static string struct

switch this throw true try typeof uint

abstract base as bool break catch case

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:

Example Inside Main(), call the myMethod() method:


static void MyMethod() //3
{
Console.WriteLine("I just got executed!"); //4
}
static void Main(string[] args) //1
{
MyMethod(); //2
}

// Outputs "I just got executed!"

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

C# Class Level Variable Scope


In C#, when we declare a variable inside a class, the variable can be accessed within the class. This is
known as class level variable scope. Class level variables are known as fields and they are declared
outside of methods, constructors, and blocks of the class.
For example,
using System;
namespace VariableScope
{
class Program {
// class level variable
string str = "Class Level";
public void display() {
Console.WriteLine(str);
}
static void Main(string[] args)
{
Program ps = new Program();
ps.display();
Console.ReadLine();
}}}

Method Level Variable Scope


When we declare a variable inside a method, the variable cannot be accessed outside of the method.
This is known as method level variable 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();
}}}

Block Level Variable Scope in C#


When we declare a variable inside a block (for loop, while loop, if..else), the variable can only be
accessed within the block. This is known as block level variable scope. For example,
using System;
namespace VariableScope {
class Program {
public void display() {
for(int i=0;i<=3;i++) {
}
Console.WriteLine(i);
}
static void Main(string[] args) {
Program ps = new Program();
ps.display();
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.

The various type of IF statement is:


➢ if statement
➢ if-else statement
➢ nested if statement
➢ if-else-if ladder

If Statement:

The C# if statement tests the condition. It is executed if condition is true.


Syntax:
if(condition)
{ //code to be executed }
Example:
using System;
public class IfExample
{
public static void Main(string[] args)
{
int num = 10;
if (num % 2 == 0)
{
Console.WriteLine("It is even number");
}
}
}

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");
}
}
}

C# IF-else-if ladder Statement


The C# if-else-if ladder statement executes one condition from multiple statements.
Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{//code to be executed if condition2 is true}
else if(condition3)
{//code to be executed if condition3 is true}
Else
{//code to be executed if all the conditions are false\
}
C# If else-if Example
using System;
public class IfExample
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number to check grade:");
int num = Convert.ToInt32(Console.ReadLine());
if (num <0 || num >100)
{ Console.WriteLine("wrong number");
} else if(num >= 0 && num < 50)
{ Console.WriteLine("Fail");
} else if (num >= 50 && num < 60)
{
Console.WriteLine("D Grade");
} else if (num >= 60 && num < 70)
{ Console.WriteLine("C Grade");
} else if (num >= 70 && num < 80)
{ Console.WriteLine("B Grade");
} else if (num >= 80 && num < 90)
{ Console.WriteLine("A Grade");
} else if (num >= 90 && num <= 100)
{
Console.WriteLine("A+ Grade");
}}}
Output:
Enter a number to check grade:66
C Grade

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}

C# While Loop Example


Let's see a simple example of while loop to print table of 1.
using System;
public class WhileExample
{
public static void Main(string[] args)
{
int i=1;
while(i<=10)
{
Console.WriteLine(i); i++;
}
}
}
Output:
1
2
3
4
5
6
7
8
9
10

C# Nested While Loop Example:


In C#, we can use while loop inside another while loop, it is known as nested while loop. The nested
while loop is executed fully when outer loop is executed once.
Let's see a simple example of nested while loop in C# programming language.
using System;
public class WhileExample
{
public static void Main(string[] args)
{
int i=1;
while(i<=3)
{
int j = 1;
while (j <= 3)
{
Console.WriteLine(i+" "+j);
j++;
}
i++;
}
}
}

Output:
11
12
13
21
22
23
31
32
33

C# Infinitive While Loop Example:


We can also create infinite while loop by passing true as the test condition.

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);

C# do-while Loop Example


Let's see a simple example of C# do-while loop to print the table of 1.

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

C# Nested do-while Loop


In C#, if you use do-while loop inside another do-while loop, it is known as nested do-while loop. The
nested do-while loop is executed fully for each outer do-while loop.
Let's see a simple example of nested do-while loop in C#.
using System;
public class DoWhileExample
{
public static void Main(string[] args)
{
int i=1;
do
{
int j = 1;
do
{
Console.WriteLine(i+" "+j);
j++;
}
while (j <= 3);
i++;
}
while (i <= 3);
}
}
Output: 1 1
12
13
21
22
23
31
32
33

C# Infinitive do-while Loop


In C#, if you pass true in the do-while loop, it will be infinitive do-while loop.
Syntax:
Do
{//code to be executed}
while(true);

C# Infinitive do-while Loop Example


using System;
public class WhileExample
{
public static void Main(string[] args)
{
Do
{
Console.WriteLine("Infinitive do-while Loop");
}
while(true);
}
}

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}

C# For Loop Example


using System;
public class ForExample
{
public static void Main(string[] args)
{
for(int i=1;i<=10;i++)
{
Console.WriteLine(i);
}
}
}
Output: 1 2 3 4 5 6 7 8 9 10

C# Nested For Loop


In C#, we can use for loop inside another for loop, it is known as nested for loop. The inner loop is
executed fully when outer loop is executed one time. So if outer loop and inner loop are executed 3
times, inner loop will be executed 3 times for each outer loop i.e. total 9 times.

Let's see a simple example of nested for loop in C#.


using System;
public class ForExample
{
public static void Main(string[] args)
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
Console.WriteLine(i+" "+j);
}
}
}
}
Output:
11
12
13
21
22
23
31
32
33
C# Infinite For Loop
If we use double semicolon in for loop, it will be executed infinite times.

Let's see a simple example of infinite for loop in C#.


using System;
public class ForExample
{
public static void Main(string[] args)
{
for (; ;)
{
Console.WriteLine("Infinitive For Loop");
}
}
}
Output:
Infinitive For Loop
Infinitive For Loop
Infinitive For Loop
Infinitive For Loop
Infinitive For Loop
ctrl+c

Compound assignment operators


The compound assignment operator is the combination of more than one operator. It includes an
assignment operator and arithmetic operator or bitwise operator.

The following are the compound assignment operators in C#.

Sl.No Operator & Operator Name

1 += Addition Assignment

2 -= Subtraction Assignment

3 *= Multiplication Assignment

4 /= Division Assignment

5 %= Modulo Assignment

6 &= Bitwise AND Assignment

7 |= Bitwise OR Assignment

8 ^= Bitwise XOR Assignment

9 <<= Left Shift Assignment


Sl.No Operator & Operator Name

10 >>= Right Shift Assignment

11 => Lambda Operator

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.

You might also like