Dot Net Set2 Model
Dot Net Set2 Model
2MARKS:
1. What is an object?
An object is an instance of a class that contains data (fields) and
methods (functions) to perform actions. It represents a real-
world entity in programming.
class Demo
{
public Demo() // Constructor
{
Console.WriteLine("Constructor called.");
}
~Demo() // Destructor
{
Console.WriteLine("Destructor called.");
}
}
class Program
{
static void Main()
{
Demo obj = new Demo();
}
}
6. Define MSIL.
MSIL (Microsoft Intermediate Language) is the code generated
by .NET compilers, which is executed by the CLR (Common
Language Runtime).
5MARKS:
1. Garbage collections in. Net with program
Garbage collection (GC) automatically manages memory
in .NET by reclaiming unused objects. It prevents memory leaks
and ensures efficient memory usage, improving application
performance.
Example Program:
using System;
class GarbageCollectionExample
{
static void Main()
{
// Create an object
MyClass obj = new MyClass();
// Nullify reference to make it eligible for garbage
collection
obj = null;
// Force garbage collection
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Garbage Collection completed.");
}
}
class MyClass
{
~MyClass() // Destructor
{
Console.WriteLine("Destructor called, object destroyed.");
}
}
2. Multiple exceptions
In C#, you can handle multiple exceptions by using multiple
catch blocks within a try-catch statement. Each catch block is
designed to handle a specific exception type. This helps in
managing errors effectively by providing specific responses to
different types of exceptions.
Example:
using System;
class Program
{
static void Main()
{
try
{
int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[5]); //
IndexOutOfRangeException
Benefits:
Allows handling specific errors with tailored responses.
Improves program robustness by preventing crashes.
Ensures fallback mechanisms for unexpected exceptions.
using System;
using System.IO;
class Program {
Console.WriteLine(content);
} } }
Writing to Files:
using System;
using System.IO;
class Program {
} } }
using System;
using System.IO;
class Program {
if (File.Exists(filePath)) {
Console.WriteLine("File exists."); }
else {
} } }
To add data to an existing file without overwriting it, you can use the
StreamWriter with the append parameter set to true.
using System.IO;
class Program {
} } }
For reading and writing binary data (e.g., images or custom binary
files), the FileStream class is used in conjunction with
BinaryReader and BinaryWriter.
using System;
using System.IO;
class Program {
{
int data = reader.ReadInt32();
} }}
OUTPUT:
Enter the starting number (m):
2
Enter the ending number (n):
5
Enter the number to skip (k):
3
Numbers between 2 and 5, skipping 3:
245
12MARKS:
1. Framework architecture
Example:
using System;
using System.Windows.Forms;
public Program()
this.Controls.Add(label);
button.Text = "Submit";
this.Controls.Add(button);
MessageBox.Show($"Hello, {textBox.Text}!");
};
}
Advantages:
Provides a drag-and-drop interface for easy GUI design.
Disadvantages:
EXAMPLE:
using System;
// single inheritance
class Animal {
public void Eat() {
Console.WriteLine("Animal is eating.");
}
}
// multi-level inheritance
class Mammal : Animal {
public void Run() {
Console.WriteLine("Mammal is running.");
}
}
// hierarchical inheritance
class Bird : Animal {
public void Fly() {
Console.WriteLine("Bird is flying.");
}
}
// multiple inheritance
interface I1 {
void Method1();
}
interface I2 {
void Method2();
}
// multi-level inheritance
Horse horse = new Horse();
horse.Eat();
horse.Run();
horse.Gallop();
// hierarchical inheritance
Eagle eagle = new Eagle();
Penguin penguin = new Penguin();
eagle.Fly();
eagle.Hunt();
penguin.Fly();
penguin.Swim();
// multiple inheritance
MyClass myClass = new MyClass();
myClass.Method1();
myClass.Method2();
Console.ReadLine();
}
}
Advantages of Inheritance:
Code Reusability: Inheritance allows us to reuse
existing code by inheriting properties and methods
from an existing class.
Code Maintenance: Inheritance makes code
maintenance easier by allowing us to modify the base
class and have the changes automatically reflected in
the derived classes.
Code Organization: Inheritance improves code
organization by grouping related classes together in a
hierarchical structure.
Disadvantages of Inheritance:
Tight Coupling: Inheritance creates a tight coupling
between the base class and the derived class, which
can make the code more difficult to maintain.
Complexity: Inheritance can increase the complexity of
the code by introducing additional levels of abstraction.
Fragility: Inheritance can make the code more fragile by
creating dependencies between the base class and the
derived class.
Default Constructor in C#
A constructor without any parameters is called a default
constructor; in other words, this type of constructor does not
take parameters. The drawback of a default constructor is that
every instance of the class will be initialized to the same values
and it is not possible to initialize each instance of the class with
different values. The default constructor initializes:
All numeric fields in the class to zero.
All string and object fields to null.
Parameterized Constructor in C#
A constructor with at least one parameter is called a
parameterized constructor. The advantage of a parameterized
constructor is that you can initialize each instance of the class
with a different value.
Example:
using System;
namespace Constructor
{
class paraconstrctor
{
public int a, b;
public paraconstrctor(int x, int y) {
a = x;
b = y;
}
}
class MainClass
{
static void Main ()
{
paraconstrctor v = new paraconstrctor(100, 175);
Console.WriteLine("Parameterized constructor example by
vithal wadje");
Console.WriteLine("\t");
Console.WriteLine("value of a=" + v.a );
Console.WriteLine("value of b=" + v.b);
Console.Read();
}
}
Copy Constructor in C#
The constructor which creates an object by copying variables
from another object is called a copy constructor. The purpose of
a copy constructor is to initialize a new instance to the values of
an existing instance.
The copy constructor is invoked by instantiating an object of
type employee and bypassing it the object to be copied.
Example:
using System;
namespace copyConstractor
{
class employee
{
private string name;
private int age;
public employee (employee emp)
{
name = emp.name;
age = emp.age;
}
public employee (string name, int age)
{
this.name = name;
this.age = age;
}
public string Details // Get deatils of employee
{
get
{
return " The age of " + name +" is "+ age.ToString();
}
}
}
class empdetail
{
static void Main ()
{
employee emp1 = new employee ("Vithal", 23
employee emp2 = new employee(emp1);
Console.WriteLine(emp2.Details);
Console.ReadLine();
}
}
}