简体   繁体   中英

LinkedList in c# xamarin

I have a linked list which contains views. I want to assign the last view element in the list to another view variable.

This is what I did :-

private readonly LinkedList<View> bufferedViews = new LinkedList<View>();
View myView = bufferedViews.RemoveLast ();

also this:-

if (bufferIndex + 1 > sideBufferSize)
{
    releaseView(bufferedViews.RemoveFirst());
}

But I get an error saying :-

Cannot convert from void to Android.Views.View

This isn't a Xamarin issue. Your code is just broken. Both RemoveFirst and RemoveLast are void methods - they don't return the first/last elements, they just remove them.

You'll need to use the First and Last properties, then remove the first and last values afterwards - assuming you actually want to remove the value. (It's not clear from the code whether you really do.)

You could always write extension methods to do what you want though:

public static T FetchAndRemoveFirst<T>(this LinkedList<T> list)
{
    T first = list.First.Value;
    list.RemoveFirst();
    return first;
}

public static T FetchAndRemoveLast<T>(this LinkedList<T> list)
{
    T last = list.Last.Value;
    list.RemoveLast();
    return last;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM