Csharp/CSharp Tutorial/Class/Nested Class

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

class nesting

internal class Outer
{
    private static int state;
    internal class Inner
    {
        void Foo() { 
          state++; 
        }
    }
}

Inner class

using System;
class MyClass
{
   class MyCounter
   {
      public int Count = 0;
   }
   private MyCounter counter;
   public MyClass() {
      counter = new MyCounter();
   }
   public int Incr() {
      return counter.Count++;
   }
   public int GetValue() {
      return counter.Count;
   }
}
class MainClass
{
   static void Main()
   {
      MyClass mc = new MyClass();
      mc.Incr();
      mc.Incr();
      mc.Incr();
      mc.Incr();
      mc.Incr();
      mc.Incr();
      Console.WriteLine("Total: {0}", mc.GetValue());
   }
}
Total: 6

Nested class object

public class A
{
   public class B
   {
   }
}
public class MainClass
{
   static void Main()
   {
      A.B b = new A.B();
   }
}