2009년 10월 07일
[C#] Class 내의 static 변수
Class 내에서 static으로 선언된 변수의 값은, 그 클래스를 여러번 호출할 경우에도 유지된다.
아래의 간단한 예제를 통해서 확인할 수 있다.
Visual C# 2008의 ConsoleApplication 응용 프로그램으로 생성되었으며,
main entry class Program() 내에서 test() 클래스를 두 번, 즉 t1, t2 를 두 번 선언하였다.
test class 내에는 static int 변수 m이 0으로 초기화 되며,
생성자가 호출될 때마다 m 값은 1씩 증가한다.
처음 t1 을 선언한 후에는 t1.m은 1이 되며, 두 번째 t2 를 선언한 후에는 t2.m은 2가 된다.
==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class test
{
static int m=0;
public int M
{ get { return m; } }
public test()
{
m++;
}
}
class Program
{
static void Main(string[] args)
{
test t1 = new test();
Console.WriteLine("t1 = {0}", t1.M);
test t2 = new test();
Console.WriteLine("t2 = {0}", t2.M);
Console.ReadKey();
}
}
}
=====================================================

이 글과 관련있는 글을 자동검색한 결과입니다 [?]
- [C#] 클래스를 배열로 선언하기 by 헤이즐넛
- [C언어] static으로 선언된 글로벌/로컬 변수 by Kyle
- JAVA공부 시작!!2009년 9월 27일 두번째 by How2create
- 비트부정 연산자 by groove
- 왜 main 메소드는 static일까. by 밀리네스
# by | 2009/10/07 09:02 | 프로그래밍 | 트랙백 | 덧글(0)





☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]