Error 메시지
error CS0120: An object reference is required for the non-static field, method, or property
발생 상황
class Program
{
    void TestMethod() 
    {    
        // Process
    }
    static void Main(string[] args)
    {
        TestMethod();
    }
}
TestMethod()를 진행하려고 하는데, 위와 같은 에러 메시지가 떴다.
해결 방법1
class Program
{
    static void TestMethod() 
    {    
        // Process
    }
    static void Main(string[] args)
    {
        TestMethod();
    }
}
말 그대로 non-static field인 메서드를 실행하려고 했기 때문에 났던 에러이다. static을 사용하면 인스턴스를 생성하지 않아도 사용할 수 있는 정적메서드가 된다. 메서드에 static을 붙여주면 해결 완료.
해결 방법2
class Program
{
    void TestMethod() 
    {    
        // Process
    }
    static void Main(string[] args)
    {
        Program p = new Program();
        p.TestMethod();
    }
}
static한 메서드로 사용하고 싶지 않다면, 해당 객체를 생성한 후 접근하면 해결 완료.
반응형
    
    
    
  'Basic > C#' 카테고리의 다른 글
| [c#] async / await 사용 예시 (0) | 2019.03.27 | 
|---|---|
| [C#] TCP/IP 프로토콜과 소켓 프로그래밍 (18) | 2019.03.18 | 
| C# 클래스 인스턴스 하나만 만드는 방법 (0) | 2016.12.14 | 
