In this article we will discuss about nesting of classes in C#.NET with examples.
“A nested class is one which is created inside another class.” A class which is created in another called referred as nested.
Syntax:
class OutsideClass
{
// members of
class InsideClass
{
// members of
}
}
|
Example:
class NestingOfClasses
{
public static void
{
Console.WriteLine(
OutsideClass
objOutside.
//InsideClass
OutsideClass.Insid
objInside.
OutsideClass.Insid
objInside1.
Console.Read();
}
}
class OutsideClass
{
// members of
public void
{
Console.WriteLine(
}
public class InsideCla
{
// members of
public void
{
Console.
}
public class Inne
{
// members
public void
{
Console.
}
}
}
}
|
Output:
|
Data points:
-
If we try to create object directly for nested classes we will get error as “The type or namespace name '
InsideClass' could not be found (are you missing a using directing or an assembly reference?)”. - By default nested class access modifiers is Private. So mark nested class with public otherwise we can’t access them in the program. We will get error as “’namespace.classname’ is inaccessible due to protection levels”
-
A nested class can have
private
,protected
andprotected internal
access modifiers along withpublic
andinternal where as normal should be public or internal.
- Nested classes can access outside class object's private members even if they are not static.
-
Nested class can access the static members
of the containing outer class without using that classes’ name. Whereas
for outer class to access static members of nested class then it should
be like NestedClassName.
staticdatamembername.
Accessing data members in nested/parent classes:
If we create data members in parent/nested class and try to access them in nested/parent class it is not possible.
class OutsideClass
{
public string strParent = "I
// We can’t access strNested in this class
class InsideClass
{
public string strNested
= "I am string strNested
from Inside class";
// We can’t access strParent in this class
}
}
|
So in order to access data members of parent/nested classes in nested/parent class we can do in two ways
1.
Marking the data members as “static” – see example1
2.
Creating objects in the classes – see example2
Example 1: Accessing using Data members as “static”:
class OutsideClass
{
// members of
public static string
public void
{
Console.WriteLine(
}
public class InsideCla
{
// members of
public static stri
public void
{
Console.
}
}
}
class NestingOfClasses
{
public static void
{
Console.WriteLine(
OutsideClass
objOutside.
OutsideClass.Insid
Console.Read();
}
}
|
Output:
|
Example2: Accessing data members using object creation:
class OutsideClass
{
// members of
public string
public void
{
InsideClass
Console.WriteLine(
}
public class InsideCla
{
// members of
public string
public void
{
OutsideClass
Console.
}
}
}
class NestingOfClasses
{
public static void
{
Console.WriteLine(
OutsideClass
objOutside.
OutsideClass.Insid
Console.Read();
}
}
|
Output:
|
Use of nesting classes:
- Data hiding can be achieved with nesting of classes. We can hide the members/details of inner class to the outside world.
- Easy way of logically grouping classes that are only used in one place.
0 Comments:
Post a Comment