1

If I have a class like:

public class C1
{
    public class SC1 {}
    public class SC2 {}

    public class C1()
    {

    }

}

Why can't I do this:

C1 c1 = new C1();

c1.SC1.somePropety = 1234;
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • I don't know C#, but this looks sooo wrong... `class` before constructor, someProperty is not declared and would need to be static, ... –  Aug 12 '10 at 15:37
  • Similar topic here ....http://stackoverflow.com/questions/804453/using-inner-classes-in-c – Jagmag Aug 12 '10 at 15:42

5 Answers5

4

SC1 is a type definition. You still need to instantiate the type as a variable.

edit: delnan makes another point - if SC1 had "someProperty" declared as static, then that would also work.

Ankur Goel
  • 1,075
  • 9
  • 10
1

Because you have created a Class, but not a property for that class.

Try

public class C1
{
  public class SC1 {}
  public class SC2 {}

  public SC1 sc1;

  public C1() {};
}

C1 c1 = new C1();
c1.sc1.SomeProperty = 1234'
AllenG
  • 8,112
  • 29
  • 40
1

C1, C1.SC1 and C1.SC2 are independent classes. In the case of C1.SC1 and C1.SC2, the outer C1 class acts as a strange kind of namespace.

You still need to create an instance of C1.SC1 (new C1.SC1().someProperty = 1234) in order to access non-static members on it.

(However, one feature that inner classes have is the ability to access private members on instances of the outer class. Inner and outer classes are still independent -- it's just that, within the scope of an inner class, private starts acting like public.)

Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
  • Unless the property is `static` then you don't need an instance class –  Aug 12 '10 at 15:42
  • True, but that applies to all `static` members, not just ones in an inner/outer class situation. – Tim Robinson Aug 12 '10 at 15:45
  • Correct. I was just commenting on the fact that you 'need' an instance, when in fact you don't. It depends on your class design if that is architecturally ok or not. –  Aug 12 '10 at 15:47
1

I suggest reading the relevant articles on the MSDN for information about nested classes:

Rich
  • 3,081
  • 1
  • 22
  • 24
0

Tutorial on Nested Classes in C#

It should be something like this:

class Demo
{
    public static void Main()
    {
        System.Console.WriteLine(OuterClass.NestedClass.x);     
    }
}

class OuterClass
{
    public class NestedClass
    {
        public static int x = 100;
    }
}

The instance class is not required to access static variables. The rules that apply to standalone classes in case of static variables also apply to nested classes. For nested classes, fully qualified names of nested classes must be used to access the static variables as shown by the above program.

Apply to your example to use that someProperty.

Edit: I also noticed that your constructor definition is wrong for C1. You cannot define class on your constructor inside the class otherwise you'll get an error like this:

'C1': member names cannot be the same as their enclosing type
  • Dont Outerclass and NestedClass both need to be static in order to do System.Console.WriteLine(OuterClass.NestedClass.x); ? – Jagmag Aug 12 '10 at 15:44
  • !OAOD - thanks for clarifying that!! Sure didnt know of that one!! thats a gotcha for me!! – Jagmag Aug 12 '10 at 15:49
  • @In Sane: You sound to me like you're coming from Java. In C#, nested classes are static nested classes in Java. – Puppy Aug 12 '10 at 16:56