What’s New in C# 4.0<br />Eyal Vardi<br />CEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com<br />
Agenda<br />Dynamically Typed Objects<br />Optional & Named Parameters<br />Co- and Contra-variance<br />
The Evolution of C#<br />Dynamic<br />C# 4.0<br />LINQ<br />C# 3.0<br />C# 2.0<br />Generics<br />C# 1.0<br />Managed Code...
Dynamically Typed Objects<br />Calculator calc = GetCalculator();<br />int sum = calc.Add(10, 20);<br />object calc   = Ge...
The Dynamic Type<br />Resolved only at runtime.<br />object 	 dynamic<br />Any object can be implicitlyconverted to dynam...
Dynamic<br />
Optional & Named Parameters<br />
Improved COM Interoperability<br />objectfileName = "Test.docx";<br />object missing  = System.Reflection.Missing.Value;<b...
Optional & Named Parameters<br />Primary method<br />publicStreamReaderOpenTextFile(<br />    string   path,<br />    Enco...
Optional & Named Parameters<br />Optional parameters<br />publicStreamReaderOpenTextFile(<br />    string   path,<br />   ...
Optional & Named Parameters<br />
Co- & Contra- Variance<br />
Covariance (Out)<br />Covariance (Out):Is the ability to use a more derived type than that specified.<br />delegateAnimal ...
Contra Variance (In)<br />Contra variance (in):Is the ability to use a less derived type<br />delegatevoidMyDel(  Dogdog);...
Covariance & Generic<br />What you think?<br />IList<string> strings = new List<string>();<br />IList<object> objects = st...
publicinterfaceIEnumerable<T><br />{<br />IEnumerator<T> GetEnumerator();<br />}<br />publicinterfaceIEnumerable<out T><br...
in= Contra-variantInput positions only<br />publicinterfaceIComparer<T><br />{<br />int Compare(T x, T y);<br />}<br />pub...
Variance<br />
Summary<br />Dynamically Typed Objects<br />Optional and Named Parameters<br />Co- and Contra-variance<br />
What\'s New in C# 4.0
Upcoming SlideShare
Loading in...5
×

What\'s New in C# 4.0

1,115

Published on

Microsoft does it again! With .Net Framework 4 Microsoft takes the C# language to new levels. In this session we will learn how to write better code using Dynamically Typed Objects, Optional &amp; Named Parameters and Co-variance and Contra-variance.

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,115
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

What\'s New in C# 4.0

  1. 1. What’s New in C# 4.0<br />Eyal Vardi<br />CEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com<br />
  2. 2. Agenda<br />Dynamically Typed Objects<br />Optional & Named Parameters<br />Co- and Contra-variance<br />
  3. 3. The Evolution of C#<br />Dynamic<br />C# 4.0<br />LINQ<br />C# 3.0<br />C# 2.0<br />Generics<br />C# 1.0<br />Managed Code<br />
  4. 4. Dynamically Typed Objects<br />Calculator calc = GetCalculator();<br />int sum = calc.Add(10, 20);<br />object calc = GetCalculator();<br />TypecalcType = calc.GetType();<br />object res = calcType.InvokeMember("Add",<br />BindingFlags.InvokeMethod, null,<br />newobject[] { 10, 20 } );<br />int sum = Convert.ToInt32(res);<br />Statically typed to be dynamic<br />dynamic calc = GetCalculator();<br />int sum = calc.Add(10, 20);<br />Dynamic method invocation<br />Dynamic conversion<br />
  5. 5. The Dynamic Type<br />Resolved only at runtime.<br />object  dynamic<br />Any object can be implicitlyconverted to dynamic.<br />dynamic  object<br />Any dynamic Type can be assignment conversion to any other type. <br />dynamic x = 1; // implicit conversion<br />int num = x; // assignment conversion<br />
  6. 6. Dynamic<br />
  7. 7. Optional & Named Parameters<br />
  8. 8. Improved COM Interoperability<br />objectfileName = "Test.docx";<br />object missing = System.Reflection.Missing.Value;<br />doc.SaveAs(reffileName,<br />ref missing, ref missing, ref missing,<br />ref missing, ref missing, ref missing,<br />ref missing, ref missing, ref missing,<br />ref missing, ref missing, ref missing,<br />ref missing, ref missing, ref missing);<br />doc.SaveAs("Test.docx");<br />
  9. 9. Optional & Named Parameters<br />Primary method<br />publicStreamReaderOpenTextFile(<br /> string path,<br /> Encodingencoding,<br />booldetectEncoding,<br />intbufferSize);<br />Secondary overloads<br />publicStreamReaderOpenTextFile(<br /> string path,<br /> Encodingencoding,<br />booldetectEncoding);<br />publicStreamReaderOpenTextFile(<br /> string path,<br /> Encodingencoding);<br />publicStreamReaderOpenTextFile(<br /> string path);<br />Call primary with default values<br />
  10. 10. Optional & Named Parameters<br />Optional parameters<br />publicStreamReaderOpenTextFile(<br /> string path,<br /> Encodingencoding,<br />booldetectEncoding,<br />intbufferSize);<br />publicStreamReaderOpenTextFile(<br /> string path,<br /> Encodingencoding = null,<br />booldetectEncoding = true,<br />intbufferSize = 1024);<br />Named argument<br />OpenTextFile("foo.txt", Encoding.UTF8);<br />OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096);<br />Arguments evaluated in order written<br />Named arguments can appear in any order<br />Named arguments must be last<br />OpenTextFile(<br />bufferSize: 4096,<br /> path: "foo.txt",<br />detectEncoding: false);<br />Non-optional must be specified<br />
  11. 11. Optional & Named Parameters<br />
  12. 12. Co- & Contra- Variance<br />
  13. 13. Covariance (Out)<br />Covariance (Out):Is the ability to use a more derived type than that specified.<br />delegateAnimal MyDel();<br />publicDogMethod() { ... }<br />MyDeldel = Method; // Co - Variance (Out): Return Dog as Animal, Ok. <br />Animalanimal = del(); <br />
  14. 14. Contra Variance (In)<br />Contra variance (in):Is the ability to use a less derived type<br />delegatevoidMyDel( Dogdog);<br />publicvoidMethod(Animalanimal ){ ... }<br />MyDeldel = Method; <br />del( newDog() ); // Contra-Variance (In): Arg Dog as Animal, Ok.<br />
  15. 15. Covariance & Generic<br />What you think?<br />IList<string> strings = new List<string>();<br />IList<object> objects = strings;<br />objects[0] = 5;strings = strings[0];<br />
  16. 16. publicinterfaceIEnumerable<T><br />{<br />IEnumerator<T> GetEnumerator();<br />}<br />publicinterfaceIEnumerable<out T><br />{<br />IEnumerator<T> GetEnumerator();<br />}<br />publicinterfaceIEnumerator<T><br />{<br /> T Current { get; }<br />boolMoveNext();<br />}<br />publicinterfaceIEnumerator<out T><br />{<br /> T Current { get; }<br />boolMoveNext();<br />}<br />Safe Covariance (Out)<br />out= Co-variantOutput positions only<br />Can be treated asless derived<br />IEnumerable<string> strings = GetStrings();<br />IEnumerable<object> objects = strings;<br />
  17. 17. in= Contra-variantInput positions only<br />publicinterfaceIComparer<T><br />{<br />int Compare(T x, T y);<br />}<br />publicinterfaceIComparer<in T><br />{<br />int Compare(T x, T y);<br />}<br />Safe Contra Variance (In)<br />Can be treated asmore derived<br />objectx, y;<br />IComparer<object> objComp = GetComparer();<br />objComp.Compare( x , y );<br />string x1, y1;<br />IComparer<string> strComp = objComp;<br />strComp.Compare( x1 , y1 );<br />
  18. 18. Variance<br />
  19. 19.
  20. 20. Summary<br />Dynamically Typed Objects<br />Optional and Named Parameters<br />Co- and Contra-variance<br />

×