✅ How to do the C# equivalent of passing an std::function into a class

Hi, so I'm trying to make a class function that can accept a function of type float(float). I don't really understand how C# works because I'm coming from C++, which is very different than C# besides the names of data types. I've heard of delegates, but in all instances of a delegate parameter I've seen it as a pointer. Is a pointer bad C# syntax though in this case, since I'm trying to learn how to write C# well versus writing C# like C++. thanks in advance
12 Replies
Angius
Angius10mo ago
Action or Func
public class Foo(Func<int, string> func)
{
public void Print(int num)
{
Console.WriteLine(func(num));
}
}

var f = new Foo(num => $"Number is {num}");
f.Print(69) // Number is 69
public class Foo(Func<int, string> func)
{
public void Print(int num)
{
Console.WriteLine(func(num));
}
}

var f = new Foo(num => $"Number is {num}");
f.Print(69) // Number is 69
UvuvwevweOnyetenye
UvuvwevweOnyetenyeOP10mo ago
how is that not on the first page of bing 😭 thanks whats the point of delegate then at that point
Angius
Angius10mo ago
With a delegate you can type those funcs and actions In the case above, you don't get any description of what that int is, for example But you can define
public delegate string SomeFunc(int numberOfLegs);
public delegate string SomeFunc(int numberOfLegs);
and use that
public class Foo(SomeFunc func)
{
public void Print(int num)
{
Console.WriteLine(func(num));
}
}

var f = new Foo(num => $"Number is {num}");
f.Print(69) // Number is 69
public class Foo(SomeFunc func)
{
public void Print(int num)
{
Console.WriteLine(func(num));
}
}

var f = new Foo(num => $"Number is {num}");
f.Print(69) // Number is 69
UvuvwevweOnyetenye
UvuvwevweOnyetenyeOP10mo ago
so the Func could come out as an int with a string parameter instead? doesnt matter for my current setup cuz of my data types but
Angius
Angius10mo ago
What do you mean? Yes, you can make the params and return type anything If that's what you're asking
UvuvwevweOnyetenye
UvuvwevweOnyetenyeOP10mo ago
public class Foo(Func<int, string> func)
{
public void Print(string arg)
{
int x = func(arg);
}
}
public class Foo(Func<int, string> func)
{
public void Print(string arg)
{
int x = func(arg);
}
}
is that what you mean by not knowing what the int is?
Angius
Angius10mo ago
No, you defined the func as something that takes an int and returns a string Not the other way around And what I mean is that the int has no name here It's just that, an int You don't know if it's age, weight, or number of eggs in a basket An explicit delegate declaration lets you name all the params as you see fit
UvuvwevweOnyetenye
UvuvwevweOnyetenyeOP10mo ago
oh ok
Angius
Angius10mo ago
Lets you even write doc comments for it So you get more help from the IDE
UvuvwevweOnyetenye
UvuvwevweOnyetenyeOP10mo ago
dubs i get it now thanks
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX10mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server