Visual C# "Whidbey": Language Enhancements

Slides:



Advertisements
Similar presentations
1 Visual C# "Whidbey": Language Enhancements Anders Hejlsberg Distinguished Engineer Microsoft Corporation Anders Hejlsberg Distinguished.
Advertisements

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.
Language Fundamentals in brief C# - Introduction.
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
DEV340 Microsoft Visual C# Under the Covers: An In-Depth Look at C# 2.0 Anders Hejlsberg Distinguished Engineer Microsoft Corporation.
Overview of Visual C# 2005 Henrik Westergaard Hansen
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++
C#C# C#C# Kit Colbert Student Consultant representing Microsoft
What’s New in Visual Studio 2005: Security &.NET Framework 2.0 Mike Pelton Developer & Platform Group Microsoft Ltd.
Iterator Pattern Dr. Neal CIS 480. Iterator An iterator pattern can be used when one class is a collection of things and would like to provide a standardized.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
DEV485.NET CodeDOM demystified Beat Schwegler Architect Evangelist.NET Developer Group Microsoft Corporation.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
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.
Jon Shemitz Complicated stuff, quickly..NET 2.0. ● Generics50% & Nullable Types ● Iterators 10% ● Delegate Enhancements35% ● Partial Types5%
Visual C# 2005: IDE Enhancements Dan Fernandez C# Product Manager
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
[ISRAR ALI] Hammad Khan. The namespace keyword is used to declare a scope. Making software components reusable can result in naming collisions (two classes.
C Sharp Web & Internet Programming Group Diana, Aren, Jeff, & Adam, Farrin 5/5/20081CS 311.
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.
Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
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 11 th Lecture Pavel Ježek
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
Visual Basic 2005: Advanced Language and IDE Features Amanda Silver Program Manager Visual Basic Session Code: DEV343.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
CSharp Overview Joe Healy Why C# ? First component-oriented language –Builds on COM+ experience –Native support for Namespaces; Versioning.
M E L B O U R N E S Y D N E Y C A N B E R R A B R I S B A N EW W W. R E A D I F Y. N E T DEV315: Visual C# Under the Covers An In-Depth Look at C# 2.0.
1 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Minimising memory churn
LESSON 06.
Basic Introduction to C#
Advanced .NET Programming I 2nd Lecture
Advanced .NET Programming I 3nd Lecture
Introduction to LINQ and Generic Collections
Interfaces and Generics
Introduction to .NET Generics
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2005
Upgrading Your C# Programming Skills to Be a More Effective Developer
Advanced .NET Programming I 4th Lecture
Iterators and Comparators
Chapter 5: Programming with C#
New Features of C# Kuppurasu Nagaraj Microsoft Connect 2016
CS360 Windows Programming
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
Visual C# 2005: Language Enhancements
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Generics in C# / Anders Børjesson
DEV321 Visual C# 2005: Language Enhancements
Interfaces and Generics
Getting Ready for Visual Studio 2005
How to organize and document your classes
Server & Tools Business
Advanced .NET Programming I 3rd Lecture
Advanced .NET Programming I 4th Lecture
Visual C# 2005: Language Enhancements
Presentation transcript:

Visual C# "Whidbey": Language Enhancements 11/22/2018 1:47 AM Session Code: TLS320 Visual C# "Whidbey": Language Enhancements Anders Hejlsberg Distinguished Engineer Microsoft Corporation andersh@microsoft.com © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

C# Language Enhancements 11/22/2018 1:47 AM C# Language Enhancements Generics Anonymous methods Iterators Partial types Other enhancements © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Generics public class List { private object[] elements; 11/22/2018 1:47 AM Generics public class List { private object[] elements; private int count; public void Add(object element) { if (count == elements.Length) Resize(count * 2); elements[count++] = element; } public object this[int index] { get { return elements[index]; } set { elements[index] = value; } public int Count { get { return count; } public class List<T> { private T[] elements; private int count; public void Add(T element) { if (count == elements.Length) Resize(count * 2); elements[count++] = element; } public T this[int index] { get { return elements[index]; } set { elements[index] = value; } public int Count { get { return count; } List<int> intList = new List<int>(); intList.Add(1); // No boxing intList.Add(2); // No boxing intList.Add("Three"); // Compile-time error int i = intList[0]; // No cast required List intList = new List(); intList.Add(1); intList.Add(2); intList.Add("Three"); int i = (int)intList[0]; List intList = new List(); intList.Add(1); // Argument is boxed intList.Add(2); // Argument is boxed intList.Add("Three"); // Should be an error int i = (int)intList[0]; // Cast required © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Generics Why generics? How are C# generics implemented? 11/22/2018 1:47 AM Generics Why generics? Type checking, no boxing, no downcasts Reduced code bloat (typed collections) How are C# generics implemented? Instantiated at run-time, not compile-time Checked at declaration, not instantiation Work for both reference and value types Complete run-time type information © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Generics Type parameters can be applied to Class, struct, interface, and delegate types class Dictionary<K,V> {…} struct HashBucket<K,V> {…} interface IComparer<T> {…} delegate R Function<A,R>(A arg); Dictionary<string,Customer> customerLookupTable; Dictionary<string,List<Order>> orderLookupTable; Dictionary<string,int> wordCount; © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Generics Type parameters can be applied to Class, struct, interface, and delegate types Methods string[] names = Utils.CreateArray<string>(3); names[0] = "Jones"; names[1] = "Anderson"; names[2] = "Williams"; Utils.SortArray(names); class Utils { public static T[] CreateArray<T>(int size) { return new T[size]; } public static void SortArray<T>(T[] array) { … © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Generics Type parameters can be applied to Class, struct, interface, and delegate types Methods Type parameters can have constraints One base class, multiple interfaces, new() class Dictionary<K,V>: IDictionary<K,V> where K: IComparable<K> where V: IKeyProvider<K>, IPersistable, new() { public void Add(K key, V value) { … } class Dictionary<K,V> where K: IComparable { public void Add(K key, V value) { … if (key.CompareTo(x) == 0) {…} } class Dictionary<K,V> { public void Add(K key, V value) { … if (((IComparable)key).CompareTo(x) == 0) {…} } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Generics Generics are “invariant” 11/22/2018 1:47 AM Generics Generics are “invariant” Interfaces can be used for type neutrality public class List<T>: IList { public void Add(T item) {…} public T this[int index] {…} … } public class List<T> { public void Add(T item) {…} public T this[int index] {…} … } public interface IList { public void Add(object item); public object this[int index] {…} … } object List<object> List<int> List<string> © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Generics T.default Null checks Type casts void Foo<T>() { 11/22/2018 1:47 AM Generics void Foo<T>() { T x = null; // Error T y = T.default; // Ok } T.default Null checks Type casts void Foo<T>(T x) { if (x == null) { throw new FooException(); } … void Foo<T>(T x) { int i = (int)x; // Error int j = (int)(object)x; // Ok } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Generics Collection classes Collection interfaces 11/22/2018 1:47 AM Generics List<T> Dictionary<K,V> SortedDictionary<K,V> Stack<T> Queue<T> Collection classes Collection interfaces Collection base classes Utility classes Reflection IList<T> IDictionary<K,V> ICollection<T> IEnumerable<T> IEnumerator<T> IComparable<T> IComparer<T> Collection<T> KeyedCollection<T> ReadOnlyCollection<T> Nullable<T> EventHandler<T> Comparer<T> © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Generics System.Nullable<T> Provides nullability for any type 11/22/2018 1:47 AM Generics System.Nullable<T> Provides nullability for any type Struct that combines a T and a bool Conversions between T and Nullable<T> Conversion from null literal to Nullable<T> Nullable<int> x = 123; Nullable<int> y = null; int i = (int)x; int j = x.Value; if (x.HasValue) Console.WriteLine(x.Value); © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Anonymous Methods class MyForm : Form { ListBox listBox; 11/22/2018 1:47 AM Anonymous Methods class MyForm : Form { ListBox listBox; TextBox textBox; Button addButton; public MyForm() { listBox = new ListBox(...); textBox = new TextBox(...); addButton = new Button(...); addButton.Click += delegate { listBox.Items.Add(textBox.Text); }; } class MyForm : Form { ListBox listBox; TextBox textBox; Button addButton; public MyForm() { listBox = new ListBox(...); textBox = new TextBox(...); addButton = new Button(...); addButton.Click += new EventHandler(AddClick); } void AddClick(object sender, EventArgs e) { listBox.Items.Add(textBox.Text); © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Anonymous Methods Allows code block in place of delegate 11/22/2018 1:47 AM Anonymous Methods Allows code block in place of delegate Delegate type automatically inferred Code block can be parameterless Or code block can have parameters In either case, return types must match button.Click += delegate { MessageBox.Show("Hello"); }; button.Click += delegate(object sender, EventArgs e) { MessageBox.Show(((Button)sender).Text); }; © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Anonymous Methods Code block can access local variables 11/22/2018 1:47 AM Anonymous Methods Code block can access local variables public class Bank { List<Account> GetLargeAccounts(double minBalance) { Helper helper = new Helper(); helper.minBalance = minBalance; return accounts.FindAll(helper.Matches); } internal class Helper internal double minBalance; internal bool Matches(Account a) { return a.Balance >= minBalance; } } public class Bank { List<Account> accounts; List<Account> GetOverdrawnAccounts() { return accounts.FindAll(delegate(Account a) { return a.Balance < 0; }); } List<Account> GetLargeAccounts(double minBalance) { return a.Balance >= minBalance; delegate bool Predicate<T>(T item); public class List<T> { public List<T> FindAll(Predicate<T> filter) { List<T> result = new List<T>(); foreach (T item in this) { if (filter(item)) result.Add(item); } return result; © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Anonymous Methods Method group conversions 11/22/2018 1:47 AM Anonymous Methods Method group conversions Delegate type inferred when possible using System; using System.Threading; class Program { static void Work() {…} static void Main() { Thread t = new Thread(new ThreadStart(Work)); t.Start(); } using System; using System.Threading; class Program { static void Work() {…} static void Main() { Thread t = new Thread(Work); t.Start(); } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Generics Performance 11/22/2018 1:47 AM © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Iterators foreach relies on “enumerator pattern” 11/22/2018 1:47 AM Iterators foreach relies on “enumerator pattern” GetEnumerator() method foreach makes enumerating easy But enumerators are hard to write! foreach (object obj in list) { DoSomething(obj); } Enumerator e = list.GetEnumerator(); while (e.MoveNext()) { object obj = e.Current; DoSomething(obj); } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Iterators public class ListEnumerator : IEnumerator { List list; 11/22/2018 1:47 AM Iterators public class ListEnumerator : IEnumerator { List list; int index; internal ListEnumerator(List list) { this.list = list; index = -1; } public bool MoveNext() { int i = index + 1; if (i >= list.count) return false; index = i; return true; public object Current { get { return list.elements[index]; } public class List { internal object[] elements; internal int count; public ListEnumerator GetEnumerator() { return new ListEnumerator(this); } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

11/22/2018 1:47 AM Iterators public IEnumerator GetEnumerator() { return new __Enumerator(this); } private class __Enumerator: IEnumerator { object current; int state; public bool MoveNext() { switch (state) { case 0: … case 1: … case 2: … … public object Current { get { return current; } Method that incrementally computes and returns a sequence of values yield return and yield break Must return IEnumerator or IEnumerable public class List { public IEnumerator GetEnumerator() { for (int i = 0; i < count; i++) { yield return elements[i]; } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Iterators public class List<T> { 11/22/2018 1:47 AM Iterators public class List<T> { public IEnumerator<T> GetEnumerator() { for (int i = 0; i < count; i++) yield return elements[i]; } public IEnumerable<T> Descending() { for (int i = count - 1; i >= 0; i--) yield return elements[i]; public IEnumerable<T> Subrange(int index, int n) { for (int i = 0; i < n; i++) yield return elements[index + i]; List<Item> items = GetItemList(); foreach (Item x in items) {…} foreach (Item x in items.Descending()) {…} foreach (Item x in Items.Subrange(10, 20)) {…} © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Partial Types public partial class Customer { private int id; 11/22/2018 1:47 AM Partial Types public partial class Customer { private int id; private string name; private string address; private List<Orders> orders; } public class Customer { private int id; private string name; private string address; private List<Orders> orders; public void SubmitOrder(Order order) { orders.Add(order); } public bool HasOutstandingOrders() { return orders.Count > 0; public partial class Customer { public void SubmitOrder(Order order) { orders.Add(order); } public bool HasOutstandingOrders() { return orders.Count > 0; © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

11/22/2018 1:47 AM Partial Types © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Other Enhancements Static classes Can contain only static members 11/22/2018 1:47 AM Other Enhancements Static classes Can contain only static members Cannot be type of variable, parameter, etc. System.Console, System.Environment, etc. public static class Math { public static double Sin(double x) {…} public static double Cos(double x) {…} … } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Other Enhancements Property accessor accessibility 11/22/2018 1:47 AM Other Enhancements Property accessor accessibility Allows one accessor to be restricted further Typically set {…} more restricted than get {…} public class Customer { private string id; public string CustomerId { get { return id; } internal set { id = value; } } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Other Enhancements Namespace alias qualifier A::B looks up A only as namespace alias global::X starts lookup in global namespace using IO = System.IO; class Program { static void Main() { IO::Stream s = new IO::File.OpenRead("foo.txt"); global::System.Console.WriteLine("Hello"); } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Other Enhancements Fixed size buffers 11/22/2018 1:47 AM Other Enhancements Fixed size buffers C style embedded arrays in unsafe code public struct OFSTRUCT { public byte cBytes; public byte fFixedDisk; public short nErrCode; private int Reserved; public fixed char szPathName[128]; } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Other Enhancements #pragma warning 11/22/2018 1:47 AM Other Enhancements #pragma warning Control individual warnings in blocks of code using System; class Program { [Obsolete] static void Foo() {} static void Main() { #pragma warning disable 612 Foo(); #pragma warning restore 612 } © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

C# and CLI Standardization 11/22/2018 1:47 AM C# and CLI Standardization Work begun in September 2000 Intel, HP, IBM, Fujitsu, Plum Hall, and others ECMA standards ratified December 2001 ISO standards published April 2003 Several CLI and C# implementations .NET Framework and Visual Studio .NET “SSCLI” – Shared source on XP, FreeBSD, OS X “Mono” – Open source on Linux Standardization of new features ongoing © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Book signing, Exhibition Hall Book Store, Wednesday, 1-2pm 11/22/2018 1:47 AM Book signing, Exhibition Hall Book Store, Wednesday, 1-2pm © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

Q & A C# Language home page ECMA C# Standard 11/22/2018 1:47 AM Q & A C# Language home page http://msdn.microsoft.com/vcsharp/language ECMA C# Standard http://www.ecma-international.org/ publications/standards/ecma-334.htm Panel: The Future of .NET Languages PNL10, Thursday, 1:45pm – 3:15pm Tools Lounge (near Hall A) Tuesday, 5:15pm – 6:30pm Wednesday, 3:00pm – 5:00pm © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

© 2003-2004 Microsoft Corporation. All rights reserved. 11/22/2018 1:47 AM © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary. © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

11/22/2018 1:47 AM © 2003-2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.