class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}
public static void Sample()
{
Console.WriteLine("SAmple ...");
}
public static void Main()
{
// Instantiation
SimpleDelegate sd = new SimpleDelegate(MyFunc);
sd += new SimpleDelegate(Sample);
// Invocation
sd();
//MyFunc();
}
}
//---------------------
using System;
// Delegate Specification
public class MyClass
{
// Declare a delegate that takes a single string parameter
// and has no return type.
public delegate void LogHandler(string message);
// The use of the delegate is just like calling a function
directly,
// though we need to add a check to see if the delegate is null
// (that is, not pointing to a function) before calling the
function.
public void ActivatePump(LogHandler pump)
{
if (pump != null)
{
pump("Pump begin");
}
}
}
// Test Application to use the defined Delegate
public class TestApplication
{
// Static Function: To which is used in the Delegate. To call
the Process()
// function, we need to declare a logging function: Logger()
that matches
// the signature of the delegate.
static void Pump1(string s)
{
Console.WriteLine(s);
}
static void Pump2(string s)
{
Console.WriteLine("Pump 2 : "+s);
}
static void Pump3(string s)
{
Console.WriteLine("Pump 3 :"+s);
}
static void Main(string[] args)
{
MyClass mc = new MyClass();
// Crate an instance of the delegate, pointing to the
logging function.
// This delegate will then be passed to the Process()
function.
MyClass.LogHandler ml = new MyClass.LogHandler(Pump1);
ml += new MyClass.LogHandler(Pump2);
ml += new MyClass.LogHandler(Pump3);
// ml("sliit");
mc.ActivatePump(ml);
}
}
//----------------------------
using System;
// Delegate Specification
public class MyClass
{
// any method that takes a string as the parameter and returns no
value
public delegate void LogHandler(string message);
// Define an Event based on the above Delegate
public event LogHandler Log;
// Instead of having the Process() function take a delegate
// as a parameter, we've declared a Log event. Call the Event,
// using the OnXXXX Method, where XXXX is the name of the
Event.
public void Process()
{
OnLog("Process() begin");
OnLog("Process() end");
}
// By Default, create an OnXXXX Method, to call the Event
protected void OnLog(string message)
{
if (Log != null)
{
Log(message);
}
}
}
// Test Application to use the defined Delegate
public class TestApplication
{
// Static Function: To which is used in the Delegate. To call
the Process()
// function, we need to declare a logging function: Logger()
that matches
// the signature of the delegate.
static void Logger(string s)
{
Console.WriteLine(s);
}
static void Logger2(string s)
{
Console.WriteLine(s + " logger 2");
}
static void Main(string[] args)
{
MyClass mc = new MyClass();
// Subscribe the Functions Logger and fl.Logger
mc.Log += new MyClass.LogHandler(Logger);
mc.Log += new MyClass.LogHandler(Logger2);
// The Event will now be triggered in the Process() Method
mc.Process();
}
}
//------------------------------------------
class test {
private int no;
public void setNo(int no) {
if (no < 100)
no = 100;
this.no = no;
}
public int getNo() {
return no;
}
}
class sample {
public static void Main() {
test t = new test();
t.setNo(200);
// t.no = t.no + 50
t.setNo(t.getNo()+50);
System.Console.WriteLine(t.getNo());
}
}
//--------------------------------
class test {
private int no;
public int No {
set {
if (value < 100)
value = 100;
this.no = value;
}
get {
return no;
}
}
}
class sample {
public static void Main() {
test t = new test();
t.No = 20;
//t.setNo(200);
t.No = t.No + 50;
//t.setNo(t.getNo()+50);
System.Console.WriteLine(t.No);
}
}
class ExamMarks {
private int marks1, marks2, marks3;
public ExamMarks(int m1, int m2, int m3) {
marks1 =m1;
marks2 = m2;
marks3 = m3;
}
public int this[int i] {
get {
if (i==1)
return marks1;
else if (i == 2)
return marks2;
else
return marks3;
}
}
}