J. Doe
J. Doe

Reputation: 35

Accessing outer class with same name as nested class

I apologize if there's an answer out there, but I couldn't find any.
I'm currently in the situation shown as below. I expected it to throw an error, but it didn't.
The variable inside 'Outer' called 'inner', is an instance of Outer.Inner, the private nested class. I'm wondering how it's possible to access the non-nested Inner instance.
This probably could be done by using different namespaces, but is there any other way?

public class Outer {
    private Inner inner = new Inner(); // This is the Outer.Inner class

    private class Inner { }
}

public class Inner { }

Upvotes: 0

Views: 645

Answers (2)

adjan
adjan

Reputation: 13684

If you don't want to wrap it in namespaces you can use global:: to refer to types inside the global namespace:

public class Outer
{
    private global::Inner inner = new global::Inner(); 
    private class Inner
    {
    }
}

public class Inner
{
}

Upvotes: 2

user1579234
user1579234

Reputation: 501

This seems to work fine.

I used the namespace before the class name.

namespace Example
{
    public class Outer
    {
        private Example.Inner inner = new Example.Inner();

        private class Inner { }
    }

    public class Inner { }
}

Upvotes: 1

Related Questions