Visual Studio .NET - what are inner classes in .net?

Asked By aman on 22-Aug-11 01:41 AM
hi all,

what are inner classes in .net?
what is the use inner classes in .net?
can inner classes are accesible by outside class?

please give some example so that i can make use of it.....

thanks and regards
Aman Khan
Ravi S replied to aman on 22-Aug-11 01:44 AM
HI

Except the inner class, there are two types of supplementary  inner classes :
 
The inner class which is declared inside the body of a method is known as the local inner classes. The class declared inside the body of a method without naming it is known as anonymous inner classes.
 
Here we are discussing  anonymous inner classes.
 
The anonymous inner classes is very useful in some situation. For example consider a situation where you need to create the instance of an object without creating subclass of a class and also performing additional tasks such as method overloading.
 
Consider the following code :
 
button.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e)
{
// do something.
}
});


refer links for details
http://social.msdn.microsoft.com/Forums/eu/csharplanguage/thread/e5c24afa-06f6-460c-8f27-203f064c0d8d
http://www.roseindia.net/javatutorials/anonymous_innerclassestutorial.shtml
http://www.geekinterview.com/question_details/64739
Venkat K replied to aman on 22-Aug-11 01:46 AM
C# supports nested classes, and it does not provide any syntactic support for inner classes.

A nested class has no special relationship with the outer class. In Java, the "static" keyword is used for this purpose.

An inner class has the "this" pointer behaviour without the keyword "static", and has access to member instance of the outer class. the implemetor of C# think the language is simpler and better without it.

Below is a sample from Microsoft implementing inner class

using System;
using System.Collections;

public class Tokens: IEnumerable
{
private string[] elements;

Tokens(string source, char[] delimiters)
{
elements = source.Split(delimiters);
}

// IEnumerable Interface Implementation:
public TokenEnumerator GetEnumerator() // non-IEnumerable version
{
return new TokenEnumerator(this);
}

IEnumerator IEnumerable.GetEnumerator() // IEnumerable version
{
return (IEnumerator) new TokenEnumerator(this);
}

// Inner class implements IEnumerator interface:
public class TokenEnumerator: IEnumerator
{
private int position = -1;
private Tokens t;

public TokenEnumerator(Tokens t)
{
this.t = t;
}

public bool MoveNext()
{
if (position < t.elements.Length - 1)
{
position++;
return true;
}
else
{
return false;
}
}

public void Reset()
{
position = -1;
}

public string Current // non-IEnumerator version: type-safe
{
get
{
return t.elements[position];
}
}

object IEnumerator.Current // IEnumerator version: returns object
{
get
{
return t.elements[position];
}
}
}

// Test Tokens, TokenEnumerator
static void Main()
{
Tokens f = new Tokens("This is a well-done program.",
new char [] {' ','-'});
foreach (string item in f) // try changing string to int
{
Console.WriteLine(item);
}
}
}

TSN ... replied to aman on 22-Aug-11 01:46 AM
hi..

In C# I know of 3 differences between regular classes and inner classes which can also form a relationship relationship between an inner class and the outer class that contains it.

One difference is that inner classes can be declared as protected, internal, protected internal, or private while normal classes cannot.

An inner class can access the static members of the containing outer class without using that classe's name.

Another difference is when the inner class accesses an instance of the outer class, it can access that object's private members even if they are not static.

Jitendra Faye replied to aman on 22-Aug-11 01:47 AM

A good question that is not always immediately answerable. There are several reasons to have inner classes. But the two that jump to mind are these:

  • Organizing code into real world situations where there is a special relationship between two objects.
  • Hiding a class within another class so that you do not want the inner class to be used from outside of the class it is created within.
namespace TestApp
{
  class Program
  {
    [STAThread]
    static void Main()
    {
      Application.Run(new Form1());
    }

    class Test1
    {
      class Test2
      {
        class Test3
        {
          
        }
      }
    }
  }
}

Follow this link-

http://en.csharp-online.net/Nested_Classes
Riley K replied to aman on 22-Aug-11 03:11 AM

A nested class is declared in the same manner as a normal http://en.csharp-online.net/Declaring_Classes. The difference is that a nested class has access to all of the available modifiers.

The this keyword reference in the inner class only holds a reference to the inner class. Data members of the outer class are not accessible using the this reference in the inner class. If this is needed, pass a reference of the outer class into the constructor of the inner class.


static members of the outer class are available in the inner class irrespective of the accessibility level.

The class B here is enclosed inside the declaration of class A. Class B is thus a nested class. Because it has a public accessibility modifier, it can be accessed in places other than class A's scope.

class A
{
  public int _v1;
 
  public class B
  {
  public int _v2;
  }
}
 
class Program
{
  static void Main()
  {
  A a = new A();
  a._v1++;
 
  A.B ab = new A.B();
  ab._v2++;
  }
}

Hope you got it

Regards

Irfan Khan replied to aman on 22-Aug-11 03:48 PM
An inner class or nested class is a http://en.wikipedia.org/wiki/Class_%28computer_science%29 declared entirely within the body of another class or interface. It is distinguished from a http://en.wikipedia.org/wiki/Subclass_%28computer_science%29.

An instance of a normal or top-level class can exist on its own. By contrast, an instance of an inner class cannot be instantiated without being bound to a top-level class.

Let us take the abstract notion of a Car with four Wheels. Our Wheels have a specific feature that relies on being part of our Car. This notion does not represent the Wheels as Wheels in a more general form that could be part of a vehicle. Instead, it represents them as specific to this one. We can model this notion using inner classes as follows:

We have the top-level class Car. Instances of Class Car are composed of four instances of the class Wheel. This particular implementation of Wheel is specific to the car, so the code does not model the general notion of a wheel that would be better represented as a top-level class. Therefore, it is semantically connected to the class Car and the code of Wheel is in some way coupled to its outer class.

Inner classes provide us with a mechanism to accurately model this connection. We say that our Wheel class is Car.Wheel, Car being the top-level class and Wheel being the inner class.

Inner classes therefore allow for the object orientation of certain parts of the program that would otherwise not be encapsulated into a class.

Larger segments of code within a class might be better modeled or refactored as a separate top-level class, rather than an inner class. This would make the code more general in its application and therefore more re-usable but potentially might be premature generalization. This may prove more effective, if code has many inner classes with the shared functionality.