- static(์ ์ ) ๋ฉ์๋์ ์ ์ ํ๋
- ํ ํ๋ก๊ทธ๋จ์์ ํด๋์ค๋ ํ๋๋ง ์กด์ฌํ์ง๋ง ์ธ์คํด์ค๋ ์ฌ๋ฌ๊ฐ๊ฐ ์กด์ฌ ํ ์ ์๋ค.
- ํ ํ๋ก๊ทธ๋จ์์ ๋๊ฐ์ ํด๋์ค๋ ๋๊ฐ๊ฐ ์กด์ฌํ ์ ์๋ค.
- static ํ๋๋ static ๋ฉ์๋๋ ํด๋์ค์ ์์๋ ๊ฒ์ด๋ฏ๋ก ํ๋ก๊ทธ๋จ์์ ์ ์ผํ๊ฒ ์กด์ฌํ๋ค.
- ์ธ์คํด์ค์ ํ๋๋ ๋ฉ์๋๋ ์ธ์คํด์ค๋ฅผ ํตํด ์ ๊ทผํ๋ฉด ๋๊ณ , static ํ๋์ ๋ฉ์๋์ ๊ฒฝ์ฐ์๋ ํด๋น ํด๋์ค์ ์ด๋ฆ์ ํตํด ์ ๊ทผํ๋ฉด ๋๋ค.
using System;
using System.Threading;
class Global
{
public static int Count = 0;
public static void ResetCount()
{
Count = 0;
}
}
class A
{
public A()
{
Global.Count++;
}
}
class B
{
public B()
{
Global.Count++;
}
}
class MainApp
{
static void Main(string[] args)
{
Console.WriteLine($"Global.Count : {Global.Count}");
new A();
new A();
new B();
new B();
Console.WriteLine($"Global.Count : {Global.Count}");
Global.ResetCount();
Console.WriteLine($"-----Reset-----");
Console.WriteLine($"Global.Count : {Global.Count}");
}
}
Output
Global.Count : 0
Global.Count : 4
-----Reset-----
Global.Count : 0
'๐ป Programming Language > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
06. ์ ๊ทผ์ ํ์(Access Modifier) (0) | 2022.04.06 |
---|---|
05. this, this() ์์ฑ์ (0) | 2022.04.06 |
04. ๊น์ ๋ณต์ฌ์ ์ดํด (Deep Copy) (0) | 2022.04.06 |
02. ์์ฑ์์ ์ข ๋ฃ์ (0) | 2022.04.06 |
01. ํด๋์ค์ ๊ฐ์ฒด (0) | 2022.04.05 |