Unit 1
Unit 1
Unit 1
1) What is .NET Framework? Explain its architecture in brief./ Draw and explain .NET framework
architecture
Ans: .NET Framework is a platform created by Microsoft for building, deploying, and running
applications and services that use .NET technologies, such as desktop applications and Web services.
Console Application
Windows Application
Web application
Base class Library:Base class library is one of component of .Net Framework. It supplies a library of base
classes that we can use to implement applications quickly.
CLR (Common Language Runtime): Common Language Runtime (CLR) is the programming (Virtual
Machine component) that manages the execution of programs written in any language that uses
the .NET Framework, for example C#, VB.Net, F# and so on. We can say that it is heart and soul of .Net
Framework or backbone.
CTS (Common Type System): It specifies a standard that represent what type of data and
value can be defined and managed in computer memory at runtime.
Ans: Common Language Runtime (CLR) is the programming (Virtual Machine component) that
manages the execution of programs written in any language that uses the .NET Framework, for
example C#, VB.Net, F# and so on.
We can say that it is heart and soul of .Net Framework or backbone.
Programmers write code in any language, including VB.Net, C# and F# then they compile their
programs into an intermediate form of code called CLI in a portable execution file (PE) that can
be managed and used by the CLR and then the CLR converts it into machine code to be will
executed by the processor.
The information about the environment, programming language, its version and what class
libraries will be used for this code are stored in the form of metadata with the compiler that tells
the CLR how to handle this code.
The CLR allows an instance of a class written in one language to call a method of the class
written in another language.
CTS: Common Type System (CTS) describes a set of types that can be used in different .Net languages in
common. That is, the Common Type System (CTS) ensure that objects written in different .Net languages
can interact with each other. For Communicating between programs written in any .NET complaint
language, the types must be compatible on the basic level. These types can be Value Types or Reference
Types. The Value Types are passed by values and stored in the stack. The Reference Types are passed by
references and stored in the heap.
CLS: CLS stands for Common Language Specification and it is a subset of CTS. It defines a set of rules and
restrictions that every language must follow which runs under .NET framework. CLS enables cross-
language integration or Interoperability.
MSIL: It is language independent code. When you compile code that uses the .NET Framework library,
we don't immediately create operating system - specific native code. Instead, we compile our code into
Microsoft Intermediate Language (MSIL) code. The MSIL code is not specific to any operating system or
to any language.
Advantages -
MSIL provide language interoperability as code in any .net language is compiled on MSIL
Same performance in all .net languages
support for different runtime environments
JIT compiler in CLR converts MSIL code into native machine code which is executed by OS
Garbage Collector
Exception handling
Type safety
Memory management (using the Garbage Collector)
Security
Improved performance
Ans: .NET refers to a framework developed by Microsoft that supports building and running
applications for various platforms, including Windows, macOS, Linux, and more. .NET provides
a common set of tools, libraries, and languages to develop different types of applications. Several
languages are supported by the .NET framework, and among them, C# is the primary language
used for .NET development. However, other languages like VB.NET, F#, and more are also part
of the .NET ecosystem.
C# Language Features:
1. Simple and Easy to Learn: C# is designed to be easy to learn and use,
especially for developers familiar with C-style syntax. It has a simple and
straightforward syntax similar to C++ and Java.
2. Object-Oriented: C# is an object-oriented programming language,
supporting concepts like classes, objects, inheritance, encapsulation, and
polymorphism. This enables developers to create modular and reusable code.
3. Type-Safety: It's a strongly-typed language that enforces type safety at
compile-time, reducing errors and enhancing reliability.
4. Memory Management: C# uses automatic memory management through a
garbage collector, which deallocates memory for objects that are no longer in use,
making memory management easier for developers.
5. Platform Independence: C# and .NET applications can run on multiple
platforms, thanks to the .NET Core and .NET 5+ framework, which provide cross-
platform support.
Ans:
Usage Best suited for iterating over a range or Specifically designed for iterating through
collection with a known number of iterations. collections and arrays.
Requires manual initialization of a loop control Automatically assigns each element in the
Initialization variable. collection to the specified variable.
Int[] arr1={1,2,3,4,5};
Provides fine-grained control over loop Automatically iterates through each element
Control initialization, condition, and iteration. in the collection.
Can be used with any class that implements Requires a collection that implements
Collection Type IEnumerable or has a defined indexer. IEnumerable or an array.
You can manually modify the loop control You cannot directly modify the elements of
Modifying variable inside the loop body. the collection being iterated.
Generally faster for indexed collections or May have slightly more overhead due to its
Performance iterating over a specific range. automatic nature.
Useful for scenarios where you need to control Preferred when you want to iterate through all
Use Cases the loop precisely. elements in a collection.
Ans: Jagged array is also called Array of Array or variable sized array. In a jagged array, each row may
have different number of columns. It is equivalent to an array of variable sized one-dimensional arrays.
The Length property is used to get the length of each one-dimensional array. diagram
class numadd
{
x[0]=new int[2]{5,13};
x[1]=new int[3]{7,8,11};
x[2]=new int[4]{2,3,4,5};
x[3]=new int[1]{9};
for(int i=0;i<4;i++)
for(int j=0;j<x[i].Length;j++)
Console.Write(x[i][j]+"\t");
Console.Write("\n");
5) Explain one dimensional and two dimensional arrays with proper syntax and example.
Syntax:
Example:
int[] numbers = new int[5]; // Creates an integer array of size 5
// Initializing elements
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
Syntax:
dataType[,] arrayName = new dataType[rows, columns];
Example:
int[,] matrix = new int[3, 3]; // Creates a 3x3 integer matrix
// Initializing elements
matrix[0, 0] = 1;
matrix[0, 1] = 2;
matrix[0, 2] = 3;
matrix[1, 0] = 4;
matrix[1, 1] = 5;
matrix[1, 2] = 6;
matrix[2, 0] = 7;
matrix[2, 1] = 8;
matrix[2, 2] = 9;
Ans:
using System;
class A
class B : A
class C : B
class Program
A a = new A();
B b = new B();
C c = new C();
a = new B();
b = new C();
Console.ReadKey();
Ans: We can use more than one Main Method in C# program, But there will only one Main Method
which will act as entry point for the program.
using System;
class A
Console.ReadLine();
class B
In this code, there are two Main() methods defined. If you try to compile this program, you will
receive an error message indicating that there are conflicting entry points.
To resolve this issue, you need to remove or comment out one of the Main() methods, leaving
only one as the entry point for your program.
8)Method/function overloading
Ans: Method Overloading is the common way of implementing polymorphism. Function overloading is a
feature in many programming languages, including C#, that allows you to define multiple functions with
the same name but with different parameter lists. This means that you can have multiple versions of a
function that perform similar operations but take different types or numbers of arguments
Ans: A delegate in C# is similar to a function pointer in C or C++. It is a reference type object that allows
the programmer to encapsulate a reference to a method in it. It defines the return type and signature
for a method and can refer to any methods that are of the same format in the signature. When same
delegate is used to call method multiple time then it is referred as Multicast delegate or Multiple
delegate. The "+" operator adds a function to the delegate object and the "-" operator removes an
existing function from a delegate object.
Example
class Abc
class Xyz
m1=m1+m2+m1+m2-m1;
m1();
Console.Read();
Ans: It’s a special type off method which is called when an object is created. The name of the
constructor should be the name of the class and it does not return any value.
Types of Constructors in C#
Default Constructor
Parameterized Constructor
Copy Constructor
Static Constructor
Private constructor
using System;
class MyClass
{
public MyClass()
{
Console.WriteLine("Default constructor called");
}
class Program
{
static void Main()
{
Console.ReadLine();
}
}
Parameterized Constructor: A constructor which is having single input parameter or multiple input
parameters of same data types or different data types are known as parameterized constructor.
using System;
class MyClass
{
private int myNumber;
class Program
{
static void Main()
{
MyClass obj = new MyClass(10);
obj.DisplayNumber();
Console.ReadLine();
}
}
Copy Constructor A constructor that contains a parameter of same class type is called as copy
constructor. C# does not provide a copy constructor. A copy constructor enables you to copy the data
stored in the member variables of an object of the class into another new object means it helps to copy
data stored in one object into another new object of the same instance.
using System;
class technicalscripter {
// copy constructor
public technicalscripter(technicalscripter tech)
{
topic_name = tech.topic_name;
article_no = tech.article_no;
}
public string Data
{
get
{
return "The name of topic is: " + topic_name +
" and number of published article is: " +
article_no.ToString();
}
}
}
Console.WriteLine(t2.Data);
Console.ReadLine();
}
}
}
Static Constructor: A static constructor is used to initialize any static data, or to perform a particular
action that needs to be performed only once.
class test
static int x;
static test()
class testmain
test.show();
Console.ReadLine();
Example:
using System;
class test
private test()
public test(int x)
{
Clas+s testmain
Console.ReadLine();
Call by Reference
In "call by reference," a reference to the original argument is passed to the function. This means the
function can directly modify the original argument. In C#, you can use the ref or out keyword to
indicate that a parameter should be passed by reference Examples in book
Ans: Namespaces are C# program elements designed to help you organize our programs. They also
provide assistance in avoiding name clashes between two sets of code. Implementing Namespaces in
our own code is a good habit because it is likely to save us from problems later when we want to reuse
some of our code.
Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace name as
follows −
namespace namespace_name {
// code declarations
Eg:
using System;
namespace MyNamespace
{
namespace AnotherNamespace
{
class Program
{
static void Main()
{
Ans: Inheritance is the concept we use to build new classes using the existing class definitions. Through
inheritance we can modify a class the way we want to create new objects. The original class is known as
base or parent class & the modified class is known as derived or subclass or child class. The concept of
inheritance facilitates the reusability of existing code & thus improves the integrity of programs &
productivity of programmers.
1. Single inheritance: A derived class that inherits from only one base class.
2. Multi-level inheritance: A derived class that inherits from a base class and the derived
class itself becomes the base class for another derived class.
3. Hierarchical inheritance: A base class that serves as a parent class for two or more
derived classes.
4. Multiple inheritance C# does not support multiple inheritance. However, you can use interfaces to
implement multiple inheritance
5. Examples of each
.
7) How multiple inheritance achieved using interfaces?
Ans:C# does not support multiple inheritance. However, you can use interfaces to implement
multiple inheritance. The following program demonstrates this: