Net Unit1 Notes
Net Unit1 Notes
NET FRAMEWORK
INTRODUCTION
o The CLR is the heart of the .NET Framework, acting as a virtual machine that
runs the code and manages various services such as memory management,
security, and thread management. Code that is compiled and executed within the
CLR is called "Managed Code," while code that the CLR does not manage is
known as "Unmanaged Code."
o The FCL provides a large set of reusable classes and methods for application
development.
.NET is a software framework that is designed and developed by Microsoft. The first version of
the .Net framework was 1.0 which came in the year 2002. In easy words, it is a virtual machine
for compiling and executing programs written in different languages like C#, [Link], etc.
It is used to develop Form-based applications, Web-based applications, and Web services. There
is a variety of programming languages available on the .Net platform, [Link] and C# being the
most common ones. It is used to build applications for Windows, phones, web, etc. It provides a
lot of functionalities and also supports industry standards.
Features of .NET
1. Platform independent
Platform independent means it supports cross platform that works on multiple platforms.
Whenever we make .NET program, we use windows operating system. In windows operating
system we have different versions such as (window 10, window 8, window 9 etc ). Suppose, if
we develop programming code in window 11 and the same code try to run on different version
like in window 10 operating system it will work perfectly fine. So, we can say that it is
interoperable between multiple windows operating system.
2. Language Independent
As Programmer can choose any language from a various programming language available on the
.NET platform. So, if I have the code of one language. I can just work with other language as
well. We can say code written in one language can be used in other language. Suppose program
is
develop in [Link] user can easily reuse that code in C#.NET as well. That means user can
reuse the code from one language to another. So, as we don’t need to write code again and again
so it improves efficiency and performance will be faster with the help of different languages.
It automatically manages the resources(file, memory, database etc). In .NET we have garbage
collector that periodically checks for unused objects (memory space) in our code, and
automatically free up that space if it is no longer needed in the program.
[Link] is used to develop applications to interact with Databases suchas Oracle or Microsoft
SQL Server. So, .NET can connect easily with database using [Link] technology.
The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that
manages the execution of .NET applications. It is responsible for loading and executing the code
written in various .NET programming languages, including C#, [Link], F#, and others.
Working of CLR
When we write a C# program, it gets compiled into Intermediate Language (IL) code, which is
platform-independent.
The CLR then uses a Just-In-Time (JIT) compiler to convert the IL code into machine-specific
code while the program runs.
CLR handles automatic memory management through Garbage Collection, preventing memory
leaks.
The CLR checks the IL code for security risks before running it.
3. Cross-Language Integration:
CLR allows code from different .NET languages (C#, [Link], F#) to work together seamlessly
through the Common Type System (CTS).
As the word specify, Common means CLR provides a common runtime or execution
environment as there are more than 60 .NET programming languages.
Common Language Specification (CLS): It defines common rules so that code written in
different languages can interoperate.
Managed Code: The MSIL code which is managed by the CLR is known as the Managed Code.
For managed code CLR provides three .NET facilities:
JIT Compiler: It converts IL code into machine code specific to the system at runtime,
optimizing execution.
Garbage Collector: It automatically manages memory by freeing unused objects, reducing the
need for manual memory management.
Common Type System (CTS): This ensures that different data types across languages are
understood by CLR and can work together. There are 2 Types of CTS that every .NET
programming language have:
Value Types: Value Types will store the value directly into the memory location. These types
work with stack mechanisms only. CLR allows memory for these at Compile Time.
Reference Types: Reference Types will contain a memory address of value because the reference
types won’t store the variable value directly in memory. These types work with Heap
mechanism. CLR allot memory for these at Runtime.
Suppose we have written a C# program and save it in a file which is known as the Source Code.
Language specific compiler compiles the source code into the MSIL(Microsoft Intermediate
Language) which is also known as the CIL(Common Intermediate Language) or IL(Intermediate
Language) along with its metadata. Metadata includes all the types, actual implementation of
each function of the program. MSIL is machine-independent code.
Now CLR comes into existence. CLR provides the services and runtime environment to the
MSIL code. Internally CLR includes the JIT(Just-In-Time) compiler which converts the MSIL
code to machine code which further executed by CPU. CLR also uses the .NET Framework class
libraries. Metadata provides information about the programming language, environment, version,
and class libraries to the CLR by which CLR handles the MSIL code. As CLR is common so it
allows an instance of a class that written in a different language to call a method of the class
which written in another language.
FRAMEWORK CLASS LIBRARY
The Framework Class Library or FCL provides the system functionality in the .NET Framework
as it has various classes, data types, interfaces, etc. to perform multiple functions and build
different types of applications such as desktop applications, web applications, mobile
applications, etc. The Framework Class Library is integrated with the Common Language
Runtime (CLR) of the .NET framework and is used by all the .NET languages such as C#, F#,
Visual Basic .NET, etc.
The functionality of the Framework Class Library can be broadly divided into three categories i.e
utility features written in .NET, wrappers around the OS functionality and frameworks. These
categories are not rigidly defined and there are many classes that may fit into more than one
category.
Details about the Categories in the Framework Class Library are given as follows:
Utility Features: The utility features in the FCL includes various collection classes such
as list, stack, queue, dictionary, etc. and also classes for more varied manipulations such
as Regex class for regular expressions.
Wrappers Around OS functionality: Some of the features in the FCL are wrappers
around the underlying Windows OS functionality. These include the classes for using the
file system, the classes to handle the network features, the classes to handle I/O for
console applications, etc.
Frameworks: There are various frameworks available in the FCL to develop certain
applications. For example, [Link] is used to develop web applications, Windows
Presentation Foundation (WPF) is used to render user interfaces in Windows applications,
and so on.
Namespaces in the Framework Class Library are a group of related classes and interfaces that
can be used by all the .NET framework languages. Some of the namespaces in the FCL along
with their description is given as follows:
Namespace Description
It is important to use the correct data type for the corresponding variable; to avoid errors, to save
time and memory, but it will also make your code more maintainable and readable. The most
common data types are:
Numbers
Integer types stores whole numbers, positive or negative (such as 123 or -456), without
decimals. Valid types are int and long. Which type you should use, depends on the numeric
value.
Floating point types represents numbers with a fractional part, containing one or more decimals.
Valid types are float and double.
Even though there are many numeric types in C#, the most used for numbers are int (for whole
numbers) and double (for floating point numbers). However, we will describe them all as you
continue to read.
Integer Types
Int
The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in
our tutorial, the int data type is the preferred data type when we create variables with a numeric
value.
Example
[Link](myNum);
Long
The long data type can store whole numbers from -9223372036854775808 to
9223372036854775807. This is used when int is not large enough to store the value. Note that
you should end the value with an "L":
Example
[Link](myNum);
You should use a floating point type whenever you need a number with a decimal, such as 9.99
or 3.14515.
The float and double data types can store fractional numbers. Note that you should end the value
with an "F" for floats and "D" for doubles:
[Link](myNum)
Double Example
[Link](myNum);
OPERATORS IN C#
In C#, Operators are special types of symbols which perform operations on variables or values. It
is a fundamental part of language which plays an important role in performing different
mathematical operations. It takes one or more operands and performs operations to produce a
result.
Types of Operators
C# has some set of operators that can be classified into various categories based on their
functionality. Categorized into the following types:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
6. Bitwise Operators
7. Ternary Operator
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations on numeric values.
Addition ( + )
Subtraction ( - )
Multiplication ( * )
Division ( / )
Modulus ( % )
Example:
using System;
class Geeks
int x = 8, y = 4;
Output
Addition: 12
Subtraction: 4
Multiplication: 32
Division: 2
Modulo: 0
2. Relational Operators
Relational operators are used to compare values. And we get the answer in either true or false
( boolean). Let's learn about different relation operators.
Equal to ( == )
Not equal to ( != )
Example:
using System;
class Geeks
[Link](x == y);
[Link](x != y);
Output
False
True
False
True
False
True
3. Logical Operators
Used when multiple conditions and there we can combine these to compare complex conditions.
Logical AND (&&) : returns true when both conditions are true.
Example:
using System;
class Geeks
{
bool a = true, b = false;
// conditional operators
if (a && b)
if (a || b)
[Link]("Either a or b is true");
if (!a)
if (!b)
Output
Either a or b is true
b is not true
4. Assignment Operators
Assignment operators are used to assign values to variables. The assignment operator is
combined with others to create shorthand compound statements. Common compound operators
include:
+= (Add and assign.)
Example:
using System;
class Geeks
int a = 10;
a += 5;
a -= 3;
a *= 2;
a %= 5;
Output
Add Assignment: 15
Subtract Assignment: 12
Multiply Assignment: 24
Division Assignment: 6
Modulo Assignment: 1
5. Increment/Decrement Operators
Increment and decrement operators are used to increase or decrease the value of a variable by 1.
++ (Increments by 1)
-- (Decrements by 1)
using System;
int a = 5;
// pre-increment
// post-increment
[Link]();
// pre-decrement
// post-decrement
[Link]("a-- returns: " + a--);
Output
++a returns: 6
a++ returns: 6
Final value of a: 7
--a returns: 6
a-- returns: 6
Final value of a: 5
6. Bitwise Operators
Bitwise operators are used to perform bit-level operations on integer values. It takes less time
because it directly works on the bits.
Example:
using System;
class Geeks
int x = 10;
int y = 2;
// Bitwise AND
// Bitwise OR
[Link](x | y);
// Bitwise XOR
[Link](x ^ y);
// Bitwise NOT
[Link](~x);
Output
-6
10
7. Ternary Operator
The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns
one of two values depending on whether the condition is true or false.
Syntax
Example:
using System;
{
int a = 10, b = 5;
// similar to if else
Output
a is greater
8. Null-Coalescing Operator
null-coalescing operator is used when we want to put a default value if the value of the variable
is null.
Example:
using System;
Output
Default Name
CONDITIONAL STATEMENTS
Conditional Statements of C#
if
if-else
if-else-if
Nested if
Switch
Nested Switch
1. If Statement
The if statement checks the given condition. If the condition evaluates to be true then the
block of code/statements will execute otherwise not.
Syntax
if ( condition ) {
//code to be executed
}
Note: If the curly brackets { } are not used with if statements then the statement just next to
it is only considered associated with the if statement.
Flowchart:
using System;
{
public static void Main(string[] args)
// Using if statement
if (name == "Geek") {
[Link]("GeeksForGeeks");
Output
GeeksForGeeks
2. If-else Statement
The if statement evaluates the code if the condition is true but what if the condition is not
true, here comes the else statement. It tells the code what to do when the if condition is
false.
Syntax
if(condition)
{
// code if condition is true
}
else
{
// code if condition is false
}
Flowchart:
public static
using System;
{
public static void Main(string[] args)
if (name == "Geeks") {
[Link]("GeeksForGeeks");
else {
[Link]("Geeks");
Output
Geeks
The if-else-if ladder is used when you need to test multiple conditions one after another.
As soon as one condition is true, its block executes and the ladder ends.
If none of the conditions are true, the else block (if provided) executes.
Syntax
if(condition1)
else if(condition2)
else if(condition3)
...
else
Flowchart:
xample: Using if-else-if ladder
using System;
class Geeks
int i = 20;
if (i == 10)
[Link]("i is 10");
else if (i == 15)
[Link]("i is 15");
else if (i == 20)
[Link]("i is 20");
else
Output
i is 20
4. Nested - If Statement
If statement inside an if statement is known as nested if. if statement in this case is the target of
another if or else statement. When more than one condition needs to be true and one of the
condition is the sub-condition of parent condition, nested if can be used.
Syntax
if (condition1)
// code to be executed
// if condition2 is true
if (condition2)
// code to be executed
// if condition2 is true
Flowchart:
class Geeks
int i = 10;
if (i == 10) {
if (i < 12)
else
Output
Switch statement is an alternative to long if-else-if ladders. The expression is checked for
different cases and the one match is execute. Break Statement is used to move out of the switch.
If the break is not used, the control will flow to all cases below it until break is found or switch
comes to an end. There is default case (optional) at the end of switch, if none of the case matches
then default case is executed.
Syntax
switch (expression)
break;
break;
break;
Flowchart:
Example: Using Switch case
using System;
{
int number = 30;
switch(number)
case 10:
[Link]("case 10");
break;
case 20:
[Link]("case 20");
break;
case 30:
[Link]("case 30");
break;
default:
[Link]("None matches");
break;
Output 30
6. Nested Switch
Nested Switch case are allowed in C# . In this case, switch is present inside other switch case.
Inner switch is present in one of the cases in parent switch.
Example:
using System;
int outter = 2;
int inner = 3;
switch(outter){
case 1:
break;
case 2:
[Link]("Outter Case 2");
switch(inner)
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
break;
default:
break;
}
Output
Outter Case 2
Inner Case 3
LOOPING STATEMENTS
Types of Loops in C#
The loops in which the condition to be checked before entering the loop body are known as
Entry Controlled Loops.
1.1 while Loop : The test condition is given at the beginning of the loop and all statements
are executed till the given Boolean condition is satisfied. When the condition becomes false,
the control will be out of the while loop.
Syntax:
loop statements...
Flowchart:
Example:
using System;
class Geeks
int x = 1;
while (x <= 4)
[Link]("GeeksforGeeks");
x++;
Output
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
1.2 for loop : The for loop is a control flow statement that allows you to execute a block of
code repeatedly for a fixed number of iterations. It’s commonly used when the number of
repetitions is known beforehand.
Syntax:
// statements to be executed
Flowchart:
Note:
Initialization part is evaluated only once when the for loop starts.
Example:
using System;
class Geeks
[Link]("GeeksforGeeks");
Output
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
The loops in which the condition is checked after the loop body is known as Exit Controlled
Loops.
Note: In Exit Controlled Loops, loop body will be evaluated for at-least one time as the
testing condition is present at the end of loop body.
Example: do-while Loop
2.1 do-while loop : The do while loop is similar to while loop with the only difference that it
checks the condition after executing the statements, i.e it will execute the loop body one
time for sure because it checks the condition after executing the statements.
Syntax :
do
statements..
} while (condition);
Flowchart:
xample:
using System;
class Geeks
int x = 21;
do
[Link]("GeeksforGeeks");
x++;
Output
GeeksforGeeks
Class and Objects in C#In C#, classes and objects are the core building blocks of object-
oriented programming. A class provides a way to model real-world entities by grouping
data and behavior together, while objects represent the actual entities created from these
classes. This approach makes programs more organized, reusable and easier to maintain.
Classes
A class in C# is a user-defined type that encapsulates data and behavior. It can contain
fields, properties, methods, events and constructors. A class itself does not occupy memory
until objects are created from it.
Syntax:
class ClassName{
// Fields
// Properties
// Methods
Declaration of Class
However, some optional attributes can be used with class declaration according to the
application requirement. Class declarations can include these components, in order:
Class Identifier: The name of the class, conventionally starting with a capital letter.
Base Class (Optional): Specifies a parent class to inherit from, using the : symbol.
using System;
// field variables
public int a, b;
[Link]("Class in C#");
Objects
Object in C# is something you create from a class, which represents a real-world entity and
lets you use the data and actions defined in that class.
In C# an object consists of :
State: It is represented by attributes of an object and reflects the properties of an object.
Behaviour: It is represented by the methods of an object and also reflects the response of an
object with other objects.
Identity: It gives a unique name to an object and enables one object to interact with other
[Link] Objects (Also called instantiating a class)
Note:
Classes define the blueprint of state, behavior and identity. Objects are the real instances
that actually hold the state, show behavior and carry identity.
Declaration of Object
When an object is created, the class is instantiated. All objects share the class’s behavior,
but each has its own unique state. A class can have multiple instances.
Example:
// Instance Variables
String name;
String breed;
int age;
String color;
// Constructor Declaration of Class same name as class
[Link] = name;
[Link] = breed;
[Link] = age;
[Link] = color;
return name;
return breed;
return age;
}
public String GetColor()
return color;
// Creating object
[Link]([Link]());
Arrays in C#
Last Updated : 09 Sep, 2025
An array is a linear data structure that stores a fixed-size sequence of elements of the same
data type in contiguous memory locations. It allows accessing elements using an index,
starting from 0.
The array has can contain primitive data types as well as objects of a class depending on
the definition of an array.
Whenever use primitives data types, the actual values have to be stored in contiguous
memory locations.
In the case of objects of a class, the actual objects are stored in the heap segment.
download
Array in C#
Syntax
Here,
<Data Type> : It define the element type of the array.
Note : Only Declaration of an array doesn’t allocate memory to the array. For that array
must be initialized.
Array Initialization
Aarray is a reference type so the new keyword used to create an instance of the array. We
can assign initialize individual array elements, with the help of the index.
Syntax:
Here, type specifies the type of data being allocated, size specifies the number of elements in
the array and Name_Array is the name of an array variable. And new will allocate memory
to an array according to its size.
Examples: To Show Different ways for the Array Declaration and Initialization
Syntax
Use Cases
Example
Defining array with size and assigning the values at the same time
The value of the array is directly initialized without taking its size
Arrays can be initialized after the declaration. It is not necessary to declare and initialize at
the same time using the new keyword.
Example:
// Initialization of array
str1 = new string[5]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” };
str2 = new string[5]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” };
Note :
Initialization without giving size is not valid in C#. It will give a compile-time error.
string[] str1;
We can access an array value through indexing, placed index of the element within square
brackets with the array name.
using System;
class Geeks
// Main Method
int[] intArray;
intArray[1] = 20;
// so on...
intArray[2] = 30;
intArray[3] = 40;
intArray[4] = 50;
[Link]("");
foreach(int i in intArray)
[Link]("");
[Link]("while loop :");
int j = 0;
j++;
[Link]("");
int k = 0;
do
k++;
Output
For loop : 10 20 30 40 50
For-each loop : 10 20 30 40 50
while loop : 10 20 30 40 50
Do-while loop : 10 20 30 40 50
Types of Arrays in C#
Jagged Array
In this array contains only one row for storing the values. All values of this array are stored
contiguously starting from 0 to the array size.
The above array contains the elements from arrayint[0] to arrayint[4]. Here, the new
operator has to create the array and also initialize its element by their default values. Above
example, all elements are initialized by zero, Because it is the int type.
Example:
using System;
class Geeks
string[] weekDays;
weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Output
Sun Mon Tue Wed Thu Fri Sat
2. Multidimensional Arrays
The multi-dimensional array contains more than one row to store the values. It is also
known as a Rectangular Array in C# because it’s each row length is same. It can be a 2D-
array or 3D-array or more. To storing and accessing the values of the array, one required
the nested loop.
Syntax:
using System;
class Geeks
{
{ 4, 5, 6 } },
{ { 7, 8, 9 },
{ 10, 11, 12 } } };
Output
arr[1][0][1] : 8
arr[1][1][2] : 12
3. Jagged Arrays
An array whose elements are arrays is known as Jagged arrays it means “array of arrays“.
The jagged array elements may be of different dimensions and sizes.
Example: Showing how to declare, initialize and access the jagged arrays
using System;
class Geeks
new int[] { 2, 4, 6, 8 } };
[Link]("Arrays :");
[Link]();
Output
Arrays :
Elements[0] Array: 1 3 5 7 9
Elements[1] Array: 2 4 6 8
Points To Remember:
GetLength(int): returns the number of elements in the first dimension of the Array.
When using jagged arrays be safe as if the index does not exist then it will throw exception
which is IndexOutOfRange.
Strings in C#
Example:
string s1 = “GeeksforGeeks”;
String s2 = “GFG”;
[Link] (defined in the .NET base class library) is a collection of char objects. A
string can hold up to ~2 billion characters (2GB). Strings are immutable, meaning their
value cannot be changed after creation.
using System;
class Geeks
[Link] Name;
// initialization of String
Name = "Geek";
String id;
// initialization of String
id = "33";
string mrk;
// initialization of String
mrk = "97";
Output
Name: Geek
Id: 33
Marks: 97
Rank: 1
Immutable: Once created, the content of a string cannot be altered. Any modification
results in the creation of a new string.
Reference Type: Strings are reference types, but they behave like value types in some
scenarios, such as comparison.
Unicode Support: Strings can contain any Unicode character, allowing support for multiple
languages.
Null and Embedded Nulls: Strings can be null and may also contain embedded null
characters (\0).
Chars: It is used to get the Char object at a specified position in the current String object.
Length: It is used to get the number of characters in the current String object. To know
more about the string class properties please go to String Properties in C#.
A string can be read out from the user input. ReadLine() method of console class is used to
read a string from user input.
Example:
using System;
class Geeks
Input:
Hello Geeks !
Output:
Method
Syntax / Example
string str = [Link]("{0} {1} Cars color " + "are {2}", [Link](), cname, clr);
Example:
using System;
// Index of
int i=1;
int j=2;
int sum= i + j;
, i , j , sum );
Output
Method 1: Geeks
Method 2: GeeksofGeeks
Method 3: GEEKS
Method 4: For
C# String Operations
There are multiple String Operations which we can perform in String in C#. Let us
demonstrate the operations using the example as mentioned below:
// Main Method
// Interpolation is performed
[Link](res);
Output
GeeksforGeeks is the Organisation Name.
Length: 39
using System;
first=[Link]();
second=[Link]();
first=[Link]("E","e");
[Link](first+second);
Output
Element at index 2: E
GeeksforGeeks
In the above, two example we have explored many methods we are not full aware of so,
there is a list of Methods associated with Strings in C# attached below.
Methods of C# String
Method
Description
Return Type
Example
IndexOf
Integer
[Link]("World");
StartsWith
Boolean
[Link]("Hello");
EndsWith
Boolean
[Link]("World!");
ToUpper
String
[Link]();
ToLower
[Link]();
Split
String Array
[Link](',');
Join
String
Contains
Boolean
[Link]("World");
PadLeft
Pads a string with spaces or a specified character to a certain length.
String
[Link](20, '*');
PadRight
Pads a string on the right with spaces or a specified character to a certain length.
String
[Link](20, '*');
Remove
String
[Link](5, 7);
Insert
String
Trim
Removes leading and trailing whitespaces.
String
[Link]();
Replace
String
[Link]("fun", "awesome");
String Array
We can also create the array of string and assigns values to it. The string arrays can be
created as follows:
Example:
using System;
class Geeks
str_arr[0] = "Geeks";
str_arr[1] = "For";
str_arr[2] = "Geeks";
Output