카테고리 없음

C# 구조체

Crony 2014. 12. 23. 14:49

Revit API를 배우기 위한 C#의 기초 여덟번째 시간

구조체 사용하는 방법


// ──────────────────────────────
//          08 C# 구조체
// ──────────────────────────────
		// 구조체 예문
        static void Main(string[] args)
        {
            Draw(10, 10, 100, 100);
        }
        static void Draw(int left, int top, int height, int width)
        {
            // Draw rectangle
            Print(left, top, height, width);
        }
        static void Print(int left, int top, int height, int width)
        {
            Console.WriteLine("Point: left={0}, top={1}", left, top);
            Console.WriteLine("Size: height={0}, Width={1}", height, top);
        }
    // 구조체
    struct Rect
    {
        public int Left;
        public int Top;
        public int Height;
        public int Width;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Rect r = new Rect();
            r.Left = 10;
            r.Top = 10;
            r.Height = 100;
            r.Width = 100;
            Draw(r);
        }
        static void Draw(Rect r)
        {
            // Draw rectangle
            Print(r);
        }
        static void Print(Rect r)
        {
            Console.WriteLine("Point: left={0}, top={1}", r.Left, r.Top);
            Console.WriteLine("Size: height={0}, Width={1}", r.Height, r.Width);

        }
    }
    struct Rect
    {
        public int Left;
        public int Top;
        public int Height;
        public int Width;
        public Rect(int left, int top, int height, int width)
        {
            Left = left;
            Top = top;
            Height = height;
            Width = width;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Rect r = new Rect(10, 10, 100, 100);
            Draw(r);
        }
        static void Draw(Rect r)
        {
            // Draw rectangle
            Print(r);
        }
        static void Print(Rect r)
        {
            Console.WriteLine("Point: left={0}, top={1}", r.Left, r.Top);
            Console.WriteLine("Size: height={0}, Width={1}", r.Height, r.Width);

        }
    }

객체에 해당되는 특성을 구조체로 만들어서 사용하시면 코딩이 더욱 간단하게 구성이 될수 있습니다.