C# static 키워드: 정적 변수, 메서드, 클래스 완벽 활용
C# static 키워드
이 가이드는 C# static 키워드를 정적 변수, 메서드, 클래스 관점에서 명확한 실전 예제와 함께 설명합니다.
C#에서 멤버에 static 키워드 적용 시 해당 타입의 모든 객체가 공유하는 단일 인스턴스 생성. 각 인스턴스별 복사본 대신 타입당 하나만 존재.
C# 정적 변수 (Static Variables)
필드에 static 선언 시 객체 생성 없이 클래스 이름으로 직접 접근 가능.
using System;
namespace StaticKeyword {
class Student {
// 모든 학생이 공유하는 정적 변수
public static string department = "Computer Science";
}
class Program {
static void Main(string[] args) {
// 클래스 이름으로 정적 필드 접근
Console.WriteLine("Department: " + Student.department);
Console.ReadLine();
}
}
}
출력
Department: Computer Science
department 필드는 모든 Student 인스턴스 공유, Student.department로 참조.
정적 변수 vs 인스턴스 변수
인스턴스 변수는 개별 객체 소속, 각자 복사본 보유. 예:
class Student {
public string studentName;
}
class Program {
static void Main(string[] args) {
Student s1 = new Student();
Student s2 = new Student();
}
}
s1, s2 별도 studentName 필드 가짐. 반면 정적 필드는 모든 인스턴스 공통, 객체 생성 없이 접근 가능.
예제: 정적 vs 인스턴스 변수
using System;
namespace StaticKeyword {
class Student {
public static string schoolName = "Programiz School"; // 공유
public string studentName; // 고유
}
class Program {
static void Main(string[] args) {
Student s1 = new Student();
s1.studentName = "Ram";
Console.WriteLine("Name: " + s1.studentName); // 인스턴스
Console.WriteLine("School: " + Student.schoolName); // 정적
Student s2 = new Student();
s2.studentName = "Shyam";
Console.WriteLine("Name: " + s2.studentName);
Console.WriteLine("School: " + Student.schoolName);
Console.ReadLine();
}
}
}
출력
Name: Ram
School: Programiz School
Name: Shyam
School: Programiz School
정적 필드 schoolName은 모든 학생 동일, 메모리 효율적이고 논리적으로 일관.
C# 정적 메서드 (Static Methods)
정적 메서드는 인스턴스가 아닌 클래스 자체에서 호출. 다른 정적 멤버만 접근 가능.
class Test {
public static void Display() {
// 구현
}
}
class Program {
static void Main(string[] args) {
Test.Display();
}
}
Test 모든 객체가 같은 Display 메서드 공유, 클래스 이름으로 호출.
예제: 정적/비정적 메서드
using System;
namespace StaticKeyword {
class Test {
public void Display1() {
Console.WriteLine("Non static method");
}
public static void Display2() {
Console.WriteLine("Static method");
}
}
class Program {
static void Main(string[] args) {
Test t1 = new Test();
t1.Display1(); // 인스턴스 호출
Test.Display2(); // 정적 호출
Console.ReadLine();
}
}
}
출력
Non static method
Static method
참고: Main 메서드 자체가 정적이라 인스턴스 없이 실행 가능.
정적 클래스 (Static Classes)
정적 클래스는 인스턴스화 불가, 정적 멤버만 포함. 도우미 메서드 그룹화에 유용.
using System;
namespace StaticKeyword {
static class Test {
static int a = 5;
static void Display() {
Console.WriteLine("Static method");
}
static void Main(string[] args) {
// Test t1 = new Test(); // ❌ 컴파일 에러
Console.WriteLine(a);
Display();
}
}
}
Test 인스턴스화 시 CS0723, CS0712 에러로 정적 성격 확인.
정적 클래스 상속 불가. 예:
static class A { /* ... */ }
// class B : A { /* ... */ } // ❌ 에러
클래스 내부에서 정적 멤버 접근
같은 클래스 내부에서는 정적 필드/메서드 클래스 한정자 없이 직접 참조 가능.
using System;
namespace StaticKeyword {
class Test {
static int age = 25;
public static void Display() {
Console.WriteLine("Static method");
}
static void Main(string[] args) {
Console.WriteLine(age); // 직접 접근
Display(); // 직접 접근
Console.ReadLine();
}
}
}
출력
25
Static method
age와 Display를 Test. 접두사 없이 사용.