• Save
What\'s New in C# 4.0
Upcoming SlideShare
Loading in...5
×
 

What\'s New in C# 4.0

on

  • 1,649 views

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 & ...

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 & Named Parameters and Co-variance and Contra-variance.

Statistics

Views

Total Views
1,649
Slideshare-icon Views on SlideShare
1,646
Embed Views
3

Actions

Likes
2
Downloads
0
Comments
0

2 Embeds 3

http://www.linkedin.com 2
http://www.lmodules.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    What\'s New in C# 4.0 What\'s New in C# 4.0 Presentation Transcript

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