반응형

C#을 하도 오랜만에 만지니 클래스랑 구조체랑, 각각의 리스트 용법이 헷갈린다...
연습삼아 한번 정리.

1. 클래스 선언.
-. 나는 ID/PW를 클래스로 관리하려고 한다.

        class Account
        {
            public string AccountID;
            public string AccountPW;

            public void AddAccount(string accountID, string accountPW)
            {
                this.AccountID = accountID;
                this.AccountPW = accountPW;
            }


        }

 

2. 클래스 객체 생성
-. 우선 하나만 생성해본다.

private void Button_Click(object sender, RoutedEventArgs e)
        {
            string accountID = "ID";
            string accountPW = "PW";

            Account tong = new Account();

            tong.AccountID = accountID;
            tong.AccountPW = accountPW;

            string dumString = String.Format("ID: {0}, PW: {1} ", tong.AccountID, tong.AccountPW);

            MessageBox.Show(dumString);


        }

-. 출력 결과

그림 1. 클래스 이용 스트링 출력


3. 클래스 리스트 생성
-. 요건 매번 헷갈리는거같다.
-. Account List 생성 이후 .add로 추가

            Accounts.Add(new Account() { AccountID = "A_ID", AccountPW = "A_PW" });
            Accounts.Add(new Account() { AccountID = "B_ID", AccountPW = "B_PW" });

            string line;
            string[] lines = new string[5];
            int i = 0;

            foreach (Account _account in Accounts) 
            { 
                line = String.Format("ID: {0}, PW: {1} ", _account.AccountID, _account.AccountPW);
                lines[i] = line;
                i++;

            }

            string dumstring = string.Join(Environment.NewLine, lines);

            MessageBox.Show(dumstring);

-. 각 line이 합쳐져서 출력 됨.

그림 2. class List 사용

 

728x90
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기