2017-11-24 15 views
0

私はInvokeを使用していて、使用していないと混同しています。次の例ではInvokeを使用する場合と使用しない場合の違いは何ですか?

、私は誰もこれで私を助けることができる

int answer = b(10, 10); 

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

と何の違いを見ていませんか? thx!

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://でのstackoverflow。 com/questions/16309286/funct-vs-funct-invoke) – Backs

答えて

0

基本的に違いはありません。

具体的には、Delegateは関数ではなく、関数を保持する関数ではないため、関数を保持するクラスには何らかの方法で呼び出す必要があります。したがって、方法Invoke。一方、Delegateにカッコを追加すると、コンパイラは自動的にInvokeを呼び出すほどスマートです。 https://jacksondunstan.com/articles/3283を参照してください。

関連する問題