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 7680953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:13:59+00:00 2026-05-31T18:13:59+00:00

public class SelfCallingTest { private static int counter; public void SelfCallingMethod(int counter) { Console.WriteLine(The

  • 0
public class SelfCallingTest
{
    private static int counter;

    public void SelfCallingMethod(int counter)
    {
        Console.WriteLine("The input integer is: {0} ", counter);

        counter++;

        while (counter <= 2)
        {
            SelfCallingMethod(counter);
        }
    }
}

The above SelfCallingTest class has a static field name “counter” and the SelfCallingMethod takes an integer parameter named “counter” (same name as static instance member counter)
SelfCallingMethod is called from console test application Main() method

static void Main(string[] args)
    {           
        SelfCallingTest sct = new SelfCallingTest();
        sct.SelfCallingMethod(0);

        Console.Read();
    }

Now the problem is that while loop is going in infinite loop because when the value of counter method level variable reaches value = 3 then the thread control is coming out of the method and again resuming while loop execution with method level counter variable getting set to 2 – I am not sure why this counter is set to 2.

As soon as I change the method parameter name counter to other than static instance field name, the results are expected and console is printing 0, 1, 2

Could you please provide me an explanation? Is there a bug in the .NET CLR to read a value from TLS (thread local storage?)

I got what the problem is here, for the initiating callstack for the method SelfCallingMethod, while loop condition is always evaluating 2 ==2 and thats causing the infinite loop.

  • 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. Editorial Team
    Editorial Team
    2026-05-31T18:14:00+00:00Added an answer on May 31, 2026 at 6:14 pm

    Your code is never using the static field. Look at this loop:

    while (counter <= 2)
    {
        SelfCallingMethod(counter);
    }
    

    counter is a local variable (the parameter). It’s then passed by value to SelfCallingMethod, so that method call isn’t going to change it. How would you expect to ever exit the loop, other than via an exception?

    So what will happen is you’ll get a stack with SelfCallingMethod(0), which calls SelfCallingMethod(1) (after the increment) which calls SelfCallingMethod(2) which calls SelfCallingMethod(3). That will increment its copy of the counter (to 4) and return… but then the loop from SelfCallingMethod(2) will just go round and call SelfCallingMethod(3) again. Don’t forget that each call of SelfCallingMethod has a separate counter variable.

    As soon as I change the method parameter name counter to other than static instance field name, the results are expected and console is printing 0, 1, 2

    Yes, because at that point all the code using counter in the method uses the field instead of the local variable. When the names are the same, the local variable hides the static variable. (You could still use SelfCallingTest.counter to access it explicitly though.)

    Is there a bug in the .NET CLR to read a value from TLS (thread local storage?)

    Absolutely not. The only bug is in your code, I’m afraid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class WrapperTest { static { print(10); } static void print(int x) { System.out.println(x);
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE;
public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
public class Bird { private static int id = 0; private String kind; public
public class upload extends Activity { private static final int CAMERA_REQUEST = 1888; private
public class Main { public static void main (String args[]) { int nums[]= {2,
public class Bird { private static int id = 0; private String kind; public
public class Sprite { private static final int BMP_COLUMNS = 3; private static final
public class StaticTest { private static StaticTest stObj=new StaticTest(); private static int VAR1=10; private

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.