Вопрос

I have the following scenario:

private void RequestInfo(string a, string b)
{
   var c = a+b;
   library.FetchInfo(c, OnInfoReceived);
}

private void OnInfoReceived(CustomParameterType object)
{
   dictionaryOfInfo.Add(c, object);
}

As you can see, my problem is that I cannot access variable "c" in the callback passed as argument to FetchInfo function.

Это было полезно?

Решение

In C# 3.0 and later, you can use a lambda expression like this:

private void RequestInfo(string a, string b)
{
   var c = a+b;
   library.FetchInfo(c, obj => dictionaryOfInfo.Add(c, obj));
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top