| Printable Version
Inner Classes In C#
By Saju
Like Java, in C# also inner classes are possible. The program code
demonstrates the implementation of inner classes and structures
in classes. If we want to create an instance out side the class
the inner class and inner structure should be public. Unlike java
there will only one EXE is created, but in java more than one class
files will create. The following program is self explanatory
with proper output
//Here Demonstrating class ,innerclass and inner structure
//Demonstrating method overriding with abstract class
using System;
abstract class admin
{
abstract public void getdetails(string x,string y);
abstract public void showdetails();
}
//Declaring Main class
class student
{
public string std_code;
public string std_name;
public student()
{
// fcode="A090"; won't work because it is an inner class field
std_code=" ";
std_name=" ";
}
public void getdata(string str1,string str2)
{
std_code=str1;
std_name=str2;
}
public void show()
{
Console.WriteLine("Student Code {0} ",std_code);
Console.WriteLine("Student Name {0} ",std_name);
}
//Declaring inner structure Examination for storing examination details
public struct Examination
{
public string ExamCode;
public string ExamName;
public void setDetails(string str1,string str2)
{
ExamCode=str1;
ExamName=str2;
}
public string[] getdetails()
{
string []temp;
temp=new string[2];
temp[0]=ExamCode;
temp[1]=ExamName;
return temp;
}
}//End of inner structure Examination
//Declaring Inner class faculty
public class faculty : admin
{
public string fcode;
public string fname;
public faculty()
{
//std_code="";Won't work,Only Static member can be permitted
fcode=" ";
fname=" ";
}
public override void getdetails(string str1,string str2)
{
fcode=str1;
fname=str2;
}
public override void showdetails()
{
Console.WriteLine("\nFaculty Code {0} ",fcode);
Console.WriteLine("Faculty Name {0} ",fname);
}
} //End Of class faculty
}
//**********************************
class classTest
{
static void Main()
{
//Instantiating an object of type student
student xx=new student();
//Instantiating an object of type inner faculty
student.faculty yy=new student.faculty();
//Putting values into the public field of object type student
xx.getdata("S0089","Ajit");
//Displaying the contents of object of type student
xx.show();
//Evoking the function getdetails() of inner class faculty
//for putting values into the public fields
yy.getdetails("V0089","Vishnu Namboodiri");
//Displaying the components of innerclass faculty
yy.showdetails();
//creating the instance of inner structure Examination of
//class student
student.Examination ee=new student.Examination();
// putting values into inner structure Examination
// of class student
ee.setDetails("St9080","GVKM Tvm");
//Displaying the contents of inner structure Examination
//using foreach looping structure
foreach(string t in ee.getdetails())
{
Console.Write("{0} ",t);
}
//Overriding the student.faculty with abstract class admin
Console.WriteLine("\nOutput After Late Binding");
admin adm=new student.faculty();
adm.getdetails("P009","Pradeep Iyangar");
adm.showdetails();
}
}
//*******************************************************************
// Output of the above program
/*
Student Code S0089
Student Name Ajit
Faculty Code V0089
Faculty Name Vishnu Namboodiri
St9080 GVKM Tvm
Output After Late Binding
Faculty Code P009
Faculty Name Pradeep Iyangar
*/
|