Friday, April 24, 2015

Adding object

You don't need arrays as a beginner.

Use a List<Student> instead. E.g.

namespace ConsoleCS
{
    using System;
    using System.Collections.Generic;

    class Student
    {
        public string StudentName { get; set; }
        public int SemesterNumber { get; set; }

        public override string ToString()
        {
            return string.Format("{0} in the {1} semester.", this.StudentName, this.SemesterNumber);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>();

            Student student1 = new Student();
            student1.SemesterNumber = 1;
            student1.StudentName = "1Toch";
            students.Add(student1);

            Student student2 = new Student();
            student2.SemesterNumber = 99;
            student2.StudentName = "Stefan Hoffmann";
            students.Add(student2);

            foreach (Student student in students)
            {
                Console.WriteLine(student.ToString());
            }

            Console.WriteLine("\nDone.");
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment