Domanda

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.

È stato utile?

Soluzione

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));
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top