Nirmal
Nirmal

Reputation:

Nested class in .net

I have a nested class. I want to access the outer and nested classes in other class. How to access both class properties and methods and my condition is i want to create object for only one class plz provide the code snippet

Upvotes: 0

Views: 679

Answers (4)

Austin Salonen
Austin Salonen

Reputation: 50245

In my opinion, the reason to nest a class is that it will only ever be used by its parent class. If you need to access the inner class, you should revisit using the nested class in the first place.

Upvotes: 3

itsmatt
itsmatt

Reputation: 31416

You can learn about Nested Types here.

Upvotes: 2

Craig Eddy
Craig Eddy

Reputation: 969

Although I would never recommend nested public classes, here's some code:

public class Foo() {
    public Foo() { }

    private Bar m_Bar = new Bar();  

    public Bar TheBar { get { return m_Bar; } }

    public class Bar { ... }
}

Upvotes: 0

Konrad Rudolph
Konrad Rudolph

Reputation: 546153

Your question isn't very clear. However, I guess you're from a Java background. C#'s and VB's nested classes behave much different from Java's nested classes. In fact, they behave much like Java's static nested classes, i.e. they don't belong to an instance of the outer class. Therefore, instances of the inner class can't access nonstatic fields in the outer class (at least not without being given an instance explicitly).

Upvotes: 0

Related Questions