Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

건축과컴퓨터

[C# - basic] struct, class 프리뷰 2 본문

C# for Grasshopper

[C# - basic] struct, class 프리뷰 2

2021. 1. 9. 18:44

이전 글 : 2021/01/09 - [C# for Grasshopper] - [C# - basic] struct, class 프리뷰 1


그러면 Student 클래스를 활용하여 재시험을 봐야 하는 학생을 뽑아내는 문제에 다시 접근해보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
 
namespace FirstTest
{
    class Program
    {
        class Student
        {
            // property
            public string Name;
            public int Score;
 
            // constructor
            public Student(string n, int s)
            {
                Name = n;
                Score = s;
            }
        }
 
        static void Main(string[] args)
        {
            // initialize
            List<Student> students = new List<Student> 
                {
                    new Student("Alice"99),
                    new Student("Betty"13),
                    new Student("Charlie"66),
                    new Student("David"82),
                    new Student("Emily"3),
                };
 
            List<Student> examAgainStudentList = new List<Student>();
 
            // find students with score equal to or lower than 50
            for (int i = 0; i < students.Count; i++)
            {
                if (students[i].Score <= 50)
                    examAgainStudentList.Add(students[i]);
            }
 
            // print name of students who need to take exam again
            for (int i = 0; i < examAgainStudentList.Count; i++)
            {
                Console.WriteLine(examAgainStudentList[i].Name);
            }
 
            Console.ReadLine();
        }
    }
}
cs

 

코드가 길지만, 8~20은 앞 글에서 만든 클래스 정의고, 뒷 부분은 튜플을 설명한 글에서의 코드와 상당히 유사하므로 직접 읽어보는 데에 무리가 없을 것이라 생각한다. 튜플로 설명할 때는 Item1, Item2와 같은 이름으로 점수와 이름을 받아왔었지만, 여기에서는 우리가 직접 지정한 속성 이름인 Score, Name을 사용하고 있음을 볼 수 있다.


다음 글 : 2021/01/10 - [C# for Grasshopper] - [C# - basic] struct, class 프리뷰 3 - 속성

Comments