public const string school1 = “seneca”;

public const string school2 = “toronto”;

public const string school3 = “humber”;

public const string school4 = “sheriden”;

위와 같은 상수값 (const 변수)를 비슷한것끼리 묶어서 아래처럼 오브젝트로 사용

enum School

{

    seneca,

    toronto,

    humber,

    sheriden

}

첫번째 상수부터 디폴트 값으로 0을 갖고 순차적으로 증가

using System;

namespace ExStruct
{
    class Example
	{
		private int num;
		private string str;
		public void SetNum(int num)
		{
			this.num = num;
		}
		public void SetStr(string str)
		{
			this.str = str;
		}
		
		public void Print()
		{
			Console.WriteLine("num: " + this.num);
		}
		
		public void PrintStr()
		{
			Console.WriteLine("str: " + this.str);
		}
	}
	
	enum School
	{
		seneca,  // 0
		toronto,  // 1
		humber = 20,  // 20
		sheriden  // 21
	}
	
	public class Program
	{
		// execute
		public static void Main(string[] args)
		{
			Example ex = new Example();
			School sc = School.sheriden;
						
            // int형으로 변환 (int)sc            
			ex.SetNum((int)sc);
			ex.Print();
			
			School sc2 = School.seneca;
            
            // string형으로 변환 ToString()
			string scStr = sc2.ToString();
			ex.SetStr(scStr);
			ex.PrintStr();
			
		}
	}
}
/* num: 21     str: seneca */

+ Recent posts