0

I have this rather strange(probably stupid) question: I have two classes:

class Sharp {
    public int x;
    public Sharp(int j) {
       x = j;
    }
}
    

and

the class containing the Main method:

Obviously I can not initialize many objects and assign them to the same variable. So when I do to do this

    Sharp ooo = new(7);
    Sharp ooo = new(9);

The compiler throws an error which is absolutely fine.

But for some reason the following code compiles fine:

    for (int i = 0; i <= 5; i++) {
        Sharp ooo = new(i);
        Console.WriteLine(ooo.x);

    }

I am trying to understand why would that compile if I am trying to initialize the ooo times. Please help me.

Eric Movsessian
  • 488
  • 1
  • 11
  • 5
    Those are 6 separate `ooo` variables, basically. Only one is in scope at a time though. It's worth differentiating between an *object* (which doesn't have a name) and a *variable* (which does). You could have multiple variables, all of which have values which are references to the same object, for example. See [this answer](https://stackoverflow.com/questions/32010172/what-is-the-difference-between-a-variable-object-and-reference/32010236#32010236) for more details on the differences. – Jon Skeet May 10 '22 at 18:19
  • 1
    In the for, the scope of the ooo variable is into the for (the brackets). You create and "destroy" the same variable many times but you aren't creating many objects at the same time. – Victor May 10 '22 at 18:19
  • 1
    that because the for loop creates a new scope for each iteration. – pm100 May 10 '22 at 18:19
  • 1
    Think in a function instead of the for. You can call a function many times and you are creating inside the function (in the function scope) the same variable, but in differents moments. – Victor May 10 '22 at 18:21
  • 1
    A variable or another named element in code (such as a method or field) referring to an address of allocated memory has a scope in which it is "visible" and within which it is unique. In C#, the scope of such a named code element is a *block* defined by a pair of curly braces. Be it a for loop, a method, an if statement, etc. You could also write `{ var a = 1; }{ var a = 2; }`. – lidqy May 10 '22 at 18:39
  • Thank you very much, all of your comments made it very clear. – Eric Movsessian May 11 '22 at 21:24
  • @pm100 could you please tell me this: those scopes that are created they are sort of parallel scopes right? ike this: {}{} but not like this {{}} – Eric Movsessian May 14 '22 at 22:22
  • 1
    @EricMovsessian there is one 'ooo' in your loop. It gets reinitlaized each time round. Importantly there is no abmiguity when you say 'ooo'. In your failing case you have 2 things called ooo (or are trying to) if it worked which one would 'ooo' refer to? – pm100 May 14 '22 at 22:30
  • @pm100, thank you for your response, I am so sorry but I ma not sure I understood. You said that with each iteration a new scope is created. If a new scope is created that a variable with the same name can be initialized in each scope. I wanted to make sure that I got cuirrectly that the newly created scope is not inside the older scope right? Because if It was that there would be two "ooo"s which is obviously impossible. – Eric Movsessian May 14 '22 at 22:44
  • 1
    I hope I understand your question. If you have an ooo outside the loop and one inside it (or any new scope) then the outer ooo is hidden by the inner one. It called 'shadowing'. Many compilers will warn you about it – pm100 May 14 '22 at 22:54
  • Thank you so much. Let me ask It like this. You said that each time a for loop iterates, a new scope gets created, right? So if that is the case, are those scopes like inside of each other or completely independent? – Eric Movsessian May 15 '22 at 11:50

0 Answers0