Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of Nested Classes

Tags:

c#

If I have a nested class, does anything from the owning class exist in the owned class?

for example:

public class OwningClass 
{

    int randomVariable = 1;

    public void MakingMethod()
    {
        OwnedClass owned = new OwnedClass();
        owned.SomeMethod();
    }

    private class OwnedClass
    {
        public void SomeMethod()
        {
            // Is anything from OwningClass available here?
        }

    }

}
like image 390
Vaccano Avatar asked Dec 18 '25 18:12

Vaccano


2 Answers

Anything "static" from the owning class is available in your nested class.

If you have an instance of the owning class in some method of the inner class, you may also access its private members.

like image 108
Philippe Leybaert Avatar answered Dec 20 '25 08:12

Philippe Leybaert


The only thing a nested class changes with respect to it's parent class is accessibility. The nested class can access private members of the containing type.

like image 37
JaredPar Avatar answered Dec 20 '25 07:12

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!