I'm trying to learn some basics of C# (not new to programming in general) and I'm having trouble making some code for a LinkedList work properly. Specifically, I'm getting a "possible null reference" error when assigned a value from the LinkedList to LinkedListNode variable - is it not possible to do this? I got the code off a website (https://dev.to/adavidoaiei/fundamental-data-structures-and-algorithms-in-c-4ocf) so it seems odd to me that the code would be completely wrong.
This is a section of the code causing issues:
// Create the linked list
string[] words =
{ "the", "fox", "jumps", "over", "the", "dog" };
LinkedList<string> sentence = new LinkedList<string>(words);
Display(sentence, "The linked list values:");
Console.WriteLine("sentence.Contains(\"jumps\") = {0}",
sentence.Contains("jumps"));
// Add the word 'today' to the beginning of the linked list
sentence.AddFirst("today");
Display(sentence, "Test 1: Add 'today' to beginning of the list:");
// Move the first node to be the last node
LinkedListNode<string> mark1 = sentence.First;
sentence.RemoveFirst();
sentence.AddLast(mark1);
Display(sentence, "Test 2: Move first node to be last node:");
When initialising the variable 'mark1', I'm getting a "possible null reference error":
LinkedListNode<string> mark1 = sentence.First;
Is what I'm trying to do possible at all or is this the completely wrong way to work with a LinkedList?