-5

I have a piece of code which has an aspect as stated in title.

public string ReplaceCC(Match m)// Replace each Regex cc match with the number of the occurrence.
{ 
    i++;//it's used in function before declaration
    return i.ToString() + i.ToString();     
} 

public static int i=0;
}

I think there is a confusion of compilation/declaration on my mind.what is the issue here?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • This is very sloppy in regards to trying to understand what your issue is, please edit the question and post all relevant code that pertains to the issues you are having, furthermore, please edit the explanation and as a real question where are you calling `ReplaceCC()` ? – MethodMan May 03 '17 at 15:51
  • 1
    The variable is static. This means that is created before it is used. https://msdn.microsoft.com/en-us/library/79b3xss3.aspx – Steve May 03 '17 at 15:52
  • 1
    @Steve The code would compile just fine (although it would behave differently) even if it were an instance variable. – Servy May 03 '17 at 15:53

4 Answers4

0

Your are declaring the variable outside the scope of the function, as a static class variable:

public static int i=0;

The variable is initialized the first time you try to use it, when the static instance is created.

JuanR
  • 7,405
  • 1
  • 19
  • 30
0

You declare variable outside the scope, it is a global variable.

For example:

public class Person
{
    public Person(string name)
    {
        this.Name = name;
    }

    public string Name { get; set; }
}

This code declare the variable Name in root of Person.

0

As much as I can perceive, the thing which is baffling you is the order of code in which it is declared. Have a look at below code:

class Program
{
    public Program()
    {
    }

    public  void myFunc()
    {
        canIAccessIt = 10;
    }

    public  int canIAccessIt = 0;
}

As in above code anyone from C language background (ignore class part which doesn't exist in C) might get into a thinking that myFunc function is trying to access a variable canIAccessIt even before it has been declared or has come into scope.

Scope of variables in C# .Net world doesn't work that way. A class level variable (aka member variable) gets associated with the instance of the object instead and comes into scope from the time the object is created till the time object is destroyed or garbage collected.

Like in the above case, one might think that canIAccessIt variable might not have come to life as it is declared below myFunc method. It is not so.

When my above code is compiled by C# compiler it becomes something like this in MSIL (Microsoft intermediate language) written inside your *.dll file which is the output of the C# project building process:

class Program
{
    public  int canIAccessIt;
    public Program()
    {
        canIAccessIt = 0;
    }

    public  void myFunc()
    {
        canIAccessIt = 10;
    }
}

Note: The above MSIL code is just for your reference purpose so that you can understand it. MSIL code looks much more weird than this as it has to be understood by the CLR rather than a user or a programmer.

So for compiler it doesn't matter where the global variable is declared inside your class. As long as it is inside the opening({) and closing curly brace (}) of your class then its initialization will get moved inside the constructor of your class. Constructor of a class is the first method which gets called whenever you new-up a class instance. The moment it happens your variable will come to life (intantiated and initialized) and become in-scope to be used by myFunc method.

The only difference with i variable in your class is that it is static so its initialization will move the static constructor of your class rather than an instance constructor of your class. Life-time and scope of your i variable is associated with Type instance (aka static instance) of your class.

So whenever you initialize your class for the first time to be able to call your ReplaceCC method here is the sequence of events:

  1. Instance constructor gets called.
  2. Static constructor gets called - It initializes your static i variable.
  3. Then you call ReplaceCC method on the object instance when i is already instantiated and initialized.

You can also look at this answer which seconds my thoughts.

Community
  • 1
  • 1
RBT
  • 24,161
  • 21
  • 159
  • 240
-2
public string ReplaceCC(Match m)
{ 
    i++;//Here your adding 1 to the existing value
    return i.ToString() + i.ToString();     
} 

public static int i=0;//Here u declared and applied value, this is  declared when your class get instanciated

Please check the comment

  • **From review queue**: May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT May 03 '17 at 20:03