Unit 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Unit 1 chapter 1 and chapter2

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.

 It is a platform for application developers.


 It is a Framework that supports Multiple Language and Cross language integration.
 It has IDE (Integrated Development Environment).
 Framework is a set of utilities or can say building blocks of our application system.
 .NET Framework provides interoperability between languages i.e. Common Type System (CTS).
 .NET Framework also includes the .NET Common Language Runtime (CLR), which responsible
for maintaining the execution of all applications developed using the .NET library.
 The .NET Framework consists primarily of a gigantic library of code.

Components of the .Net Framework:

User Interface and Program:

We can create various type of application using .net framework such as

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.

CLS (Common language Specification): It is a subset of common type system


(CTS) that defines a set of rules and regulations which should be followed by every
language that comes under the .net framework.
Microsoft .NET Assemblies:A .NET assembly is the main building block of
the .NET Framework. It is a small unit of code that contains a logical compiled code
in the Common Language infrastructure (CLI), which is used for deployment,
security and versioning.

FCL (Framework Class Library): 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.

2) Explain CLR in detail.

 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.

Components of the CLR:

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

Functions of the CLR

 Garbage Collector
 Exception handling
 Type safety
 Memory management (using the Garbage Collector)
 Security
 Improved performance

3) What are .NET languages? Explain various features of C# languages.

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.

3)Difference between for and for each loop

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.

for (initialization; condition;


Syntax iteration) foreach (array as value )

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

foreach (int x in arr1)


Syntax Example for (int i = 0; i < =10; i++) {Response.Write(i) ;} {Response.Write(x); }

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.

4) Explain jagged array with example

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

A jagged array can be declared and initialized as follows:

datatype[][] arrayname=new datatype[rowsize][];

arrayname[0]=new datatype[]{val1,val2,val3, ...};

arrayname[1]=new datatype[]{val1,val2,val3, ...};

arrayname[2]=new datatype[]{val1,val2,val3, ...};(diagram)

class numadd
{

public static void Main()

int[][] x=new int[4][];

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.

Ans: A one-dimensional array is a linear collection of elements where each element is


accessed by a single index

Syntax:

dataType[] arrayName = new dataType[size];

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;

Console.WriteLine("One-Dimensional Array Elements:");


for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}

A two-dimensional array is an array of arrays, forming a matrix-like structure with


rows and columns. It is accessed using two indices - one for rows and one for
columns.

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;

// Accessing and printing elements


Console.WriteLine("Two-Dimensional Array Elements:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}

6) Explain method overriding with example

Ans:

 Method overriding in C# is achieved through inheritance.


 If a class is inherited by a derived class, then the derived class can override the base class
method. This concept is termed as Method Overriding.
 Method Overriding is implemented in C# using the keywords virtual and override. The base class
method that has to be overridden must be marked as virtual or abstract (if the base class is an
abstract class).
 The derived class method that overrides the base class method should be marked with the
keyword override.

using System;

class A

public virtual void Test() { Console.WriteLine("A::Test()"); }

class B : A

public override void Test() { Console.WriteLine("B::Test()"); }

class C : B

public override void Test() { Console.WriteLine("C::Test()"); }

class Program

static void Main(string[] args)

A a = new A();

B b = new B();

C c = new C();

a.Test(); // output --> "A::Test()"


b.Test(); // output --> "B::Test()"

c.Test(); // output --> "C::Test()"

a = new B();

a.Test(); // output --> "B::Test()"

b = new C();

b.Test(); // output --> "C::Test()"

Console.ReadKey();

7) Is Multiple Main () allowed in C#? Justify

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

static void Main(string[] args)

Console.WriteLine("I am from Class A");

Console.ReadLine();

class B

static void Main(string[] args)

Console.WriteLine("I am from Class B");


Console.ReadLine();

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

9) What is delegate? Explain multicast delegate with an example.

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.

Following are condition for multicast delegate:

void is used as return type

Out parameter is not allowed as arguments

Example

public delegate void MyDelegate();

class Abc

public void Show()

Console.WriteLine (“New Delhi”);


}

public void Display()

Console.WriteLine (“New York”);

class Xyz

public static void Main()

Abc a1=new Abc();

MyDelegate m1=new MyDelegate(a1.Show);

MyDelegate m2=new MyDelegate(a1.Display);

m1=m1+m2+m1+m2-m1;

m1();

Console.Read();

} Output: New York New Delhi New York(Remainder to check)

1) oop features-from book


2) Explain various Types of Constructors in C#

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#

There are 5 types of constructor in C# as listed below

Default Constructor
Parameterized Constructor

Copy Constructor

Static Constructor

Private constructor

Default Constructor: A constructor without any parameters is called as default constructor

Example of Default Constructor :

using System;

class MyClass
{

public MyClass()
{
Console.WriteLine("Default constructor called");
}

class Program
{
static void Main()
{

MyClass obj = new MyClass(); // This calls the default constructor

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.

Example of Parameterized Constructor :

using System;

class MyClass
{
private int myNumber;

public MyClass(int number)


{
myNumber = number;
}

public void DisplayNumber()


{
Console.WriteLine("My number is: {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.

Example of Copy Constructor:

using System;

class technicalscripter {

private string topic_name;


private int article_no;

public technicalscripter(string topic_name, int article_no)


{
this.topic_name = topic_name;
this.article_no = article_no;
}

// 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();
}
}
}

public class GFG {

static public void Main()


{

technicalscripter t1 = new technicalscripter(" C# | Copy Constructor", 38);

technicalscripter t2 = new technicalscripter(t1);

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.

Example of Static Constructor: using System;

class test

static int x;

static test()

Console.WriteLine("static cons ");

x = 10;//set the values for static member here


}

public static void show()

Console.WriteLine("show of x--->" + x);

class testmain

public static void Main()

test.show();

Console.ReadLine();

Private constructor: A private constructor is a special instance constructor. It is commonly used in


classes that contain static members only. This type of constructors is mainly used for creating singleton
object. If you don't want the class to be inherited we declare its constructor private.

Example:

using System;

class test

private test()

Console.WriteLine("private cons ");

public test(int x)
{

Console.WriteLine("non private cons--->"+ x);

Clas+s testmain

public static void Main()

test t1 = new test();//Error'test.test()' is inaccessible due to its protection level

test t2 = new test(10);

Console.ReadLine();

3) Write short note on Value type and reference type-from book

Call by value and by reference

Ans: Call By Value


In "call by value," a copy of the argument's value is passed to the function. The function operates on this
copy, and any changes made to the parameter inside the function have no effect on the original
argument.

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

4) What is namespace? Explain with the help of an example.

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
{

public class MyClass


{
public void Display()
{
Console.WriteLine("Inside MyNamespace.MyClass.Display method");
}
}
}

namespace AnotherNamespace
{

public class AnotherClass


{
public void Show()
{
Console.WriteLine("Inside AnotherNamespace.AnotherClass.Show method");
}
}
}

class Program
{
static void Main()
{

MyNamespace.MyClass obj1 = new MyNamespace.MyClass();


obj1.Display();

AnotherNamespace.AnotherClass obj2 = new AnotherNamespace.AnotherClass();


obj2.Show();
}
}0

5) Write a short note on Assembly: in book


6) Inheritance

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.

In C#, there are 4 types of inheritance:

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:

You might also like