c# object 객체 재활용해 보는 방법을 살펴봅니다.
이를 사용하기 위해선 static 클래스를 생성하는 것입니다.
using System; class BusinessLogic { private int Cnt = 0; public BusinessLogic() { the_object = this; } public static BusinessLogic get_object() { // 객체가 생성되지 않았으면, 생성한 후 객체를 반환한다. if(the_object == null) { the_object = new BusinessLogic(); } return the_object; } public int used_function() { //Console.WriteLine("Hello, OOP."); Cnt += 1; return Cnt; } private static BusinessLogic the_object; } public class MainClass { public void main_func() { // 위에서 작성한 get_object()를 이용하여 객체를 얻어온다. int cnt = BusinessLogic.get_object().used_function(); Console.WriteLine(cnt); } public static int Main(string[] args) { int cnt = 0; //instance 를 매번 발생한다. BusinessLogic biz = new BusinessLogic(); cnt = biz.used_function(); Console.WriteLine(cnt); //하나의 instance 만들어 재활용한다. cnt = BusinessLogic.get_object().used_function(); Console.WriteLine(cnt); System.Console.ReadKey(); return 0; } }
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.