Here i am providing a program to implement events and delegates in c#. In this program i have created an event as "Raisealarm" and delegate as "Alarm". Then the event is called in main() function after passing reference of event handler "fn" to delegate "Alarm".
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
class Class1
{
public delegate void Alarm();
public event Alarm RaiseAlarm;
public static void fn()
{
Console.WriteLine("Alarm Raised");
}
public static void Main()
{
int time = 0;
Class1 c = new Class1();
Console.WriteLine("Enter time");
time = Convert.ToInt32(Console.ReadLine());
c.RaiseAlarm += new Alarm(fn);
if (time == 6)
{
c.RaiseAlarm();
}
}
}
}
Tuesday, August 10, 2010
Implementation of Delegates in c#
Here i am providing a program to implement coffee vending machine using delegates in c#.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public delegate void vending();
public void hotwater()
{
Console.WriteLine("Please take your hot water.");
}
public void blackcoffe()
{
Console.WriteLine("Please take your balck coffee.");
}
public void cappucino()
{
Console.WriteLine("Please take your cappucino.");
}
static void Main(string[] args)
{
int ch;
char ans;
Program p = new Program();
do
{
Console.WriteLine("Coffee vending machine");
Console.Write("1.Hot Water\n2.Black Coffee\n3.Cappucino\n4.Stop\n\nEnter your choice: ");
ch = Convert.ToInt32(Console.ReadLine());
switch (ch)
{
case 1: vending v = new vending(p.hotwater);
v();
break;
case 2: vending v1 = new vending(p.blackcoffe);
v1();
break;
case 3: vending v2 = new vending(p.cappucino);
v2();
break;
default: break;
}
Console.Write("\nDo u want to continue?(y/n): ");
ans = Convert.ToChar(Console.ReadLine());
} while (ans == 'Y' || ans == 'y');
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public delegate void vending();
public void hotwater()
{
Console.WriteLine("Please take your hot water.");
}
public void blackcoffe()
{
Console.WriteLine("Please take your balck coffee.");
}
public void cappucino()
{
Console.WriteLine("Please take your cappucino.");
}
static void Main(string[] args)
{
int ch;
char ans;
Program p = new Program();
do
{
Console.WriteLine("Coffee vending machine");
Console.Write("1.Hot Water\n2.Black Coffee\n3.Cappucino\n4.Stop\n\nEnter your choice: ");
ch = Convert.ToInt32(Console.ReadLine());
switch (ch)
{
case 1: vending v = new vending(p.hotwater);
v();
break;
case 2: vending v1 = new vending(p.blackcoffe);
v1();
break;
case 3: vending v2 = new vending(p.cappucino);
v2();
break;
default: break;
}
Console.Write("\nDo u want to continue?(y/n): ");
ans = Convert.ToChar(Console.ReadLine());
} while (ans == 'Y' || ans == 'y');
}
}
}
Monday, August 9, 2010
Implementing threads in c#
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace threads
{
class Program
{
public static void pendrive()
{
for (int i = 0; i <= 5; i++)
{
Console.WriteLine("Port is assigned to pendrive.");
Thread.Sleep(4000);
}
}
public static void printer()
{
for (int i = 0; i <= 4; i++)
{
Console.WriteLine("Port is assigned to printer.");
Thread.Sleep(2000);
}
}
public static void harddisk()
{
for (int i = 0; i <= 3; i++)
{
Console.WriteLine("Port is assigned to harddisk.");
Thread.Sleep(3000);
}
}
static void Main(string[] args)
{
ThreadStart ts1 = new ThreadStart(pendrive);
Thread t1 = new Thread(ts1);
ThreadStart ts2 = new ThreadStart(printer);
Thread t2 = new Thread(ts2);
ThreadStart ts3 = new ThreadStart(harddisk);
Thread t3 = new Thread(ts3);
t1.Start();
t2.Start();
t3.Start();
Console.WriteLine("main ended.");
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace threads
{
class Program
{
public static void pendrive()
{
for (int i = 0; i <= 5; i++)
{
Console.WriteLine("Port is assigned to pendrive.");
Thread.Sleep(4000);
}
}
public static void printer()
{
for (int i = 0; i <= 4; i++)
{
Console.WriteLine("Port is assigned to printer.");
Thread.Sleep(2000);
}
}
public static void harddisk()
{
for (int i = 0; i <= 3; i++)
{
Console.WriteLine("Port is assigned to harddisk.");
Thread.Sleep(3000);
}
}
static void Main(string[] args)
{
ThreadStart ts1 = new ThreadStart(pendrive);
Thread t1 = new Thread(ts1);
ThreadStart ts2 = new ThreadStart(printer);
Thread t2 = new Thread(ts2);
ThreadStart ts3 = new ThreadStart(harddisk);
Thread t3 = new Thread(ts3);
t1.Start();
t2.Start();
t3.Start();
Console.WriteLine("main ended.");
}
}
}
Program to implement exception handling in c#
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
public class myexception : ApplicationException
{
public myexception(string message) : base(message) { }
}
class Program
{
static void Main(string[] args)
{
char ch;
ticket t = new ticket();
do
{
try
{
t.check();
}
catch (myexception me)
{
Console.WriteLine("Negative value Exception: {0}", me.Message);
}
Console.WriteLine("Do u wnt to book more tickets?(y/n): ");
ch = Convert.ToChar(Console.ReadLine());
} while (ch == 'y' || ch == 'Y');
}
}
class ticket
{
int tic = 10,a;
public void check()
{
Console.Write("enter no. of tickets u want to book: ");
a = Convert.ToInt32(Console.ReadLine());
tic = tic - a;
if (tic < 1)
{
throw (new myexception("Sorry! You can book upto 10 tickets in a day."));
}
else
{
Console.WriteLine("Remaining tickets:{0}", tic);
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
public class myexception : ApplicationException
{
public myexception(string message) : base(message) { }
}
class Program
{
static void Main(string[] args)
{
char ch;
ticket t = new ticket();
do
{
try
{
t.check();
}
catch (myexception me)
{
Console.WriteLine("Negative value Exception: {0}", me.Message);
}
Console.WriteLine("Do u wnt to book more tickets?(y/n): ");
ch = Convert.ToChar(Console.ReadLine());
} while (ch == 'y' || ch == 'Y');
}
}
class ticket
{
int tic = 10,a;
public void check()
{
Console.Write("enter no. of tickets u want to book: ");
a = Convert.ToInt32(Console.ReadLine());
tic = tic - a;
if (tic < 1)
{
throw (new myexception("Sorry! You can book upto 10 tickets in a day."));
}
else
{
Console.WriteLine("Remaining tickets:{0}", tic);
}
}
}
}
Program to implement interfaces in c#
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication33
{
interface g
{
void add();
}
interface demo :g
{
void sub();
}
class P :demo
{
public void add()
{
Console.WriteLine ("hello");
}
public void sub()
{
Console.WriteLine("sub");
}
}
class d:g
{
public void add()
{
Console.WriteLine("niit");
}
}
class c
{
static void Main(string[] args)
{
P p1 = new P();
p1.add();
d d1 = new d();
d1.add();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication33
{
interface g
{
void add();
}
interface demo :g
{
void sub();
}
class P :demo
{
public void add()
{
Console.WriteLine ("hello");
}
public void sub()
{
Console.WriteLine("sub");
}
}
class d:g
{
public void add()
{
Console.WriteLine("niit");
}
}
class c
{
static void Main(string[] args)
{
P p1 = new P();
p1.add();
d d1 = new d();
d1.add();
}
}
}
Run time polymorphism using c#
Program to implement Run-time polymorphism using virtual function concept
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication32
{
class a
{
public int s, r;
public virtual void add()
{
s = 23;
r = 56;
Console.WriteLine(s);
Console.WriteLine(r );
Console.WriteLine("base class a");
}
}
class b:a
{
public override void add()
{
s = 389;
r = 7648;
Console.WriteLine(s);
Console.WriteLine(r);
Console.WriteLine("derived class b");
}
}
class c:a
{
static void Main(string[] args)
{
b b1 = new b();
b1.add();
c c1 = new c();
a a1 = new a();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication32
{
class a
{
public int s, r;
public virtual void add()
{
s = 23;
r = 56;
Console.WriteLine(s);
Console.WriteLine(r );
Console.WriteLine("base class a");
}
}
class b:a
{
public override void add()
{
s = 389;
r = 7648;
Console.WriteLine(s);
Console.WriteLine(r);
Console.WriteLine("derived class b");
}
}
class c:a
{
static void Main(string[] args)
{
b b1 = new b();
b1.add();
c c1 = new c();
a a1 = new a();
}
}
}
Abstract class using c#
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication31
{
abstract class a
{
public abstract void add();
public void sub()
{
Console.WriteLine("base class a");
}
}
class b:a
{
public override void add()
{
Console.WriteLine("derived class b");
}
}
class c
{
static void Main(string[] args)
{
b b1 = new b();
b1.add();
b1.sub ();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication31
{
abstract class a
{
public abstract void add();
public void sub()
{
Console.WriteLine("base class a");
}
}
class b:a
{
public override void add()
{
Console.WriteLine("derived class b");
}
}
class c
{
static void Main(string[] args)
{
b b1 = new b();
b1.add();
b1.sub ();
}
}
}
Sunday, August 8, 2010
Parameterized constructors in c#
Program to implement parameterized constructors in c#
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication30
{
class a
{
public a()
{
Console.WriteLine("constructor--base class a");
}
public a(int g, int h)
{
Console.WriteLine("Parametrised constr...base class a");
s = g;
d = h;
}
~a()
{
Console.WriteLine("destructor--base class a");
}
public int s, d;
public void display()
{
Console.WriteLine(s);
Console.WriteLine(d );
}
}
class b : a
{
int r = 56;
int s = 23;
public b() :base(45,78)
{
Console.WriteLine("constructor--derived class b");
}
public b(int g, int h) : base (12,6)
{
Console.WriteLine("Parametrised constr...base class a");
s = g;
d = h;
}
~b()
{
Console.WriteLine("destructor--derived class b");
}
}
}
class c:a
{
static void Main(string[] args)
{
b b1 = new b();
b b2 = new b(56, 89);
b1.d = 34;
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication30
{
class a
{
public a()
{
Console.WriteLine("constructor--base class a");
}
public a(int g, int h)
{
Console.WriteLine("Parametrised constr...base class a");
s = g;
d = h;
}
~a()
{
Console.WriteLine("destructor--base class a");
}
public int s, d;
public void display()
{
Console.WriteLine(s);
Console.WriteLine(d );
}
}
class b : a
{
int r = 56;
int s = 23;
public b() :base(45,78)
{
Console.WriteLine("constructor--derived class b");
}
public b(int g, int h) : base (12,6)
{
Console.WriteLine("Parametrised constr...base class a");
s = g;
d = h;
}
~b()
{
Console.WriteLine("destructor--derived class b");
}
}
}
class c:a
{
static void Main(string[] args)
{
b b1 = new b();
b b2 = new b(56, 89);
b1.d = 34;
}
}
constructors and destructors in c#
Program to implement constructors and destructors in c#
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication30
{
class a
{
public int s, d;
public a()
{
Console.WriteLine("constructor--base class a");
}
~a()
{
Console.WriteLine("destructor--base class a");
}
public void display()
{
Console.WriteLine(s);
Console.WriteLine(d );
}
}
class b : a
{
public b()
{
Console.WriteLine("constructor--derived class b");
}
~b()
{
Console.WriteLine("destructor--derived class b");
}
}
}
class c:a
{
static void Main(string[] args)
{
b b1 = new b();
b1.d = 34;
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication30
{
class a
{
public int s, d;
public a()
{
Console.WriteLine("constructor--base class a");
}
~a()
{
Console.WriteLine("destructor--base class a");
}
public void display()
{
Console.WriteLine(s);
Console.WriteLine(d );
}
}
class b : a
{
public b()
{
Console.WriteLine("constructor--derived class b");
}
~b()
{
Console.WriteLine("destructor--derived class b");
}
}
}
class c:a
{
static void Main(string[] args)
{
b b1 = new b();
b1.d = 34;
}
}
Program of File Handling in c#
Here is a program to write text file, read text file, write binary file, read binary file and show all the files of a directory.
//Program to implement file handling in c#
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class myfile
{
string bt;
string s;
string r;
string cname;
public void writefile()
{
FileStream fs = new FileStream(@"e:\surbhi\myfile.txt", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
Console.WriteLine("Enter data: ");
s = Console.ReadLine();
sw.Write(s);
sw.Close();
fs.Close();
}
public void readfile()
{
FileStream fs1 = new FileStream("e:\\surbhi\\myfile.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs1);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
r = sr.ReadLine();
while (r != null)
{
Console.WriteLine(r);
r = sr.ReadLine();
}
}
public void binwrite()
{
FileStream fs2 = new FileStream("e:\\surbhi\\mybin.bin", FileMode.Create, FileAccess.Write);
BinaryWriter bin = new BinaryWriter(fs2);
Console.WriteLine("Enter data: ");
cname = Console.ReadLine();
bin.Write(cname);
bin.Close();
fs2.Close();
}
public void binread()
{
FileStream fs3 = new FileStream("e:\\surbhi\\mybin.bin", FileMode.Open, FileAccess.Read);
BinaryReader binr = new BinaryReader(fs3);
binr.BaseStream.Seek(0, SeekOrigin.Begin);
bt = binr.ReadString();
Console.WriteLine(bt);
}
public void mydir()
{
DirectoryInfo d = new DirectoryInfo("e:\\surbhi");
FileInfo[] f = d.GetFiles();
foreach (FileInfo fi in f)
{
Console.WriteLine(fi);
}
}
}
class Program
{
static void Main(string[] args)
{
int ch;
char ans;
myfile mf = new myfile();
do
{
Console.WriteLine("File operations");
Console.Write("1.writefile\n2.readfile\n3.write binary\n4.read binary\n5.read files of a directory\n6.exit\nEnter your choice: ");
ch = Convert.ToInt32(Console.ReadLine());
switch (ch)
{
case 1: mf.writefile();
break;
case 2: mf.readfile();
break;
case 3: mf.binwrite();
break;
case 4: mf.binread();
break;
case 5: mf.mydir();
break;
default: break;
}
Console.Write("Do you wish to continue?(y/n): ");
ans = Convert.ToChar(Console.ReadLine());
} while (ans == 'y' || ans == 'Y');
}
}
}
//Program to implement file handling in c#
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class myfile
{
string bt;
string s;
string r;
string cname;
public void writefile()
{
FileStream fs = new FileStream(@"e:\surbhi\myfile.txt", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
Console.WriteLine("Enter data: ");
s = Console.ReadLine();
sw.Write(s);
sw.Close();
fs.Close();
}
public void readfile()
{
FileStream fs1 = new FileStream("e:\\surbhi\\myfile.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs1);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
r = sr.ReadLine();
while (r != null)
{
Console.WriteLine(r);
r = sr.ReadLine();
}
}
public void binwrite()
{
FileStream fs2 = new FileStream("e:\\surbhi\\mybin.bin", FileMode.Create, FileAccess.Write);
BinaryWriter bin = new BinaryWriter(fs2);
Console.WriteLine("Enter data: ");
cname = Console.ReadLine();
bin.Write(cname);
bin.Close();
fs2.Close();
}
public void binread()
{
FileStream fs3 = new FileStream("e:\\surbhi\\mybin.bin", FileMode.Open, FileAccess.Read);
BinaryReader binr = new BinaryReader(fs3);
binr.BaseStream.Seek(0, SeekOrigin.Begin);
bt = binr.ReadString();
Console.WriteLine(bt);
}
public void mydir()
{
DirectoryInfo d = new DirectoryInfo("e:\\surbhi");
FileInfo[] f = d.GetFiles();
foreach (FileInfo fi in f)
{
Console.WriteLine(fi);
}
}
}
class Program
{
static void Main(string[] args)
{
int ch;
char ans;
myfile mf = new myfile();
do
{
Console.WriteLine("File operations");
Console.Write("1.writefile\n2.readfile\n3.write binary\n4.read binary\n5.read files of a directory\n6.exit\nEnter your choice: ");
ch = Convert.ToInt32(Console.ReadLine());
switch (ch)
{
case 1: mf.writefile();
break;
case 2: mf.readfile();
break;
case 3: mf.binwrite();
break;
case 4: mf.binread();
break;
case 5: mf.mydir();
break;
default: break;
}
Console.Write("Do you wish to continue?(y/n): ");
ans = Convert.ToChar(Console.ReadLine());
} while (ans == 'y' || ans == 'Y');
}
}
}
Subscribe to:
Comments (Atom)