Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 153379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:47:26+00:00 2026-05-11T09:47:26+00:00

I am making a seperate thread here because I believe if I write a

  • 0

I am making a seperate thread here because I believe if I write a comment, the comment won’t push the thread to the top and thus there’ll be notification for anyone who has written to that thread.

This code was supplied by Micheal Buen at the thread Writing text to the middle of a file :

        LinkedList<string> beatles = new LinkedList<string>();          beatles.AddFirst('John');         LinkedListNode<string> nextBeatles = beatles.AddAfter(beatles.First, 'Paul');         nextBeatles = beatles.AddAfter(nextBeatles, 'George');         nextBeatles = beatles.AddAfter(nextBeatles, 'Ringo');         nextBeatles = beatles.AddAfter(nextBeatles, 'George');         nextBeatles = beatles.AddAfter(nextBeatles, 'Ringo');          nextBeatles = beatles.AddAfter(nextBeatles, 'George');         nextBeatles = beatles.AddAfter(nextBeatles, 'Ringo');          nextBeatles = beatles.AddAfter(nextBeatles, 'George');         nextBeatles = beatles.AddAfter(nextBeatles, 'Ringo');          nextBeatles = beatles.AddAfter(nextBeatles, 'George');         nextBeatles = beatles.AddAfter(nextBeatles, 'Ringo');           // change the 1 to your 5th line         LinkedListNode<string> paulsNode = beatles.NodeAt(6);         LinkedListNode<string> recentHindrance = beatles.AddBefore(paulsNode, 'Yoko');         recentHindrance = beatles.AddBefore(recentHindrance, 'Aunt Mimi');         beatles.AddBefore(recentHindrance, 'Father Jim');           Console.WriteLine('{0}', string.Join('\n', beatles.ToArray()));          Console.ReadLine();  public static class Helper {     public static LinkedListNode<T> NodeAt<T>(this LinkedList<T> l, int index)     {         LinkedListNode<T> x = l.First;          while ((index--) > 0)         {                 x = x.Next;             Console.Write(x.Value);             Thread.Sleep(10000);         }            return x;     } } 

What I am wondering is, what does the extension method achieve?

On first pass, x = x.Next means we are looking at Ringo rather than George, and so on. What exactly is happening under the bonnet and what is the code doing from the point when I call NodeAt(6) onwards? I ask this as it’s important to be able to read and understand code without using the stepping through approach as an aid (sometimes at work you will read code in a printed document, for example). Also, why do we count backwards in the loop and why is there a bracket to subtract 1 before entering the loop body?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:47:27+00:00Added an answer on May 11, 2026 at 9:47 am

    The extension method only steps through the LinkedList n elements, it initializes x with the first element of the list (l.First) and then in the while, it decrements the index to step forward n times (look x = x.Next;):

     Index:  1           2            3            4      _ _ _ _     _ _ _ _     _ _ _ _ _     _ _ _ _     | John  |-> | Paul  |-> | George  |-> | Ringo |      ‾ ‾ ‾ ‾     ‾ ‾ ‾ ‾     ‾ ‾ ‾ ‾ ‾     ‾ ‾ ‾ ‾ 

    So if you call the method with the index 4 (NodeAt(4)), it will get the first item (John), decrement the counter (to 3), step to the next item (Paul), decrement again (2), get the next item (George), decrement (to 1), get the next item (Ringo) and then decrement to 0 and this will exit the while, and return the LinkedList Item, at the 4 position (Ringo).

    Additionally you might want to check the ElementAt extension method provided by System.Linq to achieve the same:

    var linkedList = new LinkedList<string>(new []{'John', 'Paul', 'George', 'Ringo'}); linkedList.ElementAt(3); // this returns you Ringo, note that                           // the index is 0-based! 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 94k
  • Answers 94k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer CTRL+H -> Find Options -> Use Regular Expressions | WildCards.… May 11, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer You could do this: <td class="a"><div class="b">...</div></td> Then the td… May 11, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer You shouldn't use DBNull with LinqToSql. The point is Language… May 11, 2026 at 6:53 pm

Related Questions

Here's the scenario: I have a multi threaded java web application which is running
In a WPF UserControl, I have to make to call to a WebService. I
I am working on a desktop application in PyGTK and seem to be bumping
What is the best solution for maintaining backup and revision control on live websites?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.