Allan
Allan

Reputation: 21

C# Class reference into another Class but Why?

i am beginner in c# i did't understand why put class reference into another class, here is the code.

  interface IDB
{
    void AddDoctor(Doctor doc);
    void AddPatient(Patient pat);
    void AddWard(Ward ward);
}

    class DataBaseManager 
{
    private IDB db;

    public DataBaseManager(IDB dbase)
    {
        this.db = dbase;
    }


class ComputerOperator
{
    private string name;
    private int age;
    private DataBaseManager dbm;

    public ComputerOperator(string name, int age,SQLDB sqdata)
    {
        this.name = name;
        this.age = age;
        dbm = new DataBaseManager(sqdata);
    }

    class BillingDepartment
    {
    private ComputerOperator operater;

    public ComputerOperator Operater
    {
        get { return operater; }
        set { operater = value; }
    }
   }

When we put the class into another class is called NESTED CLASSES, then me what is the name of this thing in c# "PUT THE CLASS REFERENCE INTO ANOTHER CLASS."

Edited to add sir, i am asking about why we need "a class reference into another class", and is that any alternate if we dont put a class reference into another class??? for example,

      namespace myNameSpace                                                                                                                                                                                                                                        
      {
       public class class1
      {
        //methods
      }

      public class class2
     { 
      private class1 myclass1;
       //methods
     }

Upvotes: 0

Views: 2484

Answers (2)

Miguel Angelo
Miguel Angelo

Reputation: 24202

A reference to another class is a different concept of a nested class.

  • Reference: is a field or property that holds an instance of another class.
  • Nested class: is a class declaration that exists inside another class declaration.

Nested classes are mainly used when you want to hide the existence of that class from the world. A nested private class, can only be used by the class that owns it.

Also, nested classes know all the private things that exists in the owner class.

Upvotes: 1

Colin Mackay
Colin Mackay

Reputation: 19185

I'm guessing what is meant (because it isn't very clear from the context or your wording) that you have a field containing a reference to another instance of a class. It has a reference to an object whose type is that of the referenced class.

public class A
{
    public class Nested
    {
    }

    public Nested _nestedFieldRefrence;
    public B _otherClass;
}

public class B
{
}

In class A you have * A nested class called Nested * A reference to an instance of the nested class called _nestedFieldReference * A reference to another class called _otherClass

Does that help your understanding?

Upvotes: 0

Related Questions