본문 바로가기
Common /C#

참조예제와 ICloneable

by 언덕너머에 2014. 5. 8.

suing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RefTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int[] scores = new int[5];

            SetByte(scores);

            for (int x = 0; x < scores.Length; x++)
            {
                textBox1.Text += scores[x].ToString() + Environment.NewLine;
            }
            /* 출력값
            1
            2
            3
            4
            5
             */
        }

        private void SetByte(int[] byteParm)
        {
            for (int x = 0; x < byteParm.Length; x++)
            {
                byteParm[x] = (int)x;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Person person1 = new Person();
            person1.name = "홍길동";

             Person person2 = person1;
            person2.name = "김환철";
             person2.age = 41;

            Person person3 = (Person)person1.Clone();
            person3.name = "둘리";
             person3.age = 30;

            textBox1.Text = person1.name + Environment.NewLine;
            textBox1.Text += person2.name + Environment.NewLine;
            textBox1.Text += person3.name;
            /* 출력값
            김환철
            김환철
            둘리
            */
        }

        class Person : ICloneable
        {
            public string name = "이상용";
            public int age = 24;

            public object Clone()
            {
                Person newPerson = new Person();
                newPerson.name = this.name;
                newPerson.age = this.age;

                return newPerson;
            }
        }
    }
}

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

String.Split에서 \r\n 처리 방법  (0) 2014.05.30
Enumerable Class 1 : Enumerable Method - Aggregate  (0) 2014.05.22
Enumerable Class 0  (0) 2014.05.22
C#을 이용한 NTP Service  (0) 2014.05.17