AWD unit 1
Q1. What is .net framework? Explain its architecture in brief. 2018
Q1. Draw and explain .net framework architecture. 2023
Layer 1 VB VC++ VC# Jscript Microsoft
visual studio
Layer 2 Console/windows Web application
Layer 3 ADO .Net : Data & XML
Layer 4 BCL/FCL
Layer 5 CLR
Layer 6 OS
Layer 1: .NET supports multiple programming languages, including C#, [Link], and others,
which can all be used to build applications.
Layer 2: .NET allows for different types of applications like Console (command-line),
Windows (desktop), and Web (online) applications.
Layer 3: ADO (Active Data Object) .Net and XML (Extensible Markup Language) are used
for data management.
• [Link]: Connects frontend (user interface) and backend (database).
• XML: Acts as temporary data storage.
Layer 4: Contains libraries like BCL (Base Class Library) and FCL (Framework Class Library)
that provide essential functions, similar to header files in C or packages in Java.
Layer 5: The Common Language Runtime (CLR) is responsible for running .NET
applications, handling tasks like memory management and error handling.
Layer 6: The Operating System (OS) runs either on 32-bit or 64-bit, hosting the .NET IDE
(Visual Studio), which is used to develop .NET applications.
To develop our application, we’ll use Microsoft Visual Studio and C# as the programming
language. This will allow us to create either a console, Windows, or web application based
on our needs.
Q2. How does the garbage collector function in the context of .net? provide the brief
overview of the base class library in .Net. 2023
Garbage Collector (GC) in .NET
1. Automatic Memory Management (AMM): The GC in .NET automatically manages
memory, removing the need for manual memory handling by the programmer.
2. Purpose: Designed to ensure efficient memory use in applications, which is crucial for
Windows and web applications.
3. Manual vs. Automatic: Manually cleaning memory can lead to errors, so GC
automatically manages memory on the heap.
4. Generations: The heap is divided into three generations (0, 1, and 2) to improve
efficiency:
• Generation 0: Holds short-lived objects, often deleted quickly.
• Generation 1: Acts as a buffer for objects that survive Generation 0 but may not be
long-lived.
• Generation 2: Stores long-lived objects, cleared less frequently.
5. Process Phases: GC works in three main phases:
• Marking: Identifies all active (live) objects.
• Reallocating: Updates references to objects if they are moved.
• Compacting: Removes dead objects, freeing memory.
6. Efficient Allocation: New objects start in Generation 0, with GC promoting objects to
higher generations if needed.
7. Root and Reference Objects: GC doesn’t clear the root application objects or objects still
in use.
Base Class Library (BCL) in .NET
1. Core Library: BCL provides essential libraries, methods, and classes for .NET
applications.
2. Versatile Functions: Includes classes for tasks like file handling, data collections, and
more.
3. Uniformity Across Languages: BCL is accessible from any .NET-supported language,
ensuring consistent code functionality.
4. Data Handling Support: Provides tools for database connectivity, XML processing, and
network communication.
Q3. What are .net languages? Explain various features of c#. 2019
.NET Languages and C# Features
1. .NET Languages: The .NET framework supports multiple languages, including C#, Visual
Basic .NET ([Link]), and F#. These languages allow developers to build diverse
applications within the .NET environment.
Features of C#:
2. Object-Oriented: C# is an object-oriented language, which means it organizes code
using classes and objects to enhance code structure, reuse, and readability.
3. Type Safety: C# has strong type-checking to ensure that variables are correctly used and
prevent type errors.
4. Automatic Memory Management: C# leverages .NET’s garbage collection, automatically
managing memory and handling object allocation and deallocation.
5. Interoperability: C# can interact with other languages within the .NET framework,
making it versatile for mixed-language development projects.
6. Rich Library Support: C# has extensive libraries for various functionalities such as file
I/O, networking, and data manipulation, accessible through the Base Class Library (BCL).
7. Error Handling: C# provides structured exception handling, which allows developers to
manage errors effectively using try-catch blocks.
8. Scalability and Performance: C# is designed for high performance and scalability,
making it suitable for both small applications and large, complex systems.
9. Events and Delegates: C# supports events and delegates, which enable the use of event-
driven programming and make it suitable for interactive applications.
10. Language Integration: C# works well with other .NET languages and can leverage
.NET’s unified type system for consistent language behavior.
Q4. Explain one-dimensional and two-dimensional arrays with proper syntax and example.
2019
One-Dimensional Arrays
1. Definition: A one-dimensional array, also called a "vector," is a list of elements stored
sequentially.
2. Syntax: The basic syntax for declaring a one-dimensional array is:
DATATYPE[] VARIABLE = new DATATYPE[size];
int[] arr = new int[size];
3. Example Initialization:
Integer Array: int[] arr = new int[5];
Character Array: char[] arr1 = new char[4] { 'v', 't', 's', 'y' };
String Array: string[] arr2 = new string[4] { "jan", "feb", "mar", "apr"};
4. Accessing Elements: Elements can be accessed by their index, starting at 0, such as
arr[0].
5. Use Cases: One-dimensional arrays are useful for storing simple lists of data, such as a
list of numbers, names, or items.
Two-Dimensional Arrays
1. Definition: A two-dimensional array, often called a "matrix," is structured in rows and
columns.
2. Syntax: To declare a two-dimensional array, the syntax is:
DATATYPE[,] VARIABLE = new DATATYPE[SIZE];
int[,] arr = new int[rows, columns];
3. Example Initialization:
Integer Array: int[,] arr = new int[3, 3];
Matrix Initialization: int[,] arr = new int[3, 3] { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
4. Accessing Elements: Elements are accessed by specifying the row and column index, like
arr[0,1] = 10;.
5. Use Cases: Two-dimensional arrays are useful for storing tabular data, like matrices or
grids.
Q5. Explain jagged array with an example. 2018
1. A jagged array is an array containing other arrays, where each array can have a
different size or length.
2. It's often called an "array of arrays."
3. Example declaration in C#:
int[][] s = new int[3][];
4. The above declaration means s can hold three arrays.
5. Each sub-array can be of different sizes. For instance:
s[0] = new int[] {1, 2, 3, 4};
s[1] = new int[] {1, 2, 3};
s[2] = new int[] {1, 2, 3, 4, 5};
6. Accessing elements uses two indices, one for the main array and one for the sub-array,
like s[0][1], which refers to the second element in the first array, which is 2.
7. A loop can be used to display all elements in the jagged array:
for(int i = 0; i < [Link]; i++) {
for(int j = 0; j < s[i].Length; j++) {
[Link](s[i][j] + "\t");
}
}
8. Another example combines declaration and assignment:
int[][] t = new int[][] { new int[] {92, 93, 94}, new int[] {95, 96, 97, 98} };
9. Jagged arrays are useful when dealing with rows of different lengths, like in matrices
with variable column counts.
10. They offer flexibility in memory allocation, allowing each sub-array to consume only as
much space as needed.
Q6. Explain foreach loop with suitable example. 2022
1. Purpose: The foreach loop is used to iterate through elements in a collection or array
without using an index.
2. Syntax:
foreach (type element in collection) {
// Code to execute
}
3. Example:
int[] numbers = {1, 2, 3, 4};
foreach (int num in numbers) {
[Link](num);
}
4. Explanation of Example: This loop prints each number in the numbers array.
5. Automatic Handling: The foreach loop automatically stops after the last element,
reducing errors related to array bounds.
6. Read-Only Access: foreach provides read-only access to elements in the collection.
7. Simplified Syntax: It eliminates the need for a counter variable, making the code
simpler and easier to read.
8. Collections Support: The foreach loop works with arrays, lists, dictionaries, and other
collections that implement IEnumerable.
9. Benefit: It is especially useful for iterating over items when you do not need to modify
the collection.
10. Limitation: foreach cannot be used to modify elements directly in the collection, only
to read them.
Q7. Explain the process of datatype conversion in C# and identify its various types. 2023
1. Data type conversion in C# means changing a variable from one type to another.
2. Implicit conversion happens automatically when converting from a smaller to a larger
data type, like int to double.
int a = 512;
double b = a; // Automatic conversion to double
3. Examples of implicit conversions:
Convert from datatype Convert to datatype
Byte short, int, long, float, double
Short int, long, float, double
Int long, float, double
Long float, double
Float Double
4. Explicit conversion requires using casting for converting from a larger to a smaller data
type, like double to int.
double b = 512.5;
int a = (int)b; // Explicit conversion to int
5. Examples of explicit conversions:
• double to int
• base type to derived type
6. C# offers built-in methods for conversions, such as:
• ToInt32 for converting to a 32-bit integer.
• ToString for converting to a string.
7. More built-in conversion methods include:
• ToChar for character conversion.
• ToByte for byte conversion.
• ToDouble for double conversion.
8. Use explicit conversion when data loss might occur, like dropping decimal values when
converting double to int.
9. Automatic conversion avoids data loss but is limited to compatible data types.
10. These conversions help ensure correct data types are used in operations, improving
code reliability.
Q8. what is namespace? Explain with the help of an example. 2018
Q8. What is namespace? Describe the system namespace. 2022
1. A namespace in C# is a container for organizing code, which keeps names separate to
avoid conflicts.
2. It is declared using the namespace keyword and doesn't require access modifiers, as all
namespaces are implicitly public.
3. Namespaces can hold various elements, including other namespaces, classes,
structures, enumerations, and interfaces.
4. The syntax for a namespace declaration is:
namespace namespace_name {
// code declarations
}
5. Example:
namespace Bank {
class Saving {
public string name;
public string mobile;
}
}
6. This example creates a Bank namespace with a class Saving to store customer details.
7. The syntax for call namespce
namespace_name.item_name;
8. To use the Saving class within the Bank namespace:
[Link] s = new [Link]();
[Link] = "John";
[Link] = "1234567890";
9. System namespace: This is a base namespace in C#, containing essential classes for data
types, I/O, exceptions, and system-level tasks.
10. The System namespace includes sub-namespaces like [Link] for file handling and
[Link] for data structures.
11. Overall, namespaces help organize and manage code, especially in larger programs,
preventing naming conflicts.
Q9. What is an assembly? Explain the difference between public and private assembly.
2022
1. An assembly is a small unit of deployment in C#, containing either a .dll (Dynamic Link
Library) or .exe (executable) file.
2. When we compile a class library, a .dll file is created, which can be reused by other
applications but does not contain a main() method.
3. Executable files (.exe) are generated when applications are compiled and can be run
independently.
4. Assemblies come in two types: public and private.
Feature Public Assembly Private Assembly
Usage Used by multiple applications Used by a single application
Location Stored in Global Assembly Cache Stored in the application’s own
(GAC) folder
Sharing Shared across applications Not shared outside the application
Strong Name Requires a strong name Does not require a strong name
Special Term Also known as a shared No special term
assembly
Default Status Not the default assembly type Default assembly type
Main() Method Does not contain a main() Can be part of a .exe with main() if
method standalone
Examples Libraries like System in .NET Application-specific libraries
framework
Accessibility Accessible globally Accessible only within the app
folder
Installation Typically installed in Windows Installed in the application’s folder
GAC folder
Q. Write a program in C# to demonstrate multiple inheritance using interfaces. 2018
Q10. Explain various types of constructor in c#. 2018
Q10. Explain static constructor and copy constructor with examples. 2019
1. Constructor: A constructor is a special method in C# that initializes objects. It has the
same name as the class and no return type.
2. Default Constructor:
Automatically created if no other constructor is defined.
It sets default values to fields (e.g., int fields get 0, strings get null).
Example:
public class Example {
public Example() { // Default constructor
[Link]("Object created.");
}
}
3. Parameterized Constructor:
Takes parameters to initialize fields with specific values when creating an object.
Example:
public class Example {
public int x;
public Example(int val) { // Parameterized constructor
x = val;
}
}
4. Copy Constructor:
Initializes an object by copying fields from another object.
Example:
public class Example {
public int x;
public Example(Example obj) { // Copy constructor
x = obj.x;
}
}
5. Static Constructor:
Used to initialize static members of a class.
Runs only once, before any instance of the class is created.
Example:
public class Example {
static int staticValue;
static Example() { // Static constructor
staticValue = 10;
}
}
6. Private Constructor:
Prevents instantiation from outside the class, often used in singleton patterns.
7. Chained Constructor:
Allows one constructor to call another within the same class using this keyword.
8. Destructors:
Though not a constructor, a destructor (~ClassName) cleans up resources when an object
is destroyed.
9. Overloaded Constructor:
Allows multiple constructors with different parameters to provide various ways to create
objects.
10. Constructors play a key role in setting up an object’s initial state and can be
customized based on application needs.
Q11. Define inheritance. Explain single inheritance with example. 2019
1. Inheritance: Inheritance in C# allows a class to inherit properties and methods from
another class, promoting code reusability.
2. In this example, class B inherits from class A, meaning B can use the methods defined in
A.
3. Single Inheritance: This code uses single inheritance, where B inherits only from A.
4. Code Explanation:
• class A defines a method square(int n), which returns the square of an integer n.
• class B, inheriting from A, defines an additional method cube(int n), which returns
the cube of n.
5. By inheriting from A, class B has access to both the square and cube methods.
6. In the Button1_Click method in Page_Load, an object of class B is created to access both
methods.
B obj = new B();
7. Calculation Example:
int x = [Link](n); calls the inherited square method to calculate the square of
n.
int y = [Link](n); calls the cube method to calculate the cube of n.
8. The square and cube values are displayed on labels:
[Link] = "Square is " + [Link]();
[Link] = "Cube is " + [Link]();
9. This example demonstrates single inheritance by showing how class B inherits and
extends the functionality of class A.
10. Output: If n is 3, Label1 shows "Square is 9" and Label2 shows "Cube is 27".
Q12. Distinguish between interfaces and abstract classes. 2022
Feature Interface Abstract Class
Provides a base class with both
Defines a contract or behavior that
Purpose implemented and abstract
classes must implement
(unimplemented) methods
Cannot have method Can have both abstract methods
Implementation implementations (all methods are and methods with code
abstract by default) (implementations)
A class can implement multiple A class can inherit only one
Inheritance
interfaces abstract class
Members are public by default and Allows access modifiers for
Modifiers
cannot have access modifiers members
Cannot contain fields (only Can contain fields, properties,
Fields
properties and methods) and methods
Can have constructors to initialize
Constructor Does not allow constructors
fields
Used when multiple classes need Used for classes with common
Use Case to follow the same contract behaviors or properties that need
without code reuse a base implementation
abstract class Animal { public
Example interface IAnimal { void Eat(); }
abstract void Eat(); }
Default C# 8.0+ supports default method Abstract classes can have
implementation in interfaces complete method
Implementation (limited) implementations
When unrelated classes need to When related classes share code
When to Use
implement the same behavior but also have abstract methods
Q. Write a sample C# program to demonstrate class, object and method call. Use comment
wherever require. 2022
Q13. How to derive new class from the base class? Give example. 2023
1. Inheritance in C#: Deriving a new class from an existing base class allows a derived class
to inherit fields and methods of the base class.
2. Syntax: The derived class uses a colon (:) followed by the base class name.
public class B : A { }
3. In the example code:
• A is the base class with a method square(int n) that returns the square of a
number.
• B is the derived class inheriting from A.
4. Example Code:
public class A {
public int square(int n) {
return (n * n);
}
}
public class B : A {
public int cube(int n) {
return (n * n * n);
}
}
5. Usage:
• Since B inherits from A, it can use both square (from A) and cube (defined in B).
6. In the Button1_Click event handler:
• An object obj of class B is created.
• The square and cube methods are called to calculate the square and cube of a user-
entered number n.
7. Calling Methods:
B obj = new B();
int x = [Link](n); // Calls inherited square method
int y = [Link](n); // Calls cube method from B
8. Display Results: The results are displayed in Label1 and Label2.
[Link] = "Square is " + [Link]();
[Link] = "Cube is " + [Link]();
9. This example shows how class B derives and extends the functionality of A.
10. Benefit: Inheritance here reduces code duplication by letting B reuse the square
method of A.
Q14. What steps are involved in constructing a fundamental class in c#? Please delineate
the different class modifiers. 2023
1. Class Definition: To create a fundamental class in C#, use the class keyword, followed by
the class name.
public class Example { }
2. Steps for Constructing a Class:
• Step 1: Define the class name.
• Step 2: Add fields or properties to store data.
• Step 3: Include methods to define the behaviors or actions of the class.
• Step 4: Optionally, add a constructor to initialize objects when they’re created.
3. Example Class:
public class Car {
public string Color;
public void Drive() {
[Link]("Car is driving");
}
}
4. Class Modifiers:
• Modifiers control the accessibility and behavior of classes.
5. Common Class Modifiers:
• public: The class is accessible from any other class in the project.
• private: Limits the accessibility of the class (usually used within nested classes).
• protected: Accessible within the class and by derived classes.
• internal: Accessible only within the same assembly (project).
6. Static Class:
• Declared with static to indicate that the class can’t be instantiated. It’s used for
utility or helper classes.
7. Abstract Class:
• Marked with abstract and can’t be instantiated. It’s meant to be a base class for
other classes.
8. Sealed Class:
• Marked with sealed to prevent other classes from inheriting it.
9. Partial Class:
• Declared with partial, allowing the class definition to be split across multiple files.
10. Using these steps and modifiers, you can construct classes with varying levels of
accessibility and functionality, depending on the needs of the application.
Q15. What is delegate? Explain multicast delegate with an example. 2018
1. Delegate: A delegate in C# is a type that represents references to methods with a
specific parameter list and return type.
2. Delegates are similar to function pointers in C and allow methods to be passed as
parameters.
3. Example of a Delegate:
• Here, a delegate delop is declared to hold references to methods that take two
integers as parameters and return an integer:
• delegate int delop(int a, int b);
4. Using the Delegate:
• An instance of delop is created to point to the add method:
• delop d1 = new delop([Link]);
5. Multicast Delegate:
• A multicast delegate holds references to multiple methods.
• When invoked, it calls all the methods it references, in the order they were added.
6. Example of Multicast Delegates:
d1 += new delop(o1.sub1); // Adding another method to delegate
• Here, d1 will call both add and sub1 methods in sequence.
7. Execution of Multicast Delegate:
• When d1(x, y) is called, it first performs addition and then subtraction.
8. Output:
• [Link] and [Link] show results for both operations.
9. Benefit: Multicast delegates allow multiple methods to be executed with a single call,
which is useful for event handling.
10. This example illustrates delegates and can be extended to multicast by adding more
methods to d1.
Q16. What is property? Explain read write property with proper syntax and example. 2019
1. Property: A property in C# is a member that provides a flexible way to read, write, or
compute values of a private field.
2. Properties look like fields but are accessed through get and set methods, which control
how values are read or modified.
3. Read-Write Property: A property with both get and set methods, allowing reading and
writing of values.
4. Syntax for a Read-Write Property:
private int _age; // Private field
public int Age {
get { return _age; } // Getter to read the value
set { _age = value; } // Setter to write the value
}
5. In this example, the Age property controls access to the _age field.
6. Getter (get): Returns the value of the private field (_age) when accessed.
7. Setter (set): Assigns a new value to the private field, using value to represent the
assigned value.
8. Usage Example:
Age = 25; // Calls the `set` method to assign 25 to _age
int myAge = Age; // Calls the `get` method to retrieve _age
9. Benefits: Properties allow validation or additional logic to be added when getting or
setting a field’s value.
10. Example Class:
public class Person {
private string name;
public string Name {
get { return name; }
set { name = value; }
}
}
• This Name property allows reading and writing the name field.
Q17. List various reference types in c#. Also explain boxing operation with example. 2019
1. Reference Types: In C#, reference types store references to memory locations instead of
actual data. Examples of reference types include:
• Class: Defines an object with properties and methods.
• Array: A collection of elements.
• String: Represents a sequence of characters.
• Delegate: Holds references to methods.
• Interface: Defines a contract of methods and properties without implementation.
2. Boxing Operation: Boxing is the process of converting a value type (like int, float) to a
reference type (like object).
3. Purpose of Boxing: It allows a value type to be treated as an object, making it
compatible with methods that require reference types.
4. Example of Boxing:
int num = 10; // Value type
object obj = num; // Boxing: num is wrapped as an object
5. In this example, the integer num is boxed, meaning it’s wrapped into an object.
6. Unboxing: Unboxing is the reverse of boxing, where the value type is extracted from the
object.
int num2 = (int)obj; // Unboxing: Extracting the value from the object
7. Performance Impact: Boxing and unboxing can impact performance, as boxing involves
copying data to a new location in memory.
8. Usage Scenario: Boxing is useful when working with collections like ArrayList, which
stores elements as objects.
9. Boxing Example in ArrayList:
ArrayList list = new ArrayList();
[Link](num); // num is boxed when added to ArrayList
10. This process helps in making value types work with reference types but should be used
carefully to avoid unnecessary performance costs.
Q18. Define the accessibility modifiers-public, private, protected, internal, and protected
internal. 2022
1. Public: The public modifier allows access from any other code, whether in the same
assembly or a different one. Members marked public are accessible everywhere.
public int age;
2. Private: The private modifier restricts access to within the same class only. Members
marked private are hidden from other classes and are only accessible within the class they
are defined in.
private int id;
3. Protected: The protected modifier allows access within the same class and by derived
(child) classes. It’s useful for inheritance, as derived classes can access protected members
of the base class.
protected string name;
4. Internal: The internal modifier restricts access to within the same assembly. Members
marked internal are accessible by any code within the same project but not from other
assemblies.
internal double salary;
5. Protected Internal: The protected internal modifier combines the features of both
protected and internal. Members with this modifier are accessible within the same
assembly and by derived classes, even if they are in a different assembly.
protected internal string department;
6. Usage Scenarios: These modifiers help control data and method visibility, enhancing
security and structure in programs.
7. Example of Class Using Modifiers:
public class Employee {
private int id;
protected string name;
internal double salary;
public string position;
protected internal string department;
}
8. Summary:
• public: Accessible everywhere.
• private: Accessible only within the class.
• protected: Accessible within the class and derived classes.
• internal: Accessible within the same assembly.
• protected internal: Accessible within the assembly and derived classes.
9. Importance: Modifiers help in encapsulation, allowing control over who can access
specific parts of a class.
10. These accessibility levels ensure that sensitive information and methods are protected
and accessed only where intended.
19. Explain any five properties/methods of array class. 2023
1. Length: The Length property returns the total number of elements in an array across all
dimensions.
int[] numbers = {1, 2, 3};
int len = [Link]; // len is 3
2. Rank: This property returns the number of dimensions of the array.
int[,] matrix = new int[3, 4];
int dimensions = [Link]; // dimensions is 2
3. GetValue(index): This method retrieves the value at the specified index in the array.
int val = [Link](1); // val is 2
4. SetValue(value, index): Assigns a specified value to the element at the given index.
[Link](10, 1); // sets the second element to 10
5. Sort(): Sorts the elements of a one-dimensional array in ascending order.
[Link](numbers); // numbers is now sorted
6. Clear(): This method clears all elements in an array or sets them to their default values
(e.g., 0 for numbers, null for objects).
[Link](numbers, 0, [Link]); // Clears all elements in `numbers`
7. IndexOf(): Finds the index of the first occurrence of a specified value in a one-
dimensional array.
int index = [Link](numbers, 10); // Finds the index of the value 10
These properties and methods allow for efficient management of arrays by providing
ways to access, modify, and organize elements.