P
PerezGuillermo8
Guest
Hello, I'm newbie to c#, and I was reading on my book that I can pass functions as parameters and receive functions or both... so I'm trying to make an exercise but can't make this work... please help!
My idea is to create a static function like this:
C#:
public static void TestMethod<T1, TResult>(Func<T1, TResult> myfunction ) {
...
}
Now, the idea is that I can call this static method by providing a function that matches the T1 and the TResult like this:
C#:
Func<string, string> ChangeName = name => $"My name is: {name}";
TestMethod<string, string>(ChangeName);
Now my understanding is that inside the TestMethod I received a function, now I can make use of the myfunction and call it to action, am I right? in this case, if I call the function it should return an string (according to my definition), so I think I should be able to do this:
C#:
public static void TestMethod<T1, TResult>(Func<T1, TResult> myfunction ) {
Console.WriteLine( myfunction.Invoke() );
// or
Console.WriteLine( myfunction() );
}
But I can't because it display an error asking for a T1 parameter...
PLEASE HELP!...
Continue reading...
My idea is to create a static function like this:
C#:
public static void TestMethod<T1, TResult>(Func<T1, TResult> myfunction ) {
...
}
Now, the idea is that I can call this static method by providing a function that matches the T1 and the TResult like this:
C#:
Func<string, string> ChangeName = name => $"My name is: {name}";
TestMethod<string, string>(ChangeName);
Now my understanding is that inside the TestMethod I received a function, now I can make use of the myfunction and call it to action, am I right? in this case, if I call the function it should return an string (according to my definition), so I think I should be able to do this:
C#:
public static void TestMethod<T1, TResult>(Func<T1, TResult> myfunction ) {
Console.WriteLine( myfunction.Invoke() );
// or
Console.WriteLine( myfunction() );
}
But I can't because it display an error asking for a T1 parameter...
PLEASE HELP!...
Continue reading...