C
C#16mo ago
júlio

❔ Delegates

c#
namespace MWE
{
internal class Program
{
static int add(int a, int b) {
return a + b;
}
static void Main(string[] args)
{
Delegate esum = add;
Console.WriteLine(esum(4, 6));
}
}
}
c#
namespace MWE
{
internal class Program
{
static int add(int a, int b) {
return a + b;
}
static void Main(string[] args)
{
Delegate esum = add;
Console.WriteLine(esum(4, 6));
}
}
}
This code doesn't compile and I don't understand the issue. What Am I doing wrong?
29 Replies
Thinker
Thinker16mo ago
Use Func<int, int, int> instead Delegate is just the base type for all delegate types (like Func), but it's not actually invokable
MODiX
MODiX16mo ago
thinker227#5176
REPL Result: Success
static int Add(int a, int b)
{
return a + b;
}

Func<int, int, int> esum = Add;
Console.WriteLine(esum(4, 6));
static int Add(int a, int b)
{
return a + b;
}

Func<int, int, int> esum = Add;
Console.WriteLine(esum(4, 6));
Console Output
10
10
Compile: 642.841ms | Execution: 43.745ms | React with ❌ to remove this embed.
júlio
júlio16mo ago
So I should use it like I would a pointer to base class? For polymorhpism stuff For example, take in a Delegate as parameters and cast to the actual function type?
Thinker
Thinker16mo ago
Delegate on its own is generally pretty useless I don't think I've ever seen a situation where you'd have to use Delegate as a base class, even though you can
júlio
júlio16mo ago
Maybe for dynamic stuff, but yes I can see it not being very usefull
Thinker
Thinker16mo ago
Also, you can actually just use var here and let the compiler infer the type of the delegate for you.
var esum = Add;
Console.WriteLine(esum(4, 6));
var esum = Add;
Console.WriteLine(esum(4, 6));
júlio
júlio16mo ago
Yes. I come from C++ and now I have to learn C# for work realated things, so I'm kinda trying out aspects of the language I appreciate the explanation
Thinker
Thinker16mo ago
I think in C++ you have some really long and obscure method pointer syntax...?
júlio
júlio16mo ago
there are some wrappers around function pointers std;:function is a common one, tho it has a bit of overhead you still have to tell it the type of the function ther isnt really a way to have a Delegate like c# since the functions don't come from a common object
Thinker
Thinker16mo ago
You can actually use raw method pointers in C#, but for 99% of cases you should use Func and Action, and for the other 0.9% you can create your own delegate types.
júlio
júlio16mo ago
I'll look into that :)
júlio
júlio16mo ago
Using Delegates - C# Programming Guide
Learn how to use delegates. Delegates are an object-oriented, type safe, and secure type that safely encapsulates a method.
júlio
júlio16mo ago
One thing I didnt quite understand I though that in order to be able to use a function as a delegate, I would have to mark it like so public delegate void Del(string message); But the compiler allows me to not do that declaration "The following example declares a delegate named Del that can encapsulate a method that takes a string as an argument and returns void" Okay I think I get it That is the syntax for a function pointer I though that it was related to the function itself I wanted to point to my bad So this means I have 2 different ways of doing thins
public delegate int Calculation(int a, int b);
static int add(int a, int b) {
return a + b;
}

static int subtract(int a, int b) {
return a - b;
}
static void Main(string[] args)
{
Calculation c = add; // #1
Func<int, int, int> esum = add; // #2
Console.WriteLine(esum(4, 6));
}
public delegate int Calculation(int a, int b);
static int add(int a, int b) {
return a + b;
}

static int subtract(int a, int b) {
return a - b;
}
static void Main(string[] args)
{
Calculation c = add; // #1
Func<int, int, int> esum = add; // #2
Console.WriteLine(esum(4, 6));
}
Weird the syntax hightlight doesnt seem to be recognising c#
Anton
Anton16mo ago
$codegif
júlio
júlio16mo ago
That did it, thanks
Anton
Anton16mo ago
Func is declared the same way you declared Calculation I think, it just happens to also allow a few generic parameters, which you could allow too, in principle delegate types are a useful abstraction sometimea don't hesitate to use them
júlio
júlio16mo ago
Is it common for primitive types to be alias of the System.(...) equivalent? For example, double and System.Double, double here would be an alias to System.Double, correct?
Anton
Anton16mo ago
all of them have their corresponding regular types
júlio
júlio16mo ago
So I can assume that everything comes from the System
Anton
Anton16mo ago
I think so
júlio
júlio16mo ago
Is anything bellow the System? Or is it the base of everything? I think Java follows a similar dynamic For objects atleast
Anton
Anton16mo ago
here, the names don't always correspond to their primitive names tho like float is Single, int is Int32, etc.
júlio
júlio16mo ago
Ye
júlio
júlio16mo ago
Stack Overflow
Is everything in .NET an object?
Please help us settle the controversy of "Nearly" everything is an object (an answer to Stack Overflow question As a novice, is there anything I should beware of before learning C#?). I thought tha...
júlio
júlio16mo ago
The top comment also covers the question I asked
Thinker
Thinker16mo ago
System in C# is just a namespace, and namespaces don't really mean anything other than as a way to organize code. If you wanna be pedantic, there is technically the global:: namespace, but that's only used for very niche scenarios.
júlio
júlio16mo ago
A namespace yes, my bad :)
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.