티스토리 뷰

LANGUAGE/C#

[C#] 사용자 지정형

진심스테이크 2018. 6. 12. 23:05

사용자 지정형

1. struct

- 기본 형태

public struct 구조체명 

{

  //멤버, 속성, 매소드

}

namespace _0612
{
    public struct MyStruct
    {
        public const float PI = 3.14f;
        public static int Age = 12; 
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("{0} {1}", MyStruct.PI, MyStruct.Age);
        }
    }
}
 
namespace _0612
{
    public struct MyStruct
    {
        public int Age;

        //생성자 - 생성과 동시에 초기화
        public MyStruct(int inAge)
        {
            Age = inAge;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyStruct TestStruct1;
            TestStruct1.Age = 12;
            Console.WriteLine(TestStruct1.Age); //12

            MyStruct TestStruct2 = new MyStruct();
            Console.WriteLine(TestStruct2.Age); //0 : 세팅이 되어있지 않음

            MyStruct TestStruct3 = new MyStruct(12);
            Console.WriteLine("{0}", TestStruct3.Age); //12
        }
    }
}

 

2. enum

- 열거형 : 상수를 문자열로 대치하여 선언

- 기본 형태

  enum 열거형 명칭 { 문자열1, 문자열2 };

  enum 열거형 명칭 { 문자열1 = 상수, 문자열2 = 상수 };

  enum 열거형 명칭 { 문자열1 = 상수, 문자열2 };

- 기본은 int형이지만 char형을 제외한 형식을 지정할 수 있음

 ex) enum Days : byte { Sun = 0, Mon, Tue, Wed, Thu };

- 열거형 변수가 아닌 변수에 열거형 값을 대입할 때에는 데이터형을 명시해아 함

namespace _0612
{
    enum Days : byte { Sun = 1, Mon, Tue, Wed, Thu, Fri, Sat};
    //자동적으로 Mon = 2, Tue = 3, .... Sat = 7

    class Program
    {
        static void Main(String[] args)
        {
            int nValue = (byte)Days.Mon;
            Days day = Days.Tue;
            Console.WriteLine("{0} {1}", nValue, day); //2 Tue
        }
    }
}

 

3. class

4. interface

 

 

제한 사항

1. 구조체를 같은 구조체에 대입하게 되면 값이 복사됨

namespace _0612
{
    public struct MyStruct
    {
        public int Age;
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyStruct TestStruct1, TestStruct2;
            TestStruct2.Age = 10;
            TestStruct1 = TestStruct2; //값 복사
            Console.WriteLine("{0}", TestStruct1.Age); //10
        }
    }
}

 

2. 구조체는 값 형식이고 클래스는 참조 형식

namespace _0612
{
    public struct MyStruct
    {
        public int Age;
    }

    class MyClass
    {
        public int Age;
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyStruct test1 = new MyStruct();
            test1.Age = 12;
            MyStruct test2 = test1; //구조체 참조
            test2.Age = 24;
            Console.WriteLine("{0} {1}", test1.Age, test2.Age); //12 24

            MyClass test3 = new MyClass();
            test3.Age = 12;
            MyClass test4 = test3; //클래스 참조
            test4.Age = 24;
            Console.WriteLine("{0} {1}", test3.Age, test4.Age); //24 24
        }
    }
}

 

3. 구조체는 값 형식이므로 선언만으로도 사용 가능

- new를 사용했을 때만 -> 생성자가 호출 -> 기본값으로 초기화

namespace _0612
{
    public struct MyStruct
    {
        public int Age;
        public float Num2;
        public bool IsReady;
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyStruct test = new MyStruct();
            Console.WriteLine("{0} {1} {2}", test.IsReady, test.Age, test.Num2); //false 0 0
        }
    }
}

 

4. 구조치는 구조체 또는 클래스에 상속 할 수 없음

 

5. 구조체는 인터페이스를 상속하여 메소드를 구현할 수 있음

 

 


 

성적처리

namespace _0612
{
    public struct Sub
    {
        public int kor, eng, math, total;
        public float avg;

        public void Compute()
        {
            total = kor + eng + math;
            avg = total / 3.0f;
        }
    }

    class Program
    {
        static void Main(String[] args)
        {
            Sub s = new Sub();
            s.kor = 10;
            s.eng = 20;
            s.math = 30;
            s.Compute();
            Console.WriteLine("총점 : {0}, 평균 : {1}", s.total, s.avg);
        }
    }
}

 

'LANGUAGE > C#' 카테고리의 다른 글

[C#] 기본 문법  (0) 2018.06.13
[C#] 값 형식 / 참조 형식  (0) 2018.06.12
[C#] 표준 입력  (0) 2018.06.12
[C#] 변환  (0) 2018.06.12
[C#] 데이터형  (0) 2018.06.12
댓글