2

[Edit] What I wanted to ask was just putting a class name with this, so it wasn't about referencing an outer class member. Sorry for my inappropriate example!

[Edit2] Someone reported this as a duplicate BUT NOT! As I said earlier, I just wanted to know if it's possible to reference MyClass.this and this interchangeably like in Java. This wasn't a practical question at all but just for learning C# language itself. I don't mind removing this if people really think it's a duplicate so let me know.

In Java, you can use this with class names like this:

class OuterClass {
    int outerMember = 1;

    class InnerClass {
        int innerMember = 2;

        public void printOuterMember() {
            System.out.println(OuterClass.this.outerMember);
            System.out.println(outerMember);
        }

        public void printInnerMember() {
            System.out.println(InnerClass.this.innerMember);
            System.out.println(this.innerMember);
            System.out.println(innerMember);
        }
    }
}

Sometimes class names are not needed, but sometimes helpful.

So I tried the same thing in C# but it seems it's impossible. Am I right?

Jenix
  • 2,996
  • 2
  • 29
  • 58
  • 2
    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/this, https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static – ASh Sep 05 '17 at 08:18
  • Any reason why you would want to use it like that? – Jerodev Sep 05 '17 at 08:18
  • https://stackoverflow.com/a/23502/2794280 – kgzdev Sep 05 '17 at 08:19
  • 3
    `Outerclass` and `Innerclass` are actually two completely different classes in C#. So the inner class doesn't hold a reference from the outer class. It is possible that there is not even an instance of the outerclass. It's more like a namespace. – Tim Schmelter Sep 05 '17 at 08:21
  • @Jerodev Coming from Java, just curious. And sometimes it's also more readable to me. – Jenix Sep 05 '17 at 08:33

1 Answers1

7

C# does not support this, in Java the nested class captures the parent object reference. C# nested classes are more like static nested classes in Java. If you want access to the parent class you will need to pass a reference to it in the nested class constructor.

Nested classes will have access to private fields of the parent class if they have a reference to it, so you can achieve similar results, just the access to the parent class instance is not automatic as it is in Java. So this code works

class Owner
{
    private int field;
    class Nested
    {
        public Nested(Owner owner) { this.owner = owner; }
        Owner owner;
        public int D()
        {
            return owner.field;
        }
    }
}
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
  • As I edited my question, in fact it was just an example. But now I'm sure it's impossible and your answer was also helpful, thanks! – Jenix Sep 05 '17 at 08:23
  • _"C# does not support this"_ is a bit misleading, _not support_ sounds like it's lacking this feature. Actually C# is implemented differently, the outer and inner class are not related to each other, the outer class is like an additional namespace for the inner class but you can instantiate the inner class without an instance of the outer class: `var ic = new OuterClass.InnerClass();`(no instance of `OuterClass` exist) – Tim Schmelter Sep 05 '17 at 08:26
  • @TimSchmelter Not sure how to phrase it .. C# does not do this automatically for you so it is in a sense a missing feature. Weather this features should be in C# is a different question. I don't think it should be, you can easily achieve the same with about 3 extra lines of code. – Titian Cernicova-Dragomir Sep 05 '17 at 08:29
  • @TitianCernicova-Dragomir: no, you can't achieve the same because there is no instance of the outer class if you have an instance of the inner class. And even if there is one, there is no relation between the instance of an outer class and the inner class. Only if the inner class holds an instance of the outer class it could access the fields, but that can be done with any other class(and public fields/properties) too. – Tim Schmelter Sep 05 '17 at 08:30
  • @TimSchmelter I don't mean you can achieve the same syntactically, but if you manually pass in a reference to the parent class (as in the example I give) you can do anything you could have done if the reference would have been automatically captured – Titian Cernicova-Dragomir Sep 05 '17 at 08:32