Dom,
Public is a way of saying that anyone can access the delegate. If you
feel that no one else is going to be able to use the delegate, then by all
means, make it private (as long as you can access it properly from the class
you want to use it in).
k and s in the parameter list are a little superflouous. Technically,
you don't need a parameter name, but for the sake of consistency, it makes
sense. Also, things like intellisense would need to show something when you
try and invoke it. Remember that delegates can have more than one method
associated with them, so you can't just take the parameter names of the
underlying method.
It's absolutely more type-safe. In C, you could set a function pointer
to anything in memory, and when you tried to execute it, the program
crashed. In C#, if the signature does not match, then your program doesn't
compile.
The Form class and the Button class do not have a Left property. If you
declared them on each of those types separately, then they would be
different from Invoke on the Button and the Form class. The reason you see
them on both is because the Button class and the Form class have a common
base type which has the Invoke method declared on it.
Also, you should note that the Invoke method on the Form/Button class is
different from the Invoke method on a delegate. The Invoke method on a
Form/Button instance allows you to pass a delegate to it so that the
delegate is invoked on the thread that created the control.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Dom" <do********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
I'm teaching myself about delegates and the Invoke method, and I have
a few newbie questions for the gurus out there:
Here are some CSharp statements:
1. public delegate void MyDelegate (int k, string s)
2. MyDelegate MyDelegateVar = MyMethod
3. MyForm.Invoke (MyDelegateVar)
Some questions:
1. What is the meaning of "public" in the first statement? The help
examples always use that. Isn't "private" more appropriate?
2. What is the purpose of "k" and "s" in the parameter list?
3. Isn't this a needless complication over C's pointer to a function,
which did in one statement what is done in two statements above? It
doesn't seem to be any more type-safe than what we had in C.
3. MyForm.Left has a different meaning that MyButton.Left, but what
is the difference between MyForm.Invoke, and MyButton.Invoke? For
that matter, why do we not just have System.Invoke, or something like
that?
Thanks,
Dom