티스토리 뷰

C#

7. get set

LDobac 2016. 5. 7. 19:51

 

 

이번엔는 get,set 에 대해 알아 보겟습니다

클래스 내부에서 변수를 만들 때 public으로 하면 변수가 막 바뀔까 봐 문제되고 private로 하면 바꿀 때 귀찮아 지는 경우가 있는데 이럴 때 get,set을 이용하면 됩니다

쓰는 방법은

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

   

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

  Man man = new Man();

man.Name = "느앙";

        }

    }

   

    class Man

    {

        private string name = "끄앙";

   

        public string Name

        {

            get

            {

                return name;

            }

            set

            {

                name = value;

            }

        }

    }

}

   

   

   

Colored by Color Scripter

cs

이런 형식으로 사용 하면 됩니다

Get,set이 있는 곳을 잘 보면 함수 같이 생겼는데 함수는 아닌 듯 합니다.

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

9. 구조체(Structures)  (0) 2016.06.30
8. 확장 메소드(Extension Method)  (0) 2016.06.29
6. sealed  (0) 2016.05.07
5. 배열  (0) 2016.05.07
4. params  (0) 2016.05.07
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함