25

I am converting Java into C# and have the following code (see discussion in Java Context about its use). One approach might be to create a separate file/class but is there a C# idom which preserves the intention in the Java code?

   public class Foo {

    // Foo fields and functions
    // ...
        private static class SGroup {
            private static Map<Integer, SGroup> idMap = new HashMap<Integer, SGroup>();

            public SGroup(int id, String type) {
    // ...
            }
        }
    }
Community
  • 1
  • 1
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217

3 Answers3

90

All C# nested classes are like Java static nested classes:

C#:

class Outer
{
    class Inner
    {
    }
}

Is like Java's:

class Outer
{
    static class Inner
    {
    }
}

In other words, an instance of Inner doesn't have an implicit reference to an instance of Outer.

There isn't the equivalent of a Java inner class in C# though.

The accessibility rules are somewhat different between the two languages though: in C#, the code in the nested class has access to private members in the containing class; in Java all code declared within one top-level type has access to all the other private members declared within that same top-level type.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    I think you mean all *non-static* C# nested classes are like Java static nested classes? – Mishax May 02 '13 at 05:28
  • 4
    @Mishax: Well, static classes in C# have no equivalent in Java, whether they're top-level or nested. – Jon Skeet May 02 '13 at 05:42
  • 2
    That is true! My point is you emphasized the "All" in your answer and I think the statement "All C# nested classes are like Java static nested classes" is false – Mishax May 02 '13 at 06:25
  • 3
    Perfect answer, except for one thing: _"In C#, the code in the nested class has access to private members in the containing class; in Java it's the other way round."_ ... In Java, it is not the other way round, but both ways: In Java, the containing class has access to the nested class' private members, and the nested class has access to the conaining class' private members. – Markus Weninger Mar 04 '20 at 11:25
3

Give this a look http://blogs.msdn.com/oldnewthing/archive/2006/08/01/685248.aspx

I am looking specifically at

In other words, Java inner classes are syntactic sugar that is not available to C#. In C#, you have to do it manually.

If you want, you can create your own sugar:

class OuterClass {
 ...
 InnerClass NewInnerClass() {
  return new InnerClass(this);
 }
 void SomeFunction() {
  InnerClass i = this.NewInnerClass();
  i.GetOuterString();
 }
}

Where you would want to write in Java new o.InnerClass(...) you can write in C# either o.NewInnerClass(...) or new InnerClass(o, ...). Yes, it's just a bunch of moving the word new around. Like I said, it's just sugar.

Maestro1024
  • 3,173
  • 8
  • 35
  • 52
  • The blog is useful and shows that the construct can lead to some ugly syntax. – peter.murray.rust Oct 17 '09 at 11:22
  • 12
    Java's static nested class is just for scoping mechanism. Java's static nested class translates directly to C#'s: `class Foo { class Group { } }`. This is not the correct answer, choose @JonSkeet answer – Michael Buen May 13 '12 at 14:55
-2

You can have a static nested class in C#, according to Nested Classes.