C
C#2y 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
Thinker2y 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
MODiX2y 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úlioOP2y 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
Thinker2y 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úlioOP2y ago
Maybe for dynamic stuff, but yes I can see it not being very usefull
Thinker
Thinker2y 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úlioOP2y 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
Thinker2y ago
I think in C++ you have some really long and obscure method pointer syntax...?
júlio
júlioOP2y 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
Thinker2y 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úlioOP2y ago
I'll look into that :)
júlio
júlioOP2y 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úlioOP2y 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
Anton2y ago
$codegif
júlio
júlioOP2y ago
That did it, thanks
Anton
Anton2y 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úlioOP2y 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
Anton2y ago
all of them have their corresponding regular types
júlio
júlioOP2y ago
So I can assume that everything comes from the System
Anton
Anton2y ago
I think so
júlio
júlioOP2y ago
Is anything bellow the System? Or is it the base of everything? I think Java follows a similar dynamic For objects atleast
Anton
Anton2y ago
here, the names don't always correspond to their primitive names tho like float is Single, int is Int32, etc.
júlio
júlioOP2y ago
Ye
júlio
júlioOP2y 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úlioOP2y ago
The top comment also covers the question I asked
Thinker
Thinker2y 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úlioOP2y ago
A namespace yes, my bad :)
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server