Professional Documents
Culture Documents
CH 1
CH 1
SYLLABUS
It is a virtual machine that provide a common platform to run an application that was
built using the different language such as C#, VB.NET, Visual Basic, etc. It is also used
to create a form based, console-based, mobile and web-based application or services
that are available in Microsoft environment. Furthermore, the .NET framework is a
pure object oriented, that similar to the Java language. But it is not a platform
independent as the Java. So, its application runs only to the windows platform.
The main objective of this framework is to develop an application that can run on
the windows platform. The current version of the .Net framework is 4.8.
2. Features / Advantages : 3M or 5M
C# is object-oriented programming language. It provides a lot of features/ (advantage)
that are given below.
1. Simple
2. Modern programming language
3. Object oriented
4. Type safe
5. Interoperability
6. Scalable and Updateable
7. Component oriented
8. Structured programming language
9. Rich Library
10. Fast speed
1) Simple
C# is a simple language in the sense that it provides structured approach (to break the
problem into parts), rich set of library functions, data types etc.
C# programming is based upon the current trend and it is very powerful and simple for
building scalable, interoperable and robust applications.
3) Object Oriented
C# type safe code can only access the memory location that it has permission to execute.
Therefore, it improves a security of the program.
5) Interoperability
7) Component Oriented
C# is a structured programming language in the sense that we can break the program
into parts using functions. So, it is easy to understand and modify.
9) Rich Library
3.
CLR : common language runtime 2M/ 5M
.NET CLR is a runtime environment that manages and executes the code written in any
.NET programming language. CLR is the virtual machine component of the .NET
framework. That language's compiler compiles the source code of applications developed
using .NET compliant languages into CLR's intermediate language called MSIL, i.e.,
Microsoft intermediate language code. This code is platform-independent. It is comparable
to byte code in java. Metadata is also generated during compilation and MSIL code and
stored in a file known as the Manifest file. This metadata is generally about members and
types required by CLR to execute MSIL code. A just-in-time compiler component of CLR
converts MSIL code into native code of the machine. This code is platform-dependent.
CLR manages memory, threads, exceptions, code execution, code safety, verification, and
compilation.
The following figure shows the conversion of source code into native code.
The above figure converts code into native code, which the CPU can execute.
CTS provides guidelines for declaring, using, and managing data types at runtime. It
offers cross-language communication. For example, VB.NET has an integer data type,
and C# has an int data type for managing integers. After compilation, Int32 is used by
both data types. So, CTS provides the data types using managed code. A common type
system helps in writing language-independent code.
1. Value Type: A value type stores the data in memory allocated on the stack or inline in
a structure. This category of Type holds the data directory. If one variable's value is
copied to another, both the variables store data independently. It can be of inbuilt-in
types, user-defined, or enumerations types. Built-in types are primitive data types like
numeric, Boolean, char, and date. Users in the source code create user-defined types.
An enumeration refers to a set of enumerated values represented by labels but stored as
a numeric type.
2. Reference Type: A Reference type stores a reference to the value of a memory address
and is allocated on the heap. Heap memory is used for dynamic memory allocation.
Reference Type does not hold actual data directly but holds the address of data.
Whenever a reference type object is made, it copies the address and not actual data.
Therefore two variables will refer to the same data. If data of one Reference Type object
is changed, the same is reflected for the other object. Reference types can be self-
describing types, pointer types, or interference types. The self-describing types may be
string, array, and class types that store metadata about themselves.
3. Garbage Collection: 2M
JIT Compiler is an important component of CLR. It converts the MSIL code into native
code (i.e., machine-specific code). The .NET program is compiled either explicitly or
implicitly. The developer or programmer calls a particular compiler to compile the
program in the explicit compilation. In implicit compilation, the program is compiled
twice. The source code is compiled into Microsoft Intermediate Language (MSIL)
during the first compilation process. The MSIL code is converted into native code in
the second compilation process. This process is called JIT compilation. There are three
types of JIT compilers -Pre, Econo, and Normal. Pre JIT Compiler compiles entire
MSIL code into native code before execution. Econo JIT Compiler compiles only those
parts of MSIL code required during execution and removes those parts that are not
required anymore. Normal JIT Compiler also compiles only those parts of MSIL code
required during execution but places them in cache for future use. It does not require
recompilations of already used parts as they have been placed in cache memory.
5. Metadata:1M
A Metadata is a binary information about the program, either stored in a CLR Portable
Executable file (PE) along with MSIL code or in the memory. During the execution of
MSIL, metadata is also loaded into memory for proper interpretation of classes and
related. Information used in code. So, metadata helps implement code in a language-
neutral manner or achieve language interoperability.
6. Assemblies:1M
The code that runs with CLR is called managed code, whereas the code outside the
CLR is called unmanaged code. The CLR also provides an Interoperability layer, which
allows both the managed and unmanaged codes to interoperate.
1. Managed code: 3M
Any language that is written in the .NET framework is managed code. Managed code
use CLR, which looks after your applications by managing memory, handling security,
allowing cross-language debugging, etc. The process of managed code is shown in the
figure:
2. Unmanaged code:
The code developed outside the .NET framework is known as unmanaged code.
Applications that do not run under the control of the CLR are said to be unmanaged.
Certain languages such as C++ can be used to write such applications, such as low-
level access functions of the operating system. Background compatibility with VB,
ASP, and COM are examples of unmanaged code. This code is executed with the help
of wrapper classes. The unmanaged code process is shown below:
Knowledge Part :
1.0 1.0
1.1 1.1
2.0 2.0
3.0 2.0
3.5 2.0
4 4
4.5 4
4.6 4
The base class library has a rich collection of libraries features and functions that help
to implement many programming languages in the .NET Framework, such as C #, F #,
Visual C ++, and more. Furthermore, BCL divides into two parts:
KNOWLEDGE
Namespaces Description
It provides the various system functionality in the .NET Framework, that includes
classes, interfaces and data types, etc. to create multiple functions and different types
of application such as desktop, web, mobile application, etc. In other words, it can be
defined as, it provides a base on which various applications, controls and components
are built in .NET Framework.
WinForms: Windows Forms is a smart client technology for the .NET Framework, a
set of managed libraries that simplify common application tasks such as reading and
writing to the file system.
It is a framework for building service-oriented applications. Using WCF, you can send
data as asynchronous messages from one service endpoint to another.
WF (Workflow Foundation)
It is a query language, introduced in .NET 3.5 framework. It is used to make the query
for data sources with C# or Visual Basics programming languages.
Entity Framework
It is an open source framework which is used to work with a database using .NET
objects. It eliminates a lot of developers effort to handle the database. It is Microsoft's
recommended technology to deal with the database.
Introduction to C#
o Window applications
o Web applications
o Distributed applications
o Web service applications
o Database applications etc.
The memory size of data types may change according to 32 or 64 bit operating system.
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
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.
Output:
Hello World!
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.
1. type variable_list;
1. int i, j;
2. double d;
3. float f;
4. 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:
A variable name can start with alphabet and underscore only. It can't start with digit.
A variable name must not be any reserved word or keyword e.g. char, float etc.
1. int x;
2. int _x;
3. int k20;
1. int 4;
2. int x y;
3. int double;
• The process of converting a Value Type variable (char, int etc.) to a Reference
Type variable (object) is called Boxing.
• Boxing is an implicit conversion process in which object type (super type) is used.
• Value Type variables are always stored in Stack memory, while Reference Type
variables are stored in Heap memory.
• Example :
• Example :
int num = 23; // value type is int and assigned value 23
Object Obj = num; // Boxing
int i = (int)Obj; // Unboxing
To create single dimensional array, you need to use square brackets [] after the type.
Example
using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = new int[5];//creating array
arr[0] = 10;//initializing array
arr[2] = 20;
arr[4] = 30;
//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}
Output:
10
0
20
0
30
To create multidimensional array, we need to use comma inside the square brackets.
For example:
Example:
using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr=new int[3,3];//declaration of 2D array
arr[0,1]=10;//initialization
arr[1,2]=20;
arr[2,0]=30;
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}
1. int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
➢ Jagged array
In C#, jagged array is also known as "array of arrays" because its elements are
arrays. The element size of jagged array can be different.
Let's see an example to declare jagged array that has two elements.
Let's see an example to initialize jagged array. The size of elements can be different.
Here, size of elements in jagged array is optional. So, you can write above code as given
below:
Let's see a simple example of jagged array in C# which declares, initializes and
traverse jagged arrays.
arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };
Output:
11 21 56 78
42 61 37 41 59 63
o if statement
o if-else statement
o nested if statement
o if-else-if ladder
C# IF Statement
Syntax:
if(condition){
//code to be executed
}
C# If 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");
}
}
}
Output:
It is even number
C# 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");
}
}
}
Output:
It is odd number
In this example, we are getting input from the user using Console.ReadLine() method.
It returns string. For numeric value, you need to convert it into int
using Convert.ToInt32() method.
using System;
public class IfExample
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
int num = Convert.ToInt32(Console.ReadLine());
if (num % 2 == 0)
{
Console.WriteLine("It is even number");
}
else
{
Console.WriteLine("It is odd number");
}
}
}
Output:
Enter a number:11
It is odd number
Output:
Enter a number:12
It is even number
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());
Output:
Output:
C# switch
The C# switch statement executes one statement from multiple conditions. It is like if-
else-if ladder statement in C#.
Syntax:
switch(expression){
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
......
default:
//code to be executed if all cases are not matched;
break;
}
C# Switch Example
using System;
public class SwitchExample
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
int num = Convert.ToInt32(Console.ReadLine());
switch (num)
{
case 10: Console.WriteLine("It is 10"); break;
case 20: Console.WriteLine("It is 20"); break;
case 30: Console.WriteLine("It is 30"); break;
default: Console.WriteLine("Not 10, 20 or 30"); break;
}
}
}
Output:
Enter a number:
10
It is 10
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:
1. for(initialization; condition; incr/decr){
2. //code to be executed
3. }
1. using System;
2. public class ForExample
3. {
4. public static void Main(string[] args)
5. {
6. for(int i=1;i<=10;i++){
7. Console.WriteLine(i);
8. }
9. }
10. }
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:
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:
1. while(condition){
2. //code to be executed
3. }
Example
using System;
public class WhileExample
{
public static void Main(string[] args)
{
int i=1;
while(i<=10)
{
Console.WriteLine(i);
i++;
}
}
}
C# Do-While Loop
The C# do-while loop is used to iterate a part of the program several times. If the
number of iteration 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.
There is also a foreach loop, which is used exclusively to loop through elements in
an array:
Syntax
The following example outputs all elements in the cars array, using a foreach loop:
Example
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars)
{
Console.WriteLine(i);
}
}
}
}
OP:
Volvo
BMW
Ford
Mazda