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#?

devoured elysium
  • 101,373
  • 131
  • 340
  • 557
  • 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: http://blogs.msdn.com/oldnewthing/archive/2006/08/01/685248.aspx – NG. Mar 02 '10 at 21:45
  • 1
    @NG: No, C# doesn't have inner classes. Nested classes behave significantly differently from inner classes. – Ben Voigt Mar 30 '14 at 20:15

4 Answers4

27

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)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 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. – Tohnmeister Apr 30 '15 at 08:56
  • 2
    @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. – SLaks Apr 30 '15 at 14:12
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.

Corey Sunwold
  • 10,194
  • 6
  • 51
  • 55
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.

Yishai
  • 90,445
  • 31
  • 189
  • 263
  • 3
    The lack of Java-style inner classes actually causes significant pain when you decompose a class using strategy or state patterns. – Jeffrey Hantin Mar 02 '10 at 22:04
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();
        }
    }
}
Phil Rykoff
  • 11,999
  • 3
  • 39
  • 63