A class can be created inside of another class. A class
created inside of another is referred to as nested. To nest a class, simply
create it as you would any other. Here is an example of a class called Inside
that is nested in a class called Outside:

public class Outside
{
public class Inside
{
}
}

In the same way, you can nest as many classes as you wish in
another class and you can nest as many classes inside of other nested classes
if you judge it necessary. Just as you would manage any other class so can you
exercise control on a nested class. For example, you can declare all necessary
fields, properties, or methods in the nested class or in the nesting class. When
you create one class inside of another, there is no special programmatic
relationship between both classes: just because a class is nested
does not mean that the nested class has immediate access to the members of the
nesting class. They are two different classes and they can be used separately as
you judge it necessary.

The name of a nested class is not "visible"
outside of the nesting class. To access a nested class outside of the nesting
class, you must qualify the name of the nested anywhere you want to use it. For
example, if you want to declare an Inside variable somewhere in the program but
outside of Outside, you must qualify its name. Here is an example:

Because there is no programmatically privileged relationship
between a nested class and its "container" class, if you want to
access the nested class in the nesting class, you can use its static members. In
other words, if you want, you can declare static all members of the nested class
that you want to access in the nesting class. Here is an example:

In the same way, if you want to access the nesting class in
the nested class, you can go through the static members of the nesting class. To
do this, you can declare static all members of the nesting class that you want
to access in the nested class. Here is an example:

Instead of static members, if you want to access members of
a nested class in the nesting class, you can first declare a variable of the nested
class in the nesting class. In the same way, if you want to access members of a
nesting class in the nested class, you can first declare a variable of the
nesting class in the nested class. Here is an
example:

Just like any of the variables we have used so far,
you can make a class or a structure a member variable of another class. To use a
class in your own class, of course you must have that class. You can
use one of the classes already available in C# or you can first create your own class. Here is an example of a
class:

class Point
{
public short x;
public short y;
}

A field is a member variable created from another class
instead of a primitive type. To use one class as a member variable of another class, simply
declare its variable as you would proceed with any of the member variables
we have declared so far. Here is an example:

public class Point
{
public short x;
public short y;
}
public class CoordinateSystem
{
public Point Start;
}

After a class has been declared as a member variable of another class, it can be
used regularly. Because the member is a class, declared as a reference, there
are some rules you must follow to use it. After declaring the member variable,
you must make sure you have allocated memory for it. You must also
make sure that the variable is initialized appropriately before it can be used;
otherwise you would receive an error when compiling the program.

Practical
Learning: Using a Class as a Field

Start a new Console Application and name it ElectonicStore1

To create a new class, in the Solution Explorer, right-click the name of
the project, position the mouse on Add and click Class...

Like a value from a regular type, you can return a class value from a method of a
class. To do this, you can first declare the method and specify the
class as the return type. Here is an example:

public class Point
{
public short x;
public short y;
}
public class CoordinateSystem
{
private Point Start;
private Point End;
public PointGetThePoint()
{
}
}

After implementing the method, you must return a value that is conform to the
class, otherwise you would receive an error when compiling the application. You
can proceed by declaring a variable of the class in the body of the method,
initializing the variable, and then returning it. Here is an example:

public class Point
{
public short x;
public short y;
}
public class CoordinateSystem
{
private Point Start;
private Point End;
public Point GetThePoint()
{
Point pt = new Point();
Console.Write("Enter the x coordinate of the point: ");
pt.x = short.Parse(Console.ReadLine());
Console.Write("Enter the y coordinate of the point: ");
pt.y = short.Parse(Console.ReadLine());
return pt;
}
}

Once a method has returned a value of a class, the value can be used as
normally as possible.

Passing a Class as Argument

Once a class has been created, it can be used like any
other variable. For example, its variable can be passed as argument to a
method of another class. When a class is passed as argument, its public
members are available to the method that uses it. As done for the
arguments of primitive types, you can pass more than one class as argument to a
method. Here are different examples:

Start Point
Enter the x coordinate of the point: -2
Enter the y coordinate of the point: 2
End Point
Enter the x coordinate of the point: 3
Enter the y coordinate of the point: -6
Coordinate System
Starting Point: P(-2, 2)
Ending Point: Q(3, -6)
Distance Between Both Points: 9.43
Press any key to continue . . .

Because classes are always used as references,
when passing a class as argument, it is implied to be passed by reference.
To reinforce this, you can type the ref keyword to the left of the
argument. Here is an example: