language/C#

static이란

ETIT 2020. 7. 5. 21:37

Static 

 

Static method는 static member만 엑세스 할 수 있습니다.

Static method 반대 instance method

 

  • 정적 키워드로 선언한 메서드는 객체 인스턴스를 생성(new 인스턴스 이름)하면 힙 memory 공간 안에 객체가 생성됨
  • 값을 공유하기 위한 용도로 사용하기 위함

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace Day0705
{

	class Program
	{
		static void Main()
		{
			StatTest.StatPrn();	// 정적 메서드는 따로 객체 생성을 안해도 됨
			StatTest nonTest = new StatTest();
			nonTest.Prn();
		}
	}
	class StatTest
	{
		public static void StatPrn()
		{
			// Console.WriteLine is a static method.Console.Out is a static  
			// object that can get passed as a parameter to any method that takes a TextWriter,
			// and that method could call the non-static member method WriteLine.
			Console.Out.WriteLine("Static에서 작동하는 Method");
		}

		public void Prn()
		{
			Console.Out.WriteLine("non-Static에서 작동하는 Method");
		}
	}

}