Reputation: 103
public string SendMessage(string command, Action<string> Result = null) {
string response = "test";
Result(response);
return response;
}
Usage:
string Result = SendMessage("hey");
SendMessage("hey", a => {
Console.WriteLine(a);
});
I m getting nullreferenceexception if using
string Result = SendMessage("hey");
edit: do I really need to make 2 functions?
public string SendMessage(string command) {
string response = "test";
return response;
}
public void SendMessage(string command, Action<string> Result) {
string response = "test";
Result(response);
}
Upvotes: 0
Views: 55
Reputation: 11364
public string SendMessage(string command, Action<string> Result = null) {
string response = "test";
Result?.Invoke(response); // here
return response;
}
This will fix your issue
Upvotes: 1