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

Q1:

----
Create an interface IProduct having the following methods

string GetBrand()
string GetPrice()
string GetQuality()

Create another interface IOnlineShopping having the following methods

void AppName(string name)


void AddCart()
void Checkout()

Create a class with name Laptop implement both interfaces

Instance variable:
string brand
double price
string quality generate properties for these variables

implement all the methods from interfaces

write the logics for those methods

Create another class with name Book implement both interfaces

Instance variable:
string title
double price
string quality generate properties for these variables

implement all the methods from interfaces

write the logics for those methods

Create another class with name Test write main method in that and create object for
both Laptop and Book classes

print all the data of the products.

Excepted Output:
--------------------
Laptop Details:Brand: HP
Price: $1200.5
Quality: High
Online Shopping App Name: Flipcart
Item added to the cart.
Checkout completed.

Book Details:Title: The Catcher in the Rye


Price: $15.99
Quality: Medium
Online Shopping App Name: Amazon
Item added to the cart.
Checkout completed.
Q2:
----
Create an interface with name Ilocation having methods

string GetPickupLocation()
string GetDropLocation()

Create an interface with name ITaxiBooking having methods

void BookingApp(string name)


void ConfirmBooking()

Create a class Taxi implement both interfaces

Instance variables:
string pickupLocation
string dropLocation generate properties for the variables

implement all the methods from interfaces

write the logics for those methods

Create another class with name Test write main method in that and create object for
Taxi class

print all the data of the Booking.

Excepted output:
----------------
Taxi Booking Details:
Pickup Location: Airport
Drop Location: City Center
Taxi Booking App: Ola
Booking confirmed. Taxi en route.

Q3:
----
using System;

namespace DelegateBasics
{
// Step 1: Declare a delegate with the same signature as the method you want to
assign to it.
public delegate void MyDelegate();

class Program
{
static void Main(string[] args)
{
// Step 2: Create an instance of the delegate and assign a method to
it.
MyDelegate myDelegate = new MyDelegate(SayHello);

// Step 3: Invoke the delegate to call the assigned method.


myDelegate();
Console.ReadKey();
}

// Method that matches the delegate signature


static void SayHello()
{
Console.WriteLine("Hello from the delegate!");
}
}
}

Q4:
-----

using System;

namespace MulticastDelegates
{
// Step 1: Declare a delegate with the same signature as the methods you want
to assign.
public delegate void MultiDelegate();

class Program
{
static void Main(string[] args)
{
// Step 2: Create instances of the delegate and assign multiple methods
to it.
MultiDelegate multiDelegate = Method1;
multiDelegate += Method2;
multiDelegate += Method3;

// Step 3: Invoke the multicast delegate to call all assigned methods.


Console.WriteLine("Calling multicast delegate:");
multiDelegate();

Console.ReadKey();
}

static void Method1()


{
Console.WriteLine("Method 1");
}

static void Method2()


{
Console.WriteLine("Method 2");
}

static void Method3()


{
Console.WriteLine("Method 3");
}
}
}
Q5:
-----

using System;

namespace DelegateOperatorsExample
{
public delegate void MyDelegate();

class Program
{
static void Main(string[] args)
{
MyDelegate myDelegate = null; // Initialize the delegate to null

// Adding methods to the delegate using +=


myDelegate += Method1;
myDelegate += Method2;

Console.WriteLine("Calling delegate with both methods:");


myDelegate(); // Calls both Method1 and Method2

// Removing a method from the delegate using -=


myDelegate -= Method1;

Console.WriteLine("\nCalling delegate after removing Method1:");


myDelegate(); // Calls only Method2

Console.ReadKey();
}

static void Method1()


{
Console.WriteLine("Method 1");
}

static void Method2()


{
Console.WriteLine("Method 2");
}
}
}

You might also like