dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [DotGNU]How do you simulate an inner class in C#?


From: Thong \(Tum\) Nguyen
Subject: RE: [DotGNU]How do you simulate an inner class in C#?
Date: Sun, 25 May 2003 17:19:14 +1200

Hi Mohan,

A java inner class is simply a class that has a reference to the parent
class.  You just need to pass a reference to the 'outer' class to the
'inner' class's constructor and store it somewhere.

Here's an example:

Java code:

public class Outer
{
   int x;

   public class Inner
   {
       public void Foo()
       {
           x = 10;
       }
   }
}

The java compiler generates something like this:

public class Outer
{
}

public class Outer$Inner
{
   private Outer parent;

   public class Outer$Inner(Outer parent)
   {
      this.parent = parent;
   }

   public void Foo()
   {
       this.parent.x = 10;
   }
}

In C# you could do this:

public class Outer
{
   public class Inner
   {
      private Outer parent;

      public Inner(Outer parent)
      {
          this.parent = outer;
      }

      public void Foo()
      {
          this.parent.x = 10;
      }
   }
}

Delegates aren't needed :)

^Tum


> -----Original Message-----
> From: Mohan Embar [mailto:address@hidden
> Sent: Sunday, 25 May 2003 5:07 p.m.
> To: address@hidden
> Subject: [DotGNU]How do you simulate an inner class in C#?
> 
> Hi People,
> 
> Sorry to bug you all with a non-DotGNU-related question, but I figured
that
> if anyone can answer this, you people can. I've searched the web for
this,
> but haven't found anything satisfactory. I'm experienced with Java,
but
> new to C#.
> 
> What is the best way to simulate a Java inner class in C#? Not a
nested class,
> but an inner class. In particular, I want to simulate an inner class
CI of C
> which implements interface I.
> 
> Does anyone have any instant answer to this? I suspect a combination
of a
> nested, adapter class of C which implements I and takes some delegates
of
> C in its constructor will do the trick, but wanted to see if anyone
has
> thought about this more.
> 
> Thanks in advance.
> 
> -- Mohan
> http://www.thisiscool.com/
> http://www.animalsong.org/
> 
> 
> _______________________________________________
> Developers mailing list
> address@hidden
> http://dotgnu.org/mailman/listinfo/developers



reply via email to

[Prev in Thread] Current Thread [Next in Thread]