Class,Inner Class(or)Nested class Example in C#
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleExample
{
public class OuterClass
{
public void OuterClassFunction()
{
Console.WriteLine("I am in Outer Class Function");
}
//Reinitialize the class
public InnerClass abc()
{
return new InnerClass();
}
public class InnerClass
{
public OuterClass OutC()
{
return new OuterClass();
}
public string PublicInner;
private string privateInner;
public InnerClass()
{
}
public void InnerClassFunction()
{
Console.WriteLine("I am in InnerClass");
}
void PrivateFunction_InnerClass()
{
Console.WriteLine("I am a Private function Of InnerClass");
}
}
}
}
________________________________________________________________________
For the Above example the Instance are named as abc(),OutC()
To Call an instance Methods from the main function:
static void Main(string[] args)
{
//Program Obj = new Program();
//Obj.ReadAssembly();
OuterClass.InnerClass Obj = new OuterClass.InnerClass();
Obj.OutC().OuterClassFunction();
Console.ReadLine();
}
__________________________________________________________________
Nested Class and Ordinary Class Explainations.
A nested class is declared in the same manner as a normal class declaration. 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.
No comments:
Post a Comment