2017-11-24 2 views
0

Invoke를 사용하고 사용하지 않는 것과 혼동합니다. 다음 예에서Invoke를 사용하는 것과 사용하지 않는 것의 차이점은 무엇입니까?

, 나는

int answer = b(10, 10); 

int answer = b.Invoke(10, 10); 

와 차이가 너무 사람이 나를 도울 수있는 참조? !

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

namespace TEST 
{ 
    public delegate int BinaryOp(int x, int y); 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Main innvoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); 
      BinaryOp b = new BinaryOp(add); 
      //int answer = b(10, 10);  
      int answer = b.Invoke(10, 10); 

      Console.WriteLine("Doing more work in Main"); 
      Console.WriteLine("10 + 10 is {0}", answer); 
      Console.Read(); 
     } 

     static int add(int x, int y) 
     { 
      Console.WriteLine("add innvoked on thread {0}.", Thread.CurrentThread.ManagedThreadId); 
      return x + y; 
     } 
    } 
} 
+0

이 게시물을 참조 : 의 – K0D3R

+0

가능한 복제 [Func을 .Invoke 대 Func을 ()() (https : //로 유래. co.kr/questions/16309286/funct-vs-funct-invoke) – Backs

답변

0

기본적으로 차이는 없습니다.

특히 Delegate은 실제로 함수의 소유자가 아니기 때문에 함수를 보유하고있는 클래스는 호출 할 수있는 방법이 있어야합니다. 따라서 방법은 Invoke입니다. 반면 컴파일러는 Delegate에 괄호를 추가하면 Invoke을 자동으로 호출 할만큼 똑똑합니다. https://jacksondunstan.com/articles/3283을 참조하십시오.

관련 문제