Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.

Similar presentations


Presentation on theme: "C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation."— Presentation transcript:

1 C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation

2 Agenda What’s new in.NET 2.0 C# 2.0 Language Visual Studio 2005 The LINQ Project

3

4 Increasing Level of Abstraction Micro Computers PersonalComputers Graphical User Interfaces The Web Machine code C Turbo Pascal MS-BASIC C++ Delphi Visual Basic HTML Script 1980 1990 1995 2000 XML Web Services.NET/C# Java

5 What’s New in.NET 2.0? Common Language Runtime Base Class Library ADO.NET and XML ASP.NET Web Services and UI Windows Forms Rich Client UI Visual Studio C#VBC++Others New collections Typed resources Serial I/O and much more… 64-bit support Edit and Continue Generics and much more… ClickOnce Layout panels DataGridView and much more… Multiple resultsets Async operations Compiled XSLT and much more… VS Team System Express editions Productivity and much more… Master pages PersonalizationMembership and much more… Generics Partial classes Nullable types and much more… Performance! Feedback driven feature set

6 C# 2.0 Enhancements Generics Anonymous methods Nullable value types Iterators Partial types and much more… 100% backwards compatible

7 public class List { private object[] elements; private object[] elements; private int count; private int count; public void Add(object element) { public void Add(object element) { if (count == elements.Length) Resize(count * 2); if (count == elements.Length) Resize(count * 2); elements[count++] = element; elements[count++] = element; } public object this[int index] { public object this[int index] { get { return elements[index]; } get { return elements[index]; } set { elements[index] = value; } set { elements[index] = value; } } public int Count { public int Count { get { return count; } get { return count; } }} Generics public class List public class List { private T[] elements; private T[] elements; private int count; private int count; public void Add(T element) { public void Add(T element) { if (count == elements.Length) Resize(count * 2); if (count == elements.Length) Resize(count * 2); elements[count++] = element; elements[count++] = element; } public T this[int index] { public T this[int index] { get { return elements[index]; } get { return elements[index]; } set { elements[index] = value; } set { elements[index] = value; } } public int Count { public int Count { get { return count; } get { return count; } }} 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 List intList = new List (); intList.Add(1);// No boxing intList.Add(2);// No boxing intList.Add("Three");// Compile-time error int i = intList[0];// No cast required

8 Generics Why generics? Type checking, no boxing, no downcasts Increased sharing (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 Exact run-time type information

9 Generics Collection classes Collection interfaces Collection base classes Utility classes Reflection List<T>Dictionary<K,V>SortedDictionary<K,V>Stack<T>Queue<T> 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>

10 Anonymous Methods Delegates written in-line delegate bool Predicate (T item); public class List public class List { public List FindAll(Predicate filter) { public List FindAll(Predicate filter) { List result = new List (); List result = new List (); foreach (T item in this) { foreach (T item in this) { if (filter(item)) result.Add(item); if (filter(item)) result.Add(item); } return result; return result; }} public class Bank { List accounts; List accounts; List GetOverdrawnAccounts() { List GetOverdrawnAccounts() { return accounts.FindAll(new Predicate (IsOverdrawn)); return accounts.FindAll(new Predicate (IsOverdrawn)); } public static bool IsOverdrawn(Account a) { public static bool IsOverdrawn(Account a) { return a.Balance < 0; return a.Balance < 0; }} public class Bank { List accounts; List accounts; List GetOverdrawnAccounts() { List GetOverdrawnAccounts() { return accounts.FindAll(delegate(Account a) { return a.Balance < 0; }); return accounts.FindAll(delegate(Account a) { return a.Balance < 0; }); }}

11 Generics Performance

12 Nullable Value Types System.Nullable<T> Provides nullability for any value type Struct that combines a T and a bool public struct Nullable where T: struct { private T value; private T value; private bool hasValue; private bool hasValue; public T Value { get {…} } public T Value { get {…} } public bool HasValue { get {…} } public bool HasValue { get {…} }} 123 int 123 Nullable<int> true ??? false Non-nullNull

13 Nullable Value Types T? syntax null literal Conversions Lifted operators Null coalescing int? x = 123; double? y = 1.0; int? x = null; double? y = null; int i = 123; int? x = i; int j = (int)x; int? x = GetNullableInt(); int? y = GetNullableInt(); int? z = x + y; int? x = GetNullableInt(); int i = x ?? 0;

14 Iterators Methods that incrementally compute and return a sequence of values class Program { static IEnumerable Range(int start, int count) { static IEnumerable Range(int start, int count) { for (int i = 0; i < count; i++) yield return start + i; for (int i = 0; i < count; i++) yield return start + i; } static IEnumerable Squares(IEnumerable source) { static IEnumerable Squares(IEnumerable source) { foreach (int x in source) yield return x * x; foreach (int x in source) yield return x * x; } static void Main() { static void Main() { foreach (int i in Squares(Range(0, 10))) Console.WriteLine(i); foreach (int i in Squares(Range(0, 10))) Console.WriteLine(i); }} 0149162536496481

15 Partial Types public partial class Customer { private int id; private int id; private string name; private string name; private string address; private string address; private List orders; private List orders;} public partial class Customer { public void SubmitOrder(Order order) { public void SubmitOrder(Order order) { orders.Add(order); orders.Add(order); } public bool HasOutstandingOrders() { public bool HasOutstandingOrders() { return orders.Count > 0; return orders.Count > 0; }} public class Customer { private int id; private int id; private string name; private string name; private string address; private string address; private List orders; private List orders; public void SubmitOrder(Order order) { public void SubmitOrder(Order order) { orders.Add(order); orders.Add(order); } public bool HasOutstandingOrders() { public bool HasOutstandingOrders() { return orders.Count > 0; return orders.Count > 0; }}

16 Other New Features Static classes Property accessibility control External aliases Namespace alias qualifiers Inline warning control Fixed size buffers

17 Visual Studio 2005 Developer productivity Write code Navigate code Refactor code Debug code Team productivity Source code control Build code Visualize code Migrate code Productivity

18 Visual Studio 2005

19 Problem: Data != Objects

20 The LINQ Project Standard Query Operators Objects DLinqXLinq XML.NET Language Integrated Query C#VBOthers… Relational

21 LINQ and C# 3.0

22 Benefits Of LINQ Unified querying of objects, relational, XML Type checking and IntelliSense for queries SQL and XQuery-like power in C# and VB Extensibility model for languages / APIs http://msdn.microsoft.com/netframework/future/linq/

23 © 2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.


Download ppt "C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation."

Similar presentations


Ads by Google