What’s New in C# 4.0? Pavel Yosifovich SELA Group

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

Dynamic internals. Introductions  Alexandru Ghiondea  C# Compiler QA  
 Anders Hejlsberg Technical Fellow Microsoft Corporation TL16.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Ken Casada Developer Evangelist Microsoft Switzerland
C# Types Tom Roeder CS fa. Administration CMS is up let me know if you can’t see the course Assignments are posted may not be able to do some.
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
Visual Studio 2010 and.NET Framework 4 Training Workshop.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
3. Data Types. 2 Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Gurinder CTO. Lisa Feigenbaum Microsoft Program Manager Visual Studio Languages
demo video demo Dynamic Languages Simple and succinctImplicitly typedMeta-programmingNo compilation Static Languages RobustPerformantIntelligent.
Louis de Klerk Consultant Inobits Consulting DTL308.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
Eric Vogel Software Developer A.J. Boggs & Company.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
Dynamic Languages & The.Net Framework Shay Friedman ActionBase
New team member / new project for the team Helps analyze relationships and structure Understanding code Locates code in unfamiliar code bases Understand.
Alex Turner C# Compiler PM Session Code: DEV402 On the Dynamic menu today… The Dynamic Language Runtime How does it relate to the CLR? Dynamic in C#
 Jim Hugunin Partner Architect Microsoft Corporation TL10.
Calculator calc = GetCalculator(); int sum = calc.Add(10, 20); Calculator calc = GetCalculator(); int sum = calc.Add(10, 20); object calc.
© FPT Software Advanced features in C# 1. © FPT Software Agenda Attributes Delegates & Events Anonymous Types & Dynamic Type Extension Methods Lambda.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Lisa Feigenbaum Microsoft Program Manager Session Code: DEV314.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Types in programming languages1 What are types, and why do we need them?
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
1 CSC241: Object Oriented Programming Lecture No 25.
Ahmed Salijee Developer Advisor
RUBY by Ryan Chase.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
IronRuby for the.NET Developer Cory Foy - Cory Foy, LLC + + =
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Scott Hanselman Principal Program Manager Microsoft DTL303.
Jonathan Aneja Program Manager Microsoft Corporation Session Code: DTL336 Anders Hejlsberg Technical Fellow Microsoft Corporation.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
Lap Around the.NET Framework 4 NameTitleCompany. A Look Back….NET 1.0.NET 1.1.NET NET CTP CLR 1.0 CLR 1.1 CLR 2.0 CLR.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Luke Hoban Senior Program Manager Microsoft Session Code: DTL310.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
© Copyright SELA software & Education Labs Ltd Baruch Hirsch St.Bnei Brak Israel 1 בס " ד.
Введение в DLR UNETA 16 апреля, 2010 Иван Колодяжный, Software Developer Teamdev Ltd.
Visual Studio 2010 and.NET Framework 4 Training Workshop.
/* LIFE RUNS ON CODE*/ Konstantinos Pantos Microsoft MVP ASP.NET
C# Operator Overloading and Type Conversions
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Computing with C# and the .NET Framework
C# in the Big World Mads Torgersen Program Manager
Visual Studio 2010 and .NET Framework 4 Training Workshop
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Tech·Ed North America /18/2018 5:14 PM
6 Delegate and Lambda Expressions
Presentation transcript:

What’s New in C# 4.0? Pavel Yosifovich SELA Group

2 Agenda C# Evolution Default and Named Arguments Dynamic Typing COM Interoperability Generic Variance Summary

The Evolution of C# C# 1.0 C# 2.0 C# 3.0 Managed Code Generics Language Integrated Query

Trends Declarative ConcurrentDynamic

Declarative Programming What How ImperativeDeclarative

Dynamic vs. Static Dynamic Languages Simple and succinct Implicitly typed Meta- programming No compilation Static Languages RobustPerformantIntelligent toolsBetter scaling

The Evolution of C# C# 1.0 C# 2.0 C# 3.0 Managed Code Generics Language Integrated Query C# 4.0 Dynamic Programming

Optional and Named Parameters public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding, int bufferSize); public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding, int bufferSize); public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding); public StreamReader OpenTextFile( string path, Encoding encoding); public StreamReader OpenTextFile( string path); public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding); public StreamReader OpenTextFile( string path, Encoding encoding); public StreamReader OpenTextFile( string path); Primary method Secondary overloads Call primary with default values

Optional and Named Parameters public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding, int bufferSize); public StreamReader OpenTextFile( string path, Encoding encoding, bool detectEncoding, int bufferSize); public StreamReader OpenTextFile( string path, Encoding encoding = null, bool detectEncoding = true, int bufferSize = 1024); public StreamReader OpenTextFile( string path, Encoding encoding = null, bool detectEncoding = true, int bufferSize = 1024); Optional parameters OpenTextFile("foo.txt", Encoding.UTF8); OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096); Named argument OpenTextFile( bufferSize: 4096, path: "foo.txt", detectEncoding: false); OpenTextFile( bufferSize: 4096, path: "foo.txt", detectEncoding: false); Named arguments must be last Non-optional must be specified Arguments evaluated in order written Named arguments can appear in any order

10 Named & Optional Parameters Caveats  Parameter names  Evaluation order  Virtual methods

Python Binder Ruby Binder COM Binder JavaScri pt Binder Object Binder.NET Dynamic Programming Dynamic Language Runtime Expression Trees Dynamic Dispatch Call Site Caching IronPythonIronPythonIronRubyIronRubyC#C#VB.NETVB.NETOthers…Others…

Dynamically Typed Objects Calculator calc = GetCalculator(); int sum = calc.Add(10, 20); Calculator calc = GetCalculator(); int sum = calc.Add(10, 20); object calc = GetCalculator(); Type calcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 }); int sum = Convert.ToInt32(res); object calc = GetCalculator(); Type calcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 }); int sum = Convert.ToInt32(res); ScriptObject calc = GetCalculator(); object res = calc.Invoke("Add", 10, 20); int sum = Convert.ToInt32(res); ScriptObject calc = GetCalculator(); object res = calc.Invoke("Add", 10, 20); int sum = Convert.ToInt32(res); dynamic calc = GetCalculator(); int sum = calc.Add(10, 20); dynamic calc = GetCalculator(); int sum = calc.Add(10, 20); Statically typed to be dynamic Dynamic method invocation Dynamic conversion

Dynamically Typed Objects dynamic x = 1; dynamic y = "Hello"; dynamic z = new List { 1, 2, 3 }; dynamic x = 1; dynamic y = "Hello"; dynamic z = new List { 1, 2, 3 }; Compile-time type dynamic Run-time type System.Int32 When operand(s) are dynamic… Member selection deferred to run-time At run-time, actual type(s) substituted for dynamic Static result type of operation is dynamic

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 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);... } double x = 1.75; double y = Math.Abs(x); double x = 1.75; double y = Math.Abs(x); dynamic x = 1.75; dynamic y = Math.Abs(x); dynamic x = 1.75; dynamic y = Math.Abs(x); Dynamically Typed Objects Method chosen at compile-time: double Abs(double x) Method chosen at run-time: double Abs(double x) dynamic x = 2; dynamic y = Math.Abs(x); dynamic x = 2; dynamic y = Math.Abs(x); Method chosen at run-time: int Abs(int x)

Improved COM Interoperability object fileName = "Test.docx"; object missing = System.Reflection.Missing.Value; doc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); object fileName = "Test.docx"; object missing = System.Reflection.Missing.Value; doc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.SaveAs("Test.docx"); doc.SaveAs("Test.docx");

16 Improved COM Interoperability Automatic object  dynamic mapping Optional and named parameters Indexed properties Optional “ref” modifier Interop type embedding (“No PIA”)

Co- and Contra-variance void Process(object[] objects) { … } string[] strings = GetStringArray(); Process(strings); string[] strings = GetStringArray(); Process(strings); void Process(object[] objects) { objects[0] = "Hello"; // Ok objects[1] = new Button(); // Exception! } void Process(object[] objects) { objects[0] = "Hello"; // Ok objects[1] = new Button(); // Exception! } List strings = GetStringList(); Process(strings); List strings = GetStringList(); Process(strings); void Process(IEnumerable objects) { … }.NET arrays are co-variant …but not safely co-variant Until now, C# generics have been invariant void Process(IEnumerable objects) { // IEnumerable is read-only and // therefore safely co-variant } void Process(IEnumerable objects) { // IEnumerable is read-only and // therefore safely co-variant } C# 4.0 supports safe co- and contra-variance

Safe Co- and Contra-variance public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerator { T Current { get; } bool MoveNext(); } public interface IEnumerator { T Current { get; } bool MoveNext(); } public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerator { T Current { get; } bool MoveNext(); } public interface IEnumerator { T Current { get; } bool MoveNext(); } out = Co-variant Output positions only IEnumerable strings = GetStrings(); IEnumerable objects = strings; IEnumerable strings = GetStrings(); IEnumerable objects = strings; Can be treated as less derived public interface IComparer { int Compare(T x, T y); } public interface IComparer { int Compare(T x, T y); } public interface IComparer { int Compare(T x, T y); } public interface IComparer { int Compare(T x, T y); } IComparer objComp = GetComparer(); IComparer strComp = objComp; IComparer objComp = GetComparer(); IComparer strComp = objComp; in = Contra-variant Input positions only Can be treated as more derived

19 Variance in C# 4.0 Supported for interface and delegate types “Statically checked definition-site variance” Value types are always invariant  IEnumerable is not IEnumerable  Similar to existing rules for arrays ref and out parameters need invariant type

Variance in.NET Framework 4.0 System.Collections.Generic.IEnumerable System.Collections.Generic.IEnumerator System.Linq.IQueryable System.Collections.Generic.IComparer System.Collections.Generic.IEqualityComparer System.IComparable System.Collections.Generic.IEnumerable System.Collections.Generic.IEnumerator System.Linq.IQueryable System.Collections.Generic.IComparer System.Collections.Generic.IEqualityComparer System.IComparable Interfaces System.Func System.Action System.Predicate System.Comparison System.EventHandler System.Func System.Action System.Predicate System.Comparison System.EventHandler Delegates

21 Summary Optional and Named Arguments Dynamic Typing Generic Variance Easier COM Interoperability