Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel www.sela.co.il 1 בס " ד.

Similar presentations


Presentation on theme: "© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel www.sela.co.il 1 בס " ד."— Presentation transcript:

1 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel www.sela.co.il 1 בס " ד

2 2 Dynamic lookup Named and optional parameters COM-specific interop features Variance

3 3 Dynamic objects are declared using the “dynamic” keyword When declaring a type as Dynamic there is no design time type checking And certainly no IntelliSence … Missing properties or methods will cause an exception at run time Calling a method on a dynamic object will always return a dynamic object The object can be a.NET object, a COM object, or even an html object

4 4 dynamic id = 123456789; dynamic name = "Miki Mouse"; dynamic students = new List () { "yossi", "dana", “noam" }; Console.WriteLine(++age); students.Add("shara"); foreach (dynamic item in students ) { Console.WriteLine(item); }

5 5 The dynamic keyword is NOT like the var keyword var is resolved as design time dynamic is only resolved at runtime var x = 10; x = "name"; dynamic y = 10; y = "name";

6 6 public static int Sum(params int[] values) { int sum = 0; for (int i = 0; i < values.Length; i++) { sum += values[i]; } return sum; } public static double Sum(params double[] values) { double sum = 0; for (int i = 0; i < values.Length; i++) { sum += values[i]; } return sum; }

7 7 public static dynamic Sum(params dynamic[] values) { dynamic sum = values[0]; for (int i = 1; i < values.Length; i++) { sum += values[i]; } return sum; } Console.WriteLine(Sum(2, 3, 4)); Console.WriteLine(Sum("a", "b", "c")); Console.WriteLine(Sum(DateTime.Now, new TimeSpan(3, 3, 3)));

8 8

9 9 Python Binder Ruby Binder COM Binder JavaScript Binder Object Binder Dynamic Language Runtime Expression Trees Dynamic Dispatch Call Site Caching IronPythonIronPythonIronRubyIronRubyC#C#VB.NETVB.NETOthers…Others…

10 10 static void Main(string[] args) { int i = 10; object o = new object(); dynamic i1 = i; dynamic o1 = o; }

11 11

12 12

13 13

14 14 namespace Microsoft.CSharp.RuntimeBinder { public class CSharpSetMemberBinder { } namespace ProxyBinder { public class ProxyCSharpSetMemberBinder : CSharpSetMemberBinder { } } namespace Microsoft.CSharp.RuntimeBinder { public class CSharpSetMemberBinder : ProxyCSharpSetMemberBinder { } }

15 15 Access to members is limited by Accessibility level (no access to private members) No access to static members or extension methods

16 16 public static class Math { public static decimal Abs(decimal value); public static double Abs(double value); public static float Abs(float value); public static int Abs(int value); public static long Abs(long value); public static sbyte Abs(sbyte value); public static short Abs(short value); } public class Persons { public void Add(string name, int id, float age); public void Add(string name, int id); public void Add(string name); }

17 17 Default value can be a Constant, null or default Ctor of Struct Must (officially) appear after all required parameters static void Add(int id, string name = "", float age = 0) { }

18 18

19 19 static void Add(int id, string name = "", float age = 0) {} static void Add(int id, [Optional, DefaultParameterValue("")] string name, [Optional, DefaultParameterValue(0)] float age) {} static void Add(DateTime dt1 = new DateTime(), DateTime dt2 = default(DateTime)) {} static void Add([Optional]DateTime dt1, [Optional]DateTime td2) {}

20 20

21 21 static void Main(string[] args) { Add(); } static void Add(string name = "shlomo") { }

22 22 static void Main(string[] args) { Add("shlomo"); } static void Add( [Optional, DefaultParameterValue("shlomo")] string name) { }

23 23 static void Add(int id, string name = "shlomo", float age = 0) { } static void Main() { Add(22,, 24); } static void Main() { Add(22, age: 24); }

24 24 Advantages: – Named parameters can be used even for non-optional parameters – It can makes the method call clearer Disadvantages: – You cannot change the names of parameters in public methods – The order of sent parameters can change the result

25 25

26 26 Covariance: delegate object CovarianceDelegate(); static Person CovarianceMethod(){ } CovarianceDelegate cd = CovarianceMethod; Contravariance: delegate void ContravarianceDelegate(Person obj); static void ContravarianceMethod(object obj) ContravarianceDelegate cva = ContravarianceMethod;

27 27 void PrintAllItems(List list) { foreach (var item in list) { Console.WriteLine(item); } void f() { List list = new List () { "name", "abcde", "1234" }; PrintAllItems(list); } void PrintAllItems(IEnumerable list)

28 28 class Person : IComparer { public int Compare(Person x, Person y) { } } class Employee : Person { } IComparer comparer = new Person();

29 29 static Action GetAction() { } Action action = GetAction();

30 30 public interface IEnumerable : IEnumerable { IEnumerator GetEnumerator(); } public interface IComparer { int Compare(T x, T y); } public delegate void Action (T obj);

31 31 My Blog (in Hebrew) http://blogs.microsoft.co.il/blogs/shlomo/ C# 4.0 Post Collection http://blogs.microsoft.co.il/blogs/shlomo/archive/2009/07/0 2/c-4-0-table-of-content.aspx PDC 2008 : Future of C# session. http://channel9.msdn.com/pdc2008/TL16/

32 © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel www.sela.co.il 32


Download ppt "© Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel www.sela.co.il 1 בס " ד."

Similar presentations


Ads by Google