Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual C# "Whidbey": Language Enhancements

Similar presentations


Presentation on theme: "Visual C# "Whidbey": Language Enhancements"— Presentation transcript:

1 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 © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

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

3 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 © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

4 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 © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

5 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; © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

6 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) { © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

7 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) {…} } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

8 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> © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

9 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 } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

10 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> © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

11 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); © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

12 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); © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

13 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); }; © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

14 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; © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

15 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(); } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

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

17 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); } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

18 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); } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

19 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]; } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

20 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)) {…} © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

21 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; © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

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

23 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) {…} } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

24 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; } } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

25 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"); } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

26 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]; } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

27 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 } © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

28 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 © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

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

30 Q & A C# Language home page ECMA C# Standard
11/22/2018 1:47 AM Q & A C# Language home page ECMA C# Standard 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 © Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

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

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


Download ppt "Visual C# "Whidbey": Language Enhancements"

Similar presentations


Ads by Google