Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How is it possible for this code compile?

Discussion in 'Scripting' started by ArachnidAnimal, Jul 13, 2016.

  1. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    Can someone please explain how this C# code can possibly compile?

    Code (csharp):
    1.  
    2. public class _Test
    3. {
    4.    private int x;
    5.  
    6.    public void Test()
    7.    {
    8.      _Test test = this;
    9.  
    10.      int y = test.x;
    11.    }
    12. }
    13.  
    x is private. So how can the line "test.x" compile?
    The compiler does not complain about this.

    Thanks.
     
  2. SkaredCreations

    SkaredCreations

    Joined:
    Sep 29, 2010
    Posts:
    296
    Because it's a property of this class, of course. It works because you are using it inside the same class, that's why the compiler is not raising error even if it's private.
     
    ArachnidAnimal likes this.
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yes, private means private to this class, not this method or function.
     
    ArachnidAnimal likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    You can check the private members of other objects of the same class. This is often used for the implementation of Equals:

    Code (csharp):
    1. public class Foo {
    2.  
    3.     private int someInt;
    4.  
    5.     public override bool Equals(object obj) {
    6.         if (obj == null || GetType() != obj.GetType()) {
    7.             return false;
    8.         }
    9.         Foo other = (Foo) obj;
    10.  
    11.         return other.someInt == someInt;
    12.     }
    13. }
    private members are also available in a static context - so if you've got a static method in Foo that takes a Foo argument, you can use that Foo's privates.

    (snicker. Use it's privates)
     
    Dave-Carlile and ArachnidAnimal like this.
  5. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    Hmm, interesting. I did not know that you could do this in C#. It makes sense now.
    I think I remember using other languages where this was not allowed, I can't recall the exact languages though because it was so long ago.

    I like the scripting forum because usually within one minute of posting a question, it gets answered. LOL
     
  6. SkaredCreations

    SkaredCreations

    Joined:
    Sep 29, 2010
    Posts:
    296
    Well, theoretically it works this way in any language. The exception is when you try to access a private variable from an inherited class, in which case you need to declare the variable as protected (that means it's accessible from inside any class that inherits from this).
     
    ArachnidAnimal and Dave-Carlile like this.