공부일자 : 2019-04
환경 : Visual Studio 2017
언어 : C#
공부는 유투브에 올라와있는 강좌를 참고하였다.
https://www.youtube.com/watch?v=gE1AEHPtMxE&t=321s
Customer라는 Class를 따로 만든 후, 본 Class에서 배열을 생성해보았다.
Customer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnimalShelter
{
public class Customer
{
public string FirstName;
public string LastName;
private DateTime _Birthday;
private int _Age;
private bool _IsQualifies; //false면 입양안되고 true면 입양됨.
public string Address;
public string Description;
public Customer(string firstName, string lastName, DateTime birthday)
{
this.FirstName = firstName;
this.LastName = lastName;
this._Birthday = birthday;
this._IsQualifies = Age >= 18;
//age가 18보다 크거나 같으면 true, 아니면 false.
}
public DateTime Birthday
{
get { return _Birthday; }
set
{
_Birthday = value;
_IsQualifies = Age >= 18;
}
}
//필드와 메서드의 장점을 잘 이용한 class의 멤버이다.
public int Age //속성이지만, 일반 field처럼 사용하면 됨
{
get { return DateTime.Now.Year - _Birthday.Year; }
}
public bool IsQualified
{
get
{
return _IsQualifies;
}
//set이 없으면 읽기전용. get으로 return한 것이 보호받을 수 있음.
}
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
}
}
본 코드에서 배열 생성.
Form1.cs 내부.
//클래스 사용하지 않고 배열 만들기.
Customer[] cusArray = new Customer[10];
cusArray[0] = new Customer("First", "Last", new DateTime(2000, 2, 15));
foreach(Customer cus in cusArray)
{
string fullName = cus.FullName;
}
//ArrayList Method를 이용하여 배열 만들기.
ArrayList cusArrayList = new ArrayList();
cusArrayList.Add(new Customer("First", "Last", new DateTime(2000, 2, 15)));
//List Method를 이용하여 배열 만들기.
List<Customer> cusList = new List<Customer>();
cusList.Add(new Customer("First", "Last", new DateTime(2000, 2, 15)));
코드가 돌아가는 과정을 보여주려는 것이 아니다.
다른 Class를 따로 만들어 놓고,
그 Class에 대한 Instance 배열을 생성하기 위한 방법.
3가지 방법으로 배열을 만들 수 있다.
3가지는 각자 다른 특성을 갖는다.
1. Class 사용 않고 배열 만들기
- 배열을 선언하며 Data Type 명시해야함.
- 배열의 크기를 처음 선언할 때 명시해야함.
2. ArrayList 메소드(컬렉션/Class)를 사용해 배열 만들기.
- 배열 선언시 따로 Data Type 명시하지 않음.
- 배열 선언시 따로 배열 크기 명시 않음.
- 배열에 원소를 집어넣을 때 Data Type이 동일하지 않아도 넣을 수 있음.
3. List 메소드(컬렉션/Class)를 사용해 배열 만들기.
- 배열 선언시 따로 Data Type 꼭 명시
- 배열 크기 선언 안함
- 선언한 Data Type으로만 배열 생성 가능.
'- C#' 카테고리의 다른 글
[DevExpress] GridControl 에서 RepositoryItem이 안보이는 현상 (1) | 2024.02.26 |
---|---|
[C#] Windows Form 폼 위에 다른 폼 띄우기. (Topmost, Owner, Show, ShowDialog, 모달, 모달리스) (0) | 2020.04.08 |
[C#] 메모장으로 저장, 저장한 메모장 파일 바로 열기. (0) | 2020.03.27 |
[C#] Windows Forms 'ArrayList' Class 사용하기 (using System.Collections) (0) | 2019.04.21 |