Rayne Blackham
Rayne Blackham

Reputation: 119

C# is it a reference?

At the top of the class, in the field members, what is the name for Teacher teacher;

I know this allows us access to the Teacher class; its Properties and methods. I'd like to know if its called a reference or an instance of a class so I can read up a bit more to clarify my understanding of it.

Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace P3_O_O_P
{
    public class Classroom
    {
        private int roomNo;
        private string className;
        Teacher teacher;
        public List<Student> stuList = new List<Student>();

        public Classroom(int newRoomNo, string newRoomName)
        {
            roomNo = newRoomNo;
            className = newRoomName;
        }

        public Classroom()
        {
        }

        public void addStudent(string studentName, int studentID)
        {
            stuList.Add(new Student(studentName, studentID));
        }

        public void addTeacher(string teacherName, int teacherID)
        {
            teacher = new Teacher(teacherName, teacherID);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("\nTeacher name: " + teacherName + "\nTeacher ID: " + teacherID + "\nSystem updated!\n");
        }

        public void viewClass()
        {
            for (int i = 0; i < stuList.Count; i++)
            {
                Console.WriteLine(stuList[i].toString());
            }
            if (teacher != null)
            {
                Console.WriteLine(teacher.toString());
            }
        }

        public void removeTeacher()
        {
            teacher = null;
        }

        public string ClassName
        {
            get
            {
                return className;
            }
            set
            {
                className = value;
            }
        }

        public int ClassNumber
        {
            get
            {
                return roomNo;
            }
            set
            {
                roomNo = value;
            }
        }

        public void removeStudent(int id)
        {
            for (int i = 0; i < stuList.Count(); i++)
            {
                int ID = stuList[i].ID;
                if (id == ID)
                {
                    Console.WriteLine("\nStudent name: {0}\nStudent number: {1}\n\nStudent removed from classroom\n\nSystem updated!\n\n", stuList[i].Name, stuList[i].ID);

                    stuList.RemoveAt(i);
                }

            }
        }
    }
}

Upvotes: 1

Views: 142

Answers (3)

L&#225;szl&#243; Koller
L&#225;szl&#243; Koller

Reputation: 1159

teacher is a member variable of type Teacher and it is a reference type (not a value type). Once the teacher is instantiated, via the addTeacher() method, it will refer to a specific instance of the Teacher class.

Upvotes: 1

Over Killer
Over Killer

Reputation: 513

It creates a teacher which is Teacher type. By creating an object we can access to its public members. We are also say that we create an instance to a class.

Upvotes: 1

mason
mason

Reputation: 32694

Teacher is the name of the class. teacher is a reference to an instance of the teacher class.

Another way of thinking about it: teacher is a variable of type Teacher.

Upvotes: 2

Related Questions