How will you use the C# 4 dynamic type?

后端 未结 6 1812
余生分开走
余生分开走 2020-12-29 05:33

C# 4 will contain a new dynamic keyword that will bring dynamic language features into C#.

How do you plan to use it in your own code, what pattern woul

6条回答
  •  灰色年华
    2020-12-29 06:01

    This will also allow us to avoid having to use the visitor pattern in certain cases as multi-dispatch will now be possible

    public class MySpecialFunctions
    {
      public void Execute(int x) {...}
      public void Execute(string x) {...}
      public void Execute(long x) {...}
    }
    
    dynamic x = getx();
    var myFunc = new MySpecialFunctions();
    myFunc.Execute(x);
    

    ...will call the best method match at runtime, instead of being worked out at compile time

提交回复
热议问题