CODE
using System;
delegate void MyDelegate();
class MulticastDelegate
{
static void Method1()
{
Console.WriteLine("Method 1 Called");
}
static void Method2()
{
Console.WriteLine("Method 2 Called");
}
static void Main()
{
MyDelegate d = Method1;
d += Method2;
d();
}
}
OUTPUT
Method 1 Called
Method 2 Called
CODE
using System;
class Product
{
public event EventHandler PriceChanged;
private int price;
public int Price
{
get { return price; }
set
{
price = value;
if (PriceChanged != null) PriceChanged(this, EventArgs.Empty);
}
}
}
class EventDemo
{
static void Main()
{
Product p = new Product();
p.PriceChanged += (sender, e) => Console.WriteLine("Price Changed Event Triggered");
p.Price = 100;
p.Price = 200;
}
}
OUTPUT
Price Changed Event Triggered
Price Changed Event Triggered
CODE
using System;
class Grandparent
{
public void Display1() { Console.WriteLine("Grandparent"); }
}
class Parent : Grandparent
{
public void Display2() { Console.WriteLine("Parent"); }
}
class Child : Parent
{
public void Display3() { Console.WriteLine("Child"); }
}
class MultiLevelInheritance
{
static void Main()
{
Child c = new Child();
c.Display1();
c.Display2();
c.Display3();
}
}
OUTPUT
Grandparent
Parent
Child
CODE
using System;
class Vehicle
{
public virtual void Run() { Console.WriteLine("Vehicle runs"); }
}
class Car : Vehicle
{
public override void Run() { Console.WriteLine("Car runs"); }
}
class Bike : Vehicle
{
public override void Run() { Console.WriteLine("Bike runs"); }
}
class PolymorphismDemo
{
static void Main()
{
Vehicle v1 = new Car();
Vehicle v2 = new Bike();
v1.Run();
v2.Run();
}
}
OUTPUT
Car runs
Bike runs
CODE
using System;
using System.Collections.Generic;
class ListDemo
{
static void Main()
{
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(5);
numbers.Add(20);
numbers.Add(8);
Console.Write("Original: ");
foreach(int n in numbers) Console.Write(n + " ");
Console.WriteLine();
numbers.Remove(5);
numbers.Sort();
Console.Write("Sorted & Removed: ");
foreach(int n in numbers) Console.Write(n + " ");
Console.WriteLine();
bool exists = numbers.Contains(20);
Console.WriteLine("Contains 20: " + exists);
}
}
OUTPUT
Original: 10 5 20 8
Sorted & Removed: 8 10 20
Contains 20: True
No comments:
Post a Comment