System.Collections.Generic.LinkedList<GameObject> how to get the first element

Hi, I have a LinkedList and I want to do something like this :

LinkedListorder=newLinkedList();

order.getFirst().GetComponent().text = “something”

The problem is that the method getFirst() which is saupoust to give me the first element doesnt exist. How can I do ?

There isn’t a “GetFirst()”. LinkedList have a “.first” property.

Generally linked lists are slower than Queues if you need the push/pop functionality and less flexible than Lists if you are trying to access elements in the middle… might want to reconsider using that collection

It looks like Linked list include a First and Last property.
so order.First should give you the first object.

Yes, but when I write this :
linkedList.First.GetComponent().text
I get an error like this :
Type System.Collections.Generic.LinkedListNode<UnityEngine.GameObject>' does not contain a definition for GetComponent’ and no extension method GetComponent' of type System.Collections.Generic.LinkedListNode<UnityEngine.GameObject>’ could be found (are you missing a using directive or an assembly reference?)

Never used a linkedlist before, so not sure how they work.

Generally you can right click on an error and if using VS, there is a lightbulb. Click it and see if it offers you options to resolve the issue. Mono has a similar option, but I don’t remember what they call theirs.

Otherwise, it looks like it’s returning a linkedlistnode, which based on this LinkedListNode<T> Class (System.Collections.Generic) | Microsoft Learn

Suggest you need to get the value of it.

order.First.Value.GetComponent

Really, all this info can be found on MSDN usually. Sounds like this may be the first time you are using a linkedList in c#?

2 Likes

Did you put this in there:

using System.Collections.Generic;

Thank you. In the link you gave me I found this :
linkedList.First.Value.GetComponent().text
And yeah I’m using it first time and all i found was just from another forum threads

When I search on Google for C# answers I do this:

C# linkedlist

The MSDN site is in the first couple results, its always the first place I go.
You could do this too:

C# linkedlist msdn