(retarded) newbie: unable to understand delegate w/ java background

liunx

Guest
Howdy, trying to dig into C# .net, everything seems going pretty smooth except "delegate". I went to many diff pages and everything when I thought I understand, my programming/compiler proved I don't :P

here's one of my biggest confuse:


public class DelegateTest
{
public delegate void Print (String s);
public static void Main()
{
Print s = new Print (toConsole);
Display (s);
}

public static void toConsole (String str)
{
Console.WriteLine(str);
}

public static void Display(Print pMethod)
{
pMethod("This should be displayed in the console");
}
}


confusion (1) Print s = new Print (toConsole);
what the hell, i thought to invoke toConsole you need to pass in the parameter (in this case str) how come this "delegate" is not doing it?

confusion (2) since i have a method sitting outside of the main class: Display
why not do something like this (easier?):


public class DelegateTest2
{
public static void Main()
{
Display("This should be displayed in the console")
}
public static void Display(string str)
{
Console.WriteLine(str);
}
}


maybe i'm too stupid to understand this concept, but i just failed to see the value of using delegate...

please help enlight me
TIA
 
Back
Top