1

I'm writing some code in C# to call a method that takes a callback. The method being called takes a callback as an optional parameter.

In this case I don't really need a callback for production purposes, but I'd like to put a Debug.Log in while I'm developing so I'm throwing in a lambda to log the results.

(Note that I fully understand this isn't necessary, I can just do the log in the called method, but this question is more about me learning how to do things in c# than practicality)

WebUtilities.instance.PushExperimentDataToApi(experimentType, formData, result => Debug.Log(result));

This works fine, but I'd like to pull that debug log out into a variable and pass in the variable instead. In other languages that I'm used to (dynamic) I could do something like:

let debug = string => console.log(string)
myClass.myMethod(arg1, arg2, debug)

And from looking around it seems like I may be able to accomplish the same thing with tuples, but I haven't quite figured out how.

Is there a way to do this? Again, I know this isn't necessary or practical in this case, I'm just using this as an example to better understand the language while building a project.

3 Answers 3

3

You can use Action<T> delegate for that

Action<string> debug = s => Debug.WriteLine(s);

And invoke it like debug("test"); or debug.Invoke("test");. You can also it pass to myMethod to invoke inside it.

myMethod in this case should accept an Action<string> debug parameter

void myMethod(object arg1, object arg2, Action<string> debug)
{
    ...
    debug("test");
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can achieve this with delegates, and in particular the Action<T>:

Action<string> debug = s => Console.WriteLine(s);

and then the following will work as you expect:

myClass.myMethod(arg1, arg2, debug)

where myMethod is declared as follows:

public void myMethod(SomeType arg1, SomeType arg2, Action<string> debug) 
{
    … 
    debug("something");
    …
}

1 Comment

Thanks for the quick reply. It's appreciated!
1

Is this the kind of thing you're after?

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Calling method with anonymous lambda.");
        do
        {
            Foo((string name, object data) =>
            {
                Console.WriteLine(name + "=" + data);
            });
        } while (Console.ReadKey().Key != ConsoleKey.Escape);

        do
        {
            Console.WriteLine("Calling method with null logger.");
            Foo();
        } while (Console.ReadKey().Key != ConsoleKey.Escape);


        Console.WriteLine("Calling method with delegate.");
        do
        {
            Foo(Logger);
        } while (Console.ReadKey().Key != ConsoleKey.Escape);
    }

    // Example exercising the Action<T1, T2, ...> delegate per accepted answer
    static void Foo(Action<string, object> logger = null)
    {
        int data = _random.Next(1, 100);
        logger?.Invoke("Random Number", data);
    }

    static void Logger(string name, object data)
    {
        Console.WriteLine(name + ":" + data);
    }

    // Sample data generator
    static readonly Random _random = new Random();
}

Where console output might be:

Calling method anonymous lambda.
Random Number=24
Random Number=76
Random Number=11
Calling method with null logger.
Calling method with null logger.
Calling method with null logger.
Calling method with null logger.
Calling method with delegate.
Random Number:37
Random Number:74
Random Number:25
Random Number:82

2 Comments

Hmm, not completely. I was looking for an equivalent to an implementation that I was already familiar with in a different language, but this is still helpful to look :) There are some structures and implementations in here that I haven't seen yet in c#. Thanks :)
I'm happy to see that while I was typing furiously you got your solution in a more simple fashion and it's good that you marked the one you did as an answer. Really, this is just 3 variations of that. As others pointed out, the key to this sample is the use of Action<string, object> as the argument to Foo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.