SlideShare ist ein Scribd-Unternehmen logo
1 von 21
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
What\'s New in C# 4.0

Weitere ähnliche Inhalte

Was ist angesagt?

FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
rohassanie
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
xu liwei
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
Sidd Singh
 

Was ist angesagt? (20)

Lecture20
Lecture20Lecture20
Lecture20
 
C++11
C++11C++11
C++11
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#
 
科特林λ學
科特林λ學科特林λ學
科特林λ學
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
CBSE Class XI Programming in C++
CBSE Class XI Programming in C++CBSE Class XI Programming in C++
CBSE Class XI Programming in C++
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
jq: JSON - Like a Boss
jq: JSON - Like a Bossjq: JSON - Like a Boss
jq: JSON - Like a Boss
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 
Introduction to JQ
Introduction to JQIntroduction to JQ
Introduction to JQ
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 

Ähnlich wie What\'s New in C# 4.0

Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
Paulo Morgado
 
Introduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicIntroduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamic
Gieno Miao
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
Woody Pewitt
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 

Ähnlich wie What\'s New in C# 4.0 (20)

devLink - What's New in C# 4?
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
 
Introduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicIntroduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamic
 
PDC Video on C# 4.0 Futures
PDC Video on C# 4.0 FuturesPDC Video on C# 4.0 Futures
PDC Video on C# 4.0 Futures
 
Dlr
DlrDlr
Dlr
 
Applying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing SpeedApplying Compiler Techniques to Iterate At Blazing Speed
Applying Compiler Techniques to Iterate At Blazing Speed
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
 
2.dynamic
2.dynamic2.dynamic
2.dynamic
 
How do you create a programming language for the JVM?
How do you create a programming language for the JVM?How do you create a programming language for the JVM?
How do you create a programming language for the JVM?
 
Clean code
Clean codeClean code
Clean code
 
Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#
 
Understanding linq
Understanding linqUnderstanding linq
Understanding linq
 
Javascript
JavascriptJavascript
Javascript
 
Day 1
Day 1Day 1
Day 1
 
Interpreter Case Study - Design Patterns
Interpreter Case Study - Design PatternsInterpreter Case Study - Design Patterns
Interpreter Case Study - Design Patterns
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 

Mehr von Eyal Vardi

Mehr von Eyal Vardi (20)

Why magic
Why magicWhy magic
Why magic
 
Smart Contract
Smart ContractSmart Contract
Smart Contract
 
Rachel's grandmother's recipes
Rachel's grandmother's recipesRachel's grandmother's recipes
Rachel's grandmother's recipes
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)
 
Angular 2 NgModule
Angular 2 NgModuleAngular 2 NgModule
Angular 2 NgModule
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
 
Angular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time CompilationAngular 2 - Ahead of-time Compilation
Angular 2 - Ahead of-time Compilation
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Angular 2 Architecture
Angular 2 ArchitectureAngular 2 Architecture
Angular 2 Architecture
 
Angular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.xAngular 1.x vs. Angular 2.x
Angular 1.x vs. Angular 2.x
 
Angular 2.0 Views
Angular 2.0 ViewsAngular 2.0 Views
Angular 2.0 Views
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0
 
Http Communication in Angular 2.0
Http Communication in Angular 2.0Http Communication in Angular 2.0
Http Communication in Angular 2.0
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injection
 
Angular 2.0 Routing and Navigation
Angular 2.0 Routing and NavigationAngular 2.0 Routing and Navigation
Angular 2.0 Routing and Navigation
 
Async & Parallel in JavaScript
Async & Parallel in JavaScriptAsync & Parallel in JavaScript
Async & Parallel in JavaScript
 
Angular 2.0 Pipes
Angular 2.0 PipesAngular 2.0 Pipes
Angular 2.0 Pipes
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 

What\'s New in C# 4.0

  • 1. What’s New in C# 4.0 Eyal Vardi CEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com
  • 2. Agenda Dynamically Typed Objects Optional & Named Parameters Co- and Contra-variance
  • 3. The Evolution of C# Dynamic C# 4.0 LINQ C# 3.0 C# 2.0 Generics C# 1.0 Managed Code
  • 4. 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
  • 5. 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
  • 7. Optional & Named Parameters
  • 8. 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");
  • 9. 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
  • 10. 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
  • 11. Optional & Named Parameters
  • 12. Co- & Contra- Variance
  • 13. 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();
  • 14. 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.
  • 15. Covariance & Generic What you think? IList<string> strings = new List<string>(); IList<object> objects = strings; objects[0] = 5;strings = strings[0];
  • 16. 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;
  • 17. 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 );
  • 19.
  • 20. Summary Dynamically Typed Objects Optional and Named Parameters Co- and Contra-variance