C# 4.0 - Cairo Code Camp 2010

1,157 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,157
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
22
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

C# 4.0 - Cairo Code Camp 2010

  1. 1. Productivity in C# 4.0<br />1<br />
  2. 2. Mohammad Tayseer<br />2<br />
  3. 3. Productivity<br />3<br />
  4. 4. Productivity=Value<br />4<br />
  5. 5. 5<br />Partial Classes<br />Object-Oriented Programming<br />Generics<br />C#<br />Anonymous Types<br />Expression Trees<br />Lambda Expressions<br />Extension Methods<br />Nullable<br />LINQ<br />Delegates<br />
  6. 6. C# 4.0<br />6<br />
  7. 7. Dynamic capabilities<br />Optional and named arguments<br />Co-variant and contra-variant<br />Better COM interoperability<br />7<br />
  8. 8. Dynamic?<br />8<br />
  9. 9. int x = …;x.GetType(); // Safex.SomeMethod(); // Compilation // errordynamic d = x;d.GetType(); // Safed.SomeMethod(); // Dispatched // at runtime<br />9<br />
  10. 10. x.SomeMethod()<br />x has SomeMethod<br />x.TryInvokeMember(“SomeMethod”, …)<br />10<br />
  11. 11. interface IDynamicMetaObjectProvider<br />{<br />boolTryGetMember(…);<br />boolTrySetMember(…);<br />boolTryDeleteMember(…);<br /> <br />boolTryInvoke(…);<br />boolTryInvokeMember(…);<br />boolTryCreateInstance(…);<br /> <br />boolTryGetIndex(…);<br />boolTrySetIndex();<br />boolTryDeleteIndex(…);<br />… <br />}<br />11<br />
  12. 12. 12<br />IronPython<br />IronRuby<br />C#<br />VB.NET<br />Others…<br />Dynamic Language Runtime<br />PythonBinder<br />COMBinder<br />ObjectBinder<br />
  13. 13. C# 4.0 Dynamic<br />?<br />13<br />
  14. 14. C# 4.0<br />Static<br />Optional dynamic<br />14<br />
  15. 15. When?<br />15<br />
  16. 16. Productivity<br />16<br />
  17. 17. int x = …;dynamic d = x;d.SomeMethod(); // Runtime // error<br />17<br />
  18. 18. <br />18<br />
  19. 19. ((MyCompany.MyClient.MyProject.Workflow.Activities.SqlSelect)this.GetActivityByName(“SqlSelect1”)).RowsAffected > 1<br />19<br />
  20. 20. ((MyCompany.MyClient.MyProject.Workflow.Activities.SqlSelect)this.GetActivityByName(“SqlSelect1”)).RowsAffected > 1<br />20<br />
  21. 21. ((MyCompany.MyClient.MyProject.Workflow.Activities.SqlSelect)this.GetActivityByName(“SqlSelect1”)).RowsAffected > 1<br />21<br />
  22. 22. This is too complex<br />22<br />
  23. 23. this.SqlSelect1.RowsAffected > 1<br />23<br />Runtime Lookup<br />Recursive<br />
  24. 24. ((MyCompany.MyClient.MyProject.Workflow.Activities.SqlSelect)this.GetActivityByName(“SqlSelect1”)).RowsAffected > 1this.SqlSelect1.RowsAffected > 1 <br />24<br />
  25. 25. dynamic x = …;<br />x.GetType();<br />x.Property1 = x.Property2;<br />x[“1”] = x[“value 2”];<br />x(1, 2, 3)<br />x++;<br />x.ExtensionMethod() // Runtime error<br />25<br />
  26. 26. Dynamic DispatchDemo<br />26<br />
  27. 27. Named & optional parameters<br />27<br />
  28. 28. chart.ChartWizard(<br />range.CurrentRegion,<br />MissingValue,<br />MissingValue,<br />MissingValue,<br />MissingValue,<br />MissingValue,<br />MissingValue,<br /> "Memory Usage in " + Environment.MachineName,<br />MissingValue,<br />MissingValue,<br />MissingValue); <br />28<br />
  29. 29. <br />29<br />
  30. 30. chart.ChartWizard(<br />range.CurrentRegion, // Source<br />MissingValue, // Gallery<br />MissingValue, // Format<br />MissingValue, // PlotBy<br />MissingValue, // CategoryLabels<br />MissingValue, // SeriesLabels<br />MissingValue, // HasLegend<br /> // Title<br /> "Memory Usage in " + Environment.MachineName,<br />MissingValue, // CategoryTitle<br />MissingValue, // ValueTitle<br />MissingValue); // ExtraTitle<br />30<br />
  31. 31. Strongly-typednamed parameters<br />31<br />
  32. 32. chart.ChartWizard(<br /> Source: range.CurrentRegion,<br /> Gallery: MissingValue,<br /> Format: MissingValue,<br />PlotBy: MissingValue,<br />CategoryLabels: MissingValue,<br />SeriesLabels: MissingValue,<br />HasLegend: MissingValue,<br /> Title: "Memory Usage in " + Environment.MachineName,<br />CategoryTitle: MissingValue,<br />ValueTitle: MissingValue,<br />ExtraTitle: MissingValue);<br />32<br />
  33. 33. chart.ChartWizard(<br /> Source: range.CurrentRegion,<br /> Gallery: MissingValue,<br /> Format: MissingValue,<br />PlotBy: MissingValue,<br />CategoryLabels: MissingValue,<br />SeriesLabels: MissingValue,<br />HasLegend: MissingValue,<br /> Title: "Memory Usage in " + Environment.MachineName,<br />CategoryTitle: MissingValue,<br />ValueTitle: MissingValue,<br />ExtraTitle: MissingValue);<br />33<br />
  34. 34. <br />34<br />
  35. 35. Optional parameters<br />35<br />
  36. 36. chart.ChartWizard(<br /> Source: range.CurrentRegion,<br /> Gallery: MissingValue,<br /> Format: MissingValue,<br />PlotBy: MissingValue,<br />CategoryLabels: MissingValue,<br />SeriesLabels: MissingValue,<br />HasLegend: MissingValue,<br /> Title: "Memory Usage in " + Environment.MachineName,<br />CategoryTitle: MissingValue,<br />ValueTitle: MissingValue,<br />ExtraTitle: MissingValue);<br />36<br />
  37. 37. chart.ChartWizard(<br /> Source: range.CurrentRegion,<br /> Title: "Memory Usage in " + Environment.MachineName); <br />37<br />
  38. 38. public void ChartWizard(<br /> Source = null,<br /> Gallery = null,<br /> Format = null,<br />PlotBy = null,<br />CategoryLabels = null,<br />SeriesLabels = null,<br />HasLegend = null,<br /> Title = “”,<br />CategoryTitle = null,<br />ValueTitle = null,<br />ExtraTitle = null)<br />38<br />
  39. 39. Co-variance & Contra-variance<br />39<br />
  40. 40. Back to Basics<br />40<br />
  41. 41. Lion is-a AnimalCamel is-a Animal<br />41<br />
  42. 42. public static string CleanAnimal(Animal a)<br />{<br />…<br />}<br />CleanAnimal(camel);<br />CleanAnimal(lion);<br />42<br />
  43. 43. ThusStack<Lion>is-aStack<Animal><br />43<br />
  44. 44. No<br />44<br />
  45. 45. public static string CleanAll(Stack<Animal> animals)<br />{ … }<br />Stack<Lion> lions = <br /> { … };<br />CleanAll(lions);<br />45<br />
  46. 46. public static string CleanAll(Stack<Animal> animals)<br />{<br />animals.Push(new Camel());<br /> …<br />}<br />46<br />
  47. 47. Stack<Lion> is-notStack<Animal><br />47<br />
  48. 48. IEnumerable<Lion> should-beIEnumerable<Animal><br />48<br />
  49. 49. IEnumerable<Lion>,IEnumerator<Lion> are read-only<br />49<br />
  50. 50. Only in C# 4<br />50<br />
  51. 51. interface IEnumerator<out T><br />// T only in output position<br />{<br />T Current { get; }<br />}<br />51<br />
  52. 52. interface IEnumerable<out T><br />{<br />IEnumerator<T> GetEnumerator();<br />}<br />52<br />
  53. 53. Co-variance<br />53<br />
  54. 54. Now Contra-variance<br />54<br />
  55. 55. interface IPushable<in T><br />{<br />void Push(T element);<br />}<br />55<br />
  56. 56. IPushable<Animal>is-aIPushable<Lion><br />56<br />
  57. 57. IPushable<Lion> lions = animals;<br />lions.Push(new Lion());<br />IPushable<Camel> camels = animals;<br />camels.Push(new Camel());<br />57<br />
  58. 58. Limitations<br />58<br />
  59. 59. Interfaces<br />Delegates<br />Reference types <br />59<br />
  60. 60. Co-variance & Contra-varianceDemo<br />60<br />
  61. 61. BetterCOMInteroperability<br />61<br />
  62. 62. Dynamic<br />Optional & named params<br />No PIA<br />Omitting ref<br />62<br />
  63. 63. Demos<br />63<br />
  64. 64. ?<br />64<br />
  65. 65. twitter.com/m_tayseerhttp://spellcoder.com/blogs/tayseerm_tayseer82@yahoo.com<br />65<br />

×