静态方法不返回值

时间:2016-05-23 09:58:25

标签: c# windows-phone-8.1 vk-sdk

我做错了什么?执行到VKRequest.Dispatch之后,tmp会丢失数据并返回null

更新

public class GroupClass
{
    private static LinkedList<GroupClass> tmp = new LinkedList<GroupClass>();
    /* some code */
    public static LinkedList<GroupClass> GetGroupList()
    {
        VKRequest.Dispatch<VKList<VKGroup>> ( new VKRequestParameters ( "groups.get", "extended", "1", "filter", "admin, editor, moder", "fields", "photo_100" ), ( res ) =>
        {
            if ( res.ResultCode == VKResultCode.Succeeded && res.Data.count > 0 )
            {
                var item = res.Data.items[0];
                tmp.AddLast( new GroupClass ( item.id, item.name, item.screen_name, item.photo_100 ) );                            
            }
        }); //here
        return tmp;
    }
}

1 个答案:

答案 0 :(得分:0)

在调用lambda表达式之前,应该将变量复制到局部变量中。你可以找到更多的解释here

public class GroupClass
{
    private static LinkedList<GroupClass> tmp = new LinkedList<GroupClass>();
    /* some code */
    public static LinkedList<GroupClass> GetGroupList()
    {
        var tmp1 = tmp;
        VKRequest.Dispatch<VKList<VKGroup>> ( new VKRequestParameters ( "groups.get", "extended", "1", "filter", "admin, editor, moder", "fields", "photo_100" ), ( res ) =>
        {
            if ( res.ResultCode == VKResultCode.Succeeded && res.Data.count > 0 )
            {
                var item = res.Data.items[0];
                tmp1.AddLast( new GroupClass ( item.id, item.name, item.screen_name, item.photo_100 ) );                            
            }
        }); //here
        return tmp1;
    }
}