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

  • SEARCH
  • Home
  • 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

Related Questions

Making UML sequence diagram in VS 2010RC I've observed that there is no activation
Taking this thread to the next level and now making a Main.as class to
Making echo of a question around the web: Is the syntax for svn:ignore patterns
Making my first steps with NHibernate, I'm trying to have it creating my Tables
Making an adobe flex ui in which data that is calculated must use proprietary
Making my first steps in RIA Services (VS2010Beta2) and i encountered this problem: created
When making changes using SubmitChanges() , LINQ sometimes dies with a ChangeConflictException exception with
After making some changes in my models (eg. new field in a model and
After making a few modifications to a rails app I am tinkering on, railroad
Im making a small python script to upload files on the net. The script

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.