Friday, January 20, 2012

Anonymous Functions, Lambda Expression


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace ConsoleApplication.LE_Delegates_An_Mthods
{
public class LambdaExpressions
{
delegate void DelWithoutReturnType(string message);
delegate string DelWithReturnType(string message, int i);

public void Sample1()
{
//Using Anonymous method
DelWithoutReturnType an = delegate(string msg) { Console.WriteLine(msg); };

//Using Method Group
DelWithoutReturnType mg = Console.WriteLine;

//Using Lambda Expression
DelWithoutReturnType le = (msg) => Console.WriteLine(msg);

an("Anonymous");
mg("Method Grp");
le("LE");
}

public void Sample2()
{
//Using Anonymous method
DelWithReturnType an = delegate(string msg, int cnt)
{
Console.WriteLine(msg);
Console.WriteLine();
return msg + cnt;
};

//Using Lambda Expression
DelWithReturnType le = (msg, cnt) =>
{
Console.WriteLine(msg);
return (msg + cnt);
};

an("Something needs to be done", 3);
le("I have changed the above delegate to Lambda Expression", 3);
}


}
}

Tuesday, January 10, 2012

Playing with Inheritance...

public interface IBase
{
void baseDisplay();
}

public interface I1 : IBase
{
void display();
}

public interface I2 : IBase
{
void display();
}


public class BaseB
{
public BaseB()
{
this.display();
}

public virtual void display()
{
Console.WriteLine("Base");
}
}

public class DervB : BaseB, I1, I2
{
public DervB()
{
base.display();
this.display();
display();
display(5);
}
public override void display()
{
base.display();
Console.WriteLine("Derived");
}

void I1.display()
{
Console.WriteLine("Interface I1");
}

void I2.display()
{
Console.WriteLine("Interface I2");
}

public void display(int i)
{
Console.WriteLine("Seperate Method");
}

void IBase.baseDisplay()
{
Console.WriteLine("Base interface");
}
}

-------------------------------------------------------------------------------------------------

static void Main(string[] args)
{
BaseB b = new BaseB();
b.display();
Console.WriteLine("1111------------------------------------");

BaseB b2 = new DervB();
b2.display();
Console.WriteLine("2222------------------------------------");

DervB der = new DervB();
der.display();
der.display(2);
Console.WriteLine("33------------------------------------");

I1 i = new DervB();
i.display();
Console.WriteLine("------------------------------------");


I2 i2 = new DervB();
i2.display();
Console.WriteLine("------------------------------------");

I1 i3 = new DervB();
i3.baseDisplay();
Console.WriteLine("------------------------------------");


Console.ReadLine();
}