본문 바로가기
C#/수업내용

[C# 7일차] 배열의 Class를 이용한 그룹화

by 왹져박사 2023. 1. 9.
728x90

App Class

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

namespace Study07
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App 생성자");
            Console.WriteLine();

            Student[] students = new Student[5];

            students[0] = new Student("학생0", 00000, 50);
            //Student student0 = new Student();
            //student0.id = 00000;
            //student0.name = "학생0";
            //student0.score = 50;
            students[1] = new Student("학생1", 00001, 80);
            students[2] = new Student("학생2", 00002, 100);
            students[3] = new Student("학생3", 00003, 60);
            students[4] = new Student("학생4", 00004, 90);

            //null 체크!
            for(int i = 0; i < students.Length; i++)
            {
                if (students[i] != null) 
                    Console.WriteLine("이름: {0}, 학번: {1}, 점수: {2}", students[i].name, students[i].id, students[i].score);
            }
        }
    }
}

Student Class

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

namespace Study07
{
    class Student
    {
        public string name;
        public int id;
        public int score;

        //생성자
        public Student(string name, int id, int score)
        {
            this.name = name;
            this.id = id;
            this.score = score;
        }
    }
}

 

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

namespace Study07
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}

 

실행 결과

728x90