17

I have the following Java code:

public class A {
    private int var_a = 666;

    public A() {
        B b = new B();
        b.method123();
        System.out.println(b.var_b);
    }

    public class B {
        private int var_b = 999;

        public void method123() {
            System.out.println(A.this.var_a);           
        }
    }
}

Which yields 666 and 999. Now, I've tried to set up similar code in c#, but it seems that it is not possible to accomplish the same. If that's the case, how you usually achieve a similar effect when programming in c#?

2
  • You can definitely have inner classes in C#. Can you show the code you used to convert to C# or any errors you received? This also may help: blogs.msdn.com/oldnewthing/archive/2006/08/01/685248.aspx Commented Mar 2, 2010 at 21:45
  • 1
    @NG: No, C# doesn't have inner classes. Nested classes behave significantly differently from inner classes. Commented Mar 30, 2014 at 20:15

4 Answers 4

28

You need to make the inner class take an instance of the outer class as a constructor parameter. (This is how the Java compiler implements inner classes)

Sign up to request clarification or add additional context in comments.

2 Comments

Note that in Java this is done mostly because classes tend to get to big, with too many member variables and too many responsibilities and people want to group the functionality in inner classes without splitting up the outer class too much. So basically you'd only want this for reading purposes. E.g. group similar functions together without splitting up a class in multiple classes. This can be easily achieved with C# regions.
@TonniTielens: No; it's primarily done to pass an interface implementation that is tied to the parent class (eg, event handlers). C# uses delegates instead.
15

Inner classes are handled slightly differently between C# and Java. Java implicitly passes a reference to an instance of the outer class to the inner class, allowing the inner class to access fields of the outer class. To gain similar functionality in C#, you just have to do explicitly what Java does implicitly.

Check out this article for some more information.

Comments

5

From a Java perspective, C# inner classes are like java nested classes (what you get if you declare public static class B in your code).

The reason for the difference is that C# supports delegates, which replaces the major use case for Java inner classes. Once delegates are in the language the designers of C# felt it unnecessary to add a distinction between inner and nested classes.

1 Comment

The lack of Java-style inner classes actually causes significant pain when you decompose a class using strategy or state patterns.
2

here is your code:

var_b need to be internal, which is between private and public and means like "accessible for namespace-classes":

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    public class A
    {
        private int var_a = 666;

        public A() 
        {
            B b = new B(this);
            b.method123();
            Console.WriteLine(b.var_b);
        }

        public class B
        {
            private A a = null;

            public B(A a)
            {
                this.a = a;
            }

            internal int var_b = 999;

            public void method123() 
            {
                Console.WriteLine(a.var_a);
            } 
        }
    }


    class Program
    {

        static void Main(string[] args)
        {
            new A();
        }
    }
}

1 Comment

var_a does not need to be internal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.