2017년 3월 27일 월요일

[탑크리에듀,국비지원과정,C#강좌] User-Defined Data Type(Enumeration, Struct)

이번 강좌에서는 User-Defined Type중에 Enumeration(열거) 형 대해 알아 보겠습니다. ------------------------------ User-Defined Data Types -------------------------- 1. Enumeration Type 정의 : enum Color { Red , Green , Blue } 사용법 : Color colorPalette = Color.Red; , Console.WriteLine(“{0}” , colorPalette); //display Red [예제] using System; namespace ConsoleApplication { enum Day { Monday, Thesday, Wednesday, Thursday, Friday, Saturday, Sunday } public class EnumTest { static void Main() { Day whatDay = Day.Monday; Console.WriteLine("{0}", whatDay); whatDay = (Day)1; Console.WriteLine("{0}", whatDay); } } } [결과] Monday Thesday 2. Structure Types 여러가지 형식의 자료들을 모아둔 틀이며 C#에서는 메소드가 없는 클래스 입니다. 구조체의 정의 방법 public struct Employee { String firstname; Int age; } 사용 방법 Employee companyEmployee; companyEmployee.firstname = “ Joe ” ; companyEmployee.age = 23; [예제] using System; struct Emp { public int id; public string name; } class EmpTest { public static void Main() { Emp e; e.id = 1; e.name = "1길동"; Console.WriteLine("사번: {0}",e.id); Console.WriteLine("이름: {0}",e.name); } } [결과] Monday Thesday

댓글 없음:

댓글 쓰기