CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek

Slides:



Advertisements
Similar presentations
The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 14 th Lecture Pavel Ježek
C# 3.0 Tom Roeder CS fa. Version 3 From PDC 2005 preview compiler available LINQ: language-integrated query High level points: adds native query.
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
C# vs. C++ What's Different & What's New. An example C# public sometype myfn { get; set; } C++ public: sometype myfn { sometype get (); void set (sometype.
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
Advanced .NET Programming I 13th Lecture
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# 3.0 and.NET 3.5: A Brief Overview Pavel Ježek.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
Advanced C#, part IV Niels Hallenberg IT University of Copenhagen (With thanks to Peter Sestoft and Kasper Østerbye) BAAAP – Spring 2009.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
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#
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 8 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 9 th Lecture Pavel Ježek
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
Introduction to C# By: Abir Ghattas Michel Barakat.
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
- This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 5 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 3 rd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 7 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Advanced .NET Programming I 2nd Lecture
Advanced .NET Programming I 3nd Lecture
Advanced .NET Programming II 6th Lecture
Advanced .NET Programming I 8th Lecture
Advanced .NET Programming I 4th Lecture
Advanced .NET Programming I 7th Lecture
Advanced .NET Programming I 6th Lecture
LINQ - 2 Ravi Kumar C++/C# Team.
Advanced .NET Programming I 8th Lecture
Advanced .NET Programming I 5th Lecture
C# Language & .NET Platform 10th Lecture
Advanced .NET Programming I 7th Lecture
Advanced .NET Programming I 3rd Lecture
Advanced .NET Programming I 4th Lecture
C# Language & .NET Platform 11th Lecture
Advanced .NET Programming I 6th Lecture
C# Language & .NET Platform 3rd Lecture
C# Language & .NET Platform 9th Lecture
C# Language & .NET Platform 4th Lecture
C# Language & .NET Platform 8th Lecture
C# Language & .NET Platform 12th Lecture
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (

C# 3.0 Features Implicitly Typed Local Variables Extension Methods Lambda Expressions Object Initializers Collection Initializers Anonymous Types Expression Trees Query Expressions C# 3.0 compiler and.NET 3.5 libraries are part of Visual Studio 2008

Implicitly Typed Local Variables Examples: var i = 5; var s = "Hello"; var d = 1.0; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary (); Are equivalent to: int i = 5; string s = "Hello"; double d = 1.0; int[] numbers = new int[] {1, 2, 3}; Dictionary orders = new Dictionary (); Errors: var x; // Error, no initializer to infer type from var y = {1, 2, 3}; // Error, collection initializer not permitted var z = null; // Error, null type not permitted

Extension Methods Declaration: public static partial class Extensions { public static int ToInt32(this string s) { return Int32.Parse(s); } } Usage: string s = "1234"; int i = s.ToInt32(); // Same as Extensions.ToInt32(s) Instance methods take precedence over extension methods Extension methods imported in inner namespace declarations take precedence over extension methods imported in outer namespace declarations

Extension Methods (2) Declaration: public static partial class Extensions { public static T[] Slice (this T[] source, int index, int count) { if (index < 0 || count < 0 || source.Length – index < count) throw new ArgumentException(); T[] result = new T[count]; Array.Copy(source, index, result, 0, count); return result; } } Usage: int[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int[] a = digits.Slice(4, 3); // Same as Extensions.Slice(digits, 4, 3)

Lambda Expressions Example of C# 2.0 anonymous method: class Program { delegate T BinaryOp (T x, T y); static void Method (BinaryOp op, T p1, T p2) { Console.WriteLine(op(p1, p2)); } static void Main() { Method(delegate(int a, int b) { return a + b; }, 1, 2); } C# 3.0 lambda expressions provide further simplification: class Program { delegate T BinaryOp (T x, T y); static void Method (BinaryOp op, T p1, T p2) { Console.WriteLine(op(p1, p2)); } static void Main() { Method((a, b) => a + b, 1, 2); }

Lambda Expressions (2) Expression or statement body Implicitly or explicitly typed parameters Examples: x => x + 1 // Implicitly typed, expression body x => { return x + 1; } // Implicitly typed, statement body (int x) => x + 1 // Explicitly typed, expression body (int x) => { return x + 1; } // Explicitly typed, statement body (x, y) => x * y // Multiple parameters () => Console.WriteLine() // No parameters A lambda expression is a value, that does not have a type but can be implicitly converted to a compatible delegate type delegate R Func (A arg); Func f1 = x => x + 1; // Ok Func f2 = x => x + 1; // Ok Func f3 = x => x + 1; // Error – double cannot be // implicitly converted to int

Lambda Expressions (3) Lambda expressions participate in inference process of type arguments of generic methods In initial phase, nothing is inferred from arguments that are lambda expressions Following the initial phase, additional inferences are made from lambda expressions using an iterative process

Lambda Expressions (4) Generic extension method example: public class List : IEnumerable, … { … } public static class Sequence { public static IEnumerable Select ( this IEnumerable source, Func selector) { foreach (T element in source) yield return selector(element); } Calling extension method with lambda expression: List customers = GetCustomerList(); IEnumerable names = customers.Select(c => c.Name); Rewriting extension method call: IEnumerable names = Sequence.Select (customers, c => c.Name); T type argument is inferred to Customer based on source argument type Sequence.Select (customers, c => c.Name) c lambda expression argument type is infered to Customer Sequence.Select (customers, (Customer c) => c.Name) S type argument is inferred to string based on return value type of the lambda expression Sequence.Select (customers, (Customer c) => c.Name)

Object Initializers Class representing a point: public class Point { private int x, y; public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } } New instance can be created using object initializer: var a = new Point { X = 0, Y = 1 }; Which is equivalent to: var a = new Point(); a.X = 0; a.Y = 1;

Object Initializers (2) Class representing a rectangle: public class Rectangle { private Point p1, p2; public Point P1 { get { return p1; } set { p1 = value; } } public Point P2 { get { return p2; } set { p2 = value; } } } New instance can be created using object initializer: var r = new Rectangle { P1 = new Point { X = 0, Y = 1 }, P2 = new Point { X = 2, Y = 3 } }; Which is equivalent to: var r = new Rectangle(); var __p1 = new Point(); __p1.X = 0; __p1.Y = 1; r.P1 = __p1; var __p2 = new Point(); __p2.X = 2; __p2.Y = 3; r.P2 = __p2;

Object Initializers (3) Class representing a rectangle with constructor that allocates p1 and p2: public class Rectangle { private Point p1 = new Point(); private Point p2 = new Point(); public Point P1 { get { return p1; } } public Point P2 { get { return p2; } } } New instance can be created using object initializer: var r = new Rectangle { P1 = { X = 0, Y = 1 }, P2 = { X = 2, Y = 3 } }; Which is equivalent to: var r = new Rectangle(); r.P1.X = 0; r.P1.Y = 1; r.P2.X = 2; r.P2.Y = 3;

Collection Initializers Example: List digits = new List { 0, 1, 2}; Is equivalent to: List digits = new List (); digits.Add(0); digits.Add(1); digits.Add(2); List has to implement System.Collections.Generic.ICollection interface with the Add(T) method

Combining Object and Collection Initializers Class representing a contact with name and list of phone numbers: public class Contact { private string name; private List phoneNumbers = new List (); public string Name { get { return name; } set { name = value; } } public List PhoneNumbers { get { return phoneNumbers; } } } List of contacts can be created and initialized with: var contacts = new List { new Contact { Name = "Chris Smith", PhoneNumbers = { " ", " " } }, new Contact { Name = "Bob Harris", PhoneNumbers = { " " } } }; Which is equivalent to: var contacts = new List (); var __c1 = new Contact(); __c1.Name = "Chris Smith"; __c1.PhoneNumbers.Add(" "); __c1.PhoneNumbers.Add(" "); contacts.Add(__c1); var __c2 = new Contact(); __c2.Name = "Bob Harris"; __c2.PhoneNumbers.Add(" "); contacts.Add(__c2);

Auto-implemented Properties Backed up by a private field normally inaccessible to programmer (only via the property): class LightweightCustomer { public double TotalPurchases { get; set; } public string Name { get; private set; }// read-only public int CustomerID { get; private set; } // read-only }

Anonymous Types Following expression: new { p1 = e1, p2 = e2, … pn = en } Can be used to define an anonymous type : class __Anonymous1 { private T1 f1 ; private T2 f2 ; … private Tn fn ; public T1 p1 { get { return f1 ; } set { f1 = value ; } } public T2 p2 { get { return f2 ; } set { f2 = value ; } } … public T1 p1 { get { return f1 ; } set { f1 = value ; } } } And create its instance using object initializer Different anonymous object initilizers that define properties with same names in the same order generate the same anonymous type: var p1 = new { Name = "Lawnmower", Price = }; var p2 = new { Name = "Shovel", Price = }; p1 = p2;

Partial Methods Can appear only in partial classes or structs, and must be void, private and without out parameters: partial class A { string _name; partial void OnNameChanged(); public string Name { set { _name = value; OnNameChanged(); } partial class A { partial void OnNameChanged() { // Do something }

Query Expressions – Examples Query expression: from c in customers where c.City == "London“ select c Gets translated to: customers.Where(c => c.City == "London")

Query Expressions – Examples Query expression: from c in customers where c.City == "London" select c.Name Gets translated to: customers.Where(c => c.City == "London").Select(c => c.Name)

Query Expressions – Examples Query expression: from c in customers orderby c.Name select new { c.Name, c.City } Gets translated to: customers.OrderBy(c => c.Name).Select(c => new { Name = c.Name, Phone = c.City })

Query Expressions – Examples Query expression: from c in customers where c.City == "London" orderby c.Name select new { c.City, c.Name } Gets translated to: customers.Where(c => c.City == "London").OrderBy(c => c.Name).Select(c => new { c.City, c.Name }) NOTE – is equivalent to (i.e. “c” variables in lambdas are not related to each other): customers.Where(c1 => c1.City == "London").OrderBy(c2 => c2.Name).Select(c3 => new { c3.City, c3.Name })