포스트

C# 변수와 기본 타입

목차

  1. 기본형
  2. 변수 정의
  3. 클래스에서 변수 선언
  4. 메서드에서 변수 선언

기본형 (Type)

  • 기본 자료형(Built-in Types) : 개발자가 별도로 코드를 만들지 않아도 C# 언어에서 자체적으로 제공하는 데이터 형식을 의미
    • 정수형, 실수형, 문자, 불린형

정수형 기본 타입

image

기타 기본 타입

image


변수 정의

  • 값(Literal)을 보관하는 장소
  • 변수는 반드시 (기본)타입과 함께 선언한 후 사용
    • int a;
    • private string str;
    • public List<Student> students;
  • 필요한 위치에서 선언 가능
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example_3
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            a = 1;
            char c = 'A';
            string str = "abcd";
            bool con = true;
            Console.WriteLine("a = " + a);
            Console.WriteLine("c = " + c);
            Console.WriteLine("str = " + str);
            Console.WriteLine("con = " + con);
        }
    }
}

image

변수 구분

  • 접근 제한에 따른 변수 구분
    • 멤버 변수 (전역 변수, Global 변수) - 클래스에서 선언
    • 지역 변수 - 메서드에서 선언 (파라미터 포함)
  • 타입에 따른 변수 구분
    • 값 형식 변수(Value Type)
      • 기본 타입 중에 string만 제외하고 모두가 값 형식
      • 구조체 (struct)
    • 참조 형식 변수(Reference Type)
      • 기본 타입 중에 string과 기타 모든 타입(클래스)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example_3
{
    class Program
    {
        static int a = 3;
        static void Main(string[] args)
        {
            int a = 1;
            Console.WriteLine("a = " + a);
            Function1();
        }

        static private void Function1()
        {
            Console.WriteLine("a = " + a);
        }
    }
}

image

  • static int a = 3; : 클래스 블럭내에서 선언된 변수 👉 멤버 변수
  • int a = 1; : 메서드 블럭내에서 선언된 변수 👉 지역 변수
  • 멤버 변수와 지역 변수의 이름이 동일할 경우에는 지역 변수를 우선으로 한다.

클래스에서 변수 선언(멤버 변수)

  • 인스턴스 변수(instance variable)
    • High Frequency Heap이라 불리는 힙의 특별한 부분에 저장
    • 인스턴스변수.변수명으로 접근 가능
  • 스태틱 변수 (static variable)
    • High Frequency Heap이라 불리는 힙의 특별한 부분에 저장
    • 클래스명.변수명으로 접근만 허용
  • 라이프 사이클
    • 프로그램 시작 시 할당, 프로그램 종료 시 해제

메서드에서 변수 선언(지역 변수)

  • 스택 메모리 사용
    • 함수 내부에서만 사용 가능
    • 외부에서는 접근 불가능
  • 라이프 사이클
    • 함수 호출 시 할당, 함수 리턴 시 해제

멤버 변수와 지역 변수

멤버 변수 이용

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example_3
{
    class Program
    {
        static int a = 0;
        static void Main(string[] args)
        {
            a = 1;
            Console.WriteLine("a = " + a);
            Function1();
            Console.WriteLine("a = " + a);
        }

        static private void Function1()
        {
            a = 2;
        }
    }
}

image

지역 변수 이용

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example_3
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            Console.WriteLine("a = " + a);
            Console.WriteLine("a = "+ Function1(a));
            Console.WriteLine("a = " + a);
        }

        static private int Function1(int a)
        {
            a = 2;
            return a;
        }
    }
}

image

  • Function(a);에서는 지역 변수 a를 파라미터 값을 보낸 것이고, 이 메서드에 의해서 파라미터로써의 a값이 바뀌어 리턴되는 것일 뿐, Main메서드 내의 지역변수 값이 바뀌지 않는다.
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.