❔ Delegates
This code doesn't compile and I don't understand the issue.
What Am I doing wrong?
29 Replies
Use
Func<int, int, int>
instead
Delegate
is just the base type for all delegate types (like Func
), but it's not actually invokablethinker227#5176
REPL Result: Success
Console Output
Compile: 642.841ms | Execution: 43.745ms | React with ❌ to remove this embed.
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?
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 canMaybe for dynamic stuff, but yes I can see it not being very usefull
Also, you can actually just use
var
here and let the compiler infer the type of the delegate for you. 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
I think in C++ you have some really long and obscure method pointer syntax...?
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
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.I'll look into that :)
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.
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
Weird the syntax hightlight doesnt seem to be recognising c#$codegif
That did it, thanks
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 themIs 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?all of them have their corresponding regular types
So I can assume that everything comes from the System
I think so
Is anything bellow the System? Or is it the base of everything?
I think Java follows a similar dynamic
For objects atleast
here, the names don't always correspond to their primitive names tho
like float is Single, int is Int32, etc.
Ye
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...
The top comment also covers the question I asked
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.A namespace yes, my bad :)
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.