Date post: | 17-Jan-2016 |
Category: |
Documents |
Upload: | eleanore-robinson |
View: | 215 times |
Download: | 0 times |
C# 2.0 and Future C# 2.0 and Future DirectionsDirections
Anders HejlsbergAnders HejlsbergTechnical FellowTechnical FellowMicrosoft CorporationMicrosoft Corporation
AgendaAgenda
What’s new in .NET 2.0What’s new in .NET 2.0
C# 2.0 LanguageC# 2.0 Language
Visual Studio 2005Visual Studio 2005
The LINQ ProjectThe LINQ Project
Increasing Level ofIncreasing Level ofAbstractionAbstraction
MicroMicro
Computer
Computer
ss
PersonalPersonal
Computers
Computers
Graphical
Graphical
User User
Interfaces
Interfaces
The WebThe Web
Machine Machine
codecode
CCTurbo Turbo
Pascal Pascal
MS-BASIC
MS-BASIC
C++C++
DelphiDelphi
Visual Basic
Visual BasicHTML HTML
ScriptScript
19801980
19901990
19951995
20002000
XMLXML
Web Web
ServicesServices
.NET/
.NET/
C#C#JavaJava
What’s New in .NET 2.0?What’s New in .NET 2.0?
Common Language RuntimeCommon Language Runtime
Base Class LibraryBase Class Library
ADO.NET and XMLADO.NET and XML
ASP.NETASP.NETWeb Services and UIWeb Services and UI
Windows FormsWindows FormsRich Client UIRich Client UI
Visual StudioVisual Studio
C#C# VBVB C++C++ OthersOthers
New collectionsNew collectionsTyped Typed resources Serial resources Serial I/OI/Oand much and much more…more…
64-bit support64-bit supportEdit and Edit and ContinueContinueGenericsGenericsand much and much more…more…
ClickOnceClickOnceLayout panelsLayout panelsDataGridViewDataGridViewand much and much more…more…
Multiple Multiple resultsetsresultsetsAsync Async operationsoperationsCompiled XSLTCompiled XSLTand much and much more…more…
VS Team VS Team SystemSystemExpress Express editionseditionsProductivityProductivityand much and much more…more…
Master pagesMaster pagesPersonalizationPersonalizationMembershipMembershipand much and much more…more…
GenericsGenericsPartial classesPartial classesNullable typesNullable typesand much and much more…more…
Performance!Performance!Feedback driven feature setFeedback driven feature set
C# 2.0 EnhancementsC# 2.0 Enhancements
GenericsGenerics
Anonymous methodsAnonymous methods
Nullable value typesNullable value types
IteratorsIterators
Partial typesPartial types
and much more…and much more…
100% backwards compatible100% backwards compatible
public class Listpublic 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 * if (count == elements.Length) Resize(count * 2);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; } }}}}
GenericsGenericspublic class Listpublic class List<<TT>>{{ private private TT[] elements;[] elements; private int count;private int count;
public void Add(public void Add(TT element) { element) { if (count == elements.Length) Resize(count * if (count == elements.Length) Resize(count * 2);2); elements[count++] = element;elements[count++] = element; }}
public public TT this[int index] { 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();List intList = new List();
intList.Add(1);intList.Add(1);intList.Add(2);intList.Add(2);intList.Add("Three");intList.Add("Three");
int i = (int)intList[0];int i = (int)intList[0];
List intList = new List();List intList = new List();
intList.Add(1);intList.Add(1); // Argument is boxed// Argument is boxedintList.Add(2);intList.Add(2); // Argument is boxed// Argument is boxedintList.Add("Three");intList.Add("Three"); // Should be an error// Should be an error
int i = (int)intList[0];int i = (int)intList[0]; // Cast required// Cast required
ListList<int><int> intList = new List intList = new List<int><int>();();
intList.Add(1);intList.Add(1); // No boxing// No boxingintList.Add(2);intList.Add(2); // No boxing// No boxingintList.Add("Three");intList.Add("Three"); // Compile-time error// Compile-time error
int i = intList[0];int i = intList[0]; // No cast required// No cast required
GenericsGenerics
Why generics?Why generics?Type checking, no boxing, no downcastsType checking, no boxing, no downcasts
Increased sharing (typed collections)Increased sharing (typed collections)
How are C# generics implemented?How are C# generics implemented?Instantiated at run-time, not compile-Instantiated at run-time, not compile-timetime
Checked at declaration, not instantiationChecked at declaration, not instantiation
Work for both reference and value typesWork for both reference and value types
Exact run-time type informationExact run-time type information
GenericsGenerics
Collection classesCollection classes
Collection interfacesCollection interfaces
Collection base classesCollection base classes
Utility classesUtility classes
ReflectionReflection
List<T>List<T>Dictionary<K,V>Dictionary<K,V>SortedDictionary<K,VSortedDictionary<K,V>>Stack<T>Stack<T>Queue<T>Queue<T>
IList<T>IList<T>IDictionary<K,V>IDictionary<K,V>ICollection<T>ICollection<T>IEnumerable<T>IEnumerable<T>IEnumerator<T>IEnumerator<T>IComparable<T>IComparable<T>IComparer<T>IComparer<T>
Collection<T>Collection<T>KeyedCollection<T>KeyedCollection<T>ReadOnlyCollection<TReadOnlyCollection<T>>
Nullable<T>Nullable<T>EventHandler<T>EventHandler<T>Comparer<T>Comparer<T>
Anonymous MethodsAnonymous Methods
Delegates written in-lineDelegates written in-line
delegate bool Predicate<T>(T item);delegate bool Predicate<T>(T item);
public class List<T>public class List<T>{{ public List<T> FindAll(Predicate<T> public List<T> FindAll(Predicate<T> filter) {filter) { List<T> result = new List<T>();List<T> result = new List<T>(); 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 Bankpublic class Bank{{ List<Account> accounts;List<Account> accounts;
List<Account> GetOverdrawnAccounts() {List<Account> GetOverdrawnAccounts() { return accounts.FindAll(return accounts.FindAll(new new Predicate<Account>(IsOverdrawn)Predicate<Account>(IsOverdrawn));); }}
public static bool IsOverdrawn(Account a) {public static bool IsOverdrawn(Account a) { return a.Balance < 0;return a.Balance < 0; }}}}
public class Bankpublic class Bank{{ List<Account> accounts;List<Account> accounts;
List<Account> GetOverdrawnAccounts() {List<Account> GetOverdrawnAccounts() { return accounts.FindAll(return accounts.FindAll(delegate(Account a) { return delegate(Account a) { return a.Balance < 0; }a.Balance < 0; });); }}}}
Generics PerformanceGenerics Performance
Nullable Value TypesNullable Value Types
System.Nullable<T>System.Nullable<T>Provides nullability for any value typeProvides nullability for any value type
Struct that combines a T and a boolStruct that combines a T and a bool
public struct Nullable<T> where T: structpublic struct Nullable<T> 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 {…} }}}
123123
intint
123123
Nullable<int>Nullable<int>
truetrue
??????
falsefalse
Non-nullNon-null NullNull
Nullable Value TypesNullable Value Types
T? syntaxT? syntax
null literalnull literal
ConversionsConversions
Lifted operatorsLifted operators
Null coalescingNull coalescing
int? x = 123;int? x = 123;double? y = double? y = 1.0;1.0;
int? x = null;int? x = null;double? y = double? y = null;null;
int i = 123;int i = 123;int? x = i;int? x = i;int j = (int)x;int j = (int)x;
int? x = int? x = GetNullableInt();GetNullableInt();int? y = int? y = GetNullableInt();GetNullableInt();int? z = x + y;int? z = x + y;
int? x = int? x = GetNullableInt();GetNullableInt();int i = x ?? 0;int i = x ?? 0;
IteratorsIterators
Methods that incrementally compute Methods that incrementally compute and return a sequence of valuesand return a sequence of values
class Programclass Program{{ static IEnumerable<int> Range(int start, int count) {static IEnumerable<int> Range(int start, int count) { for (int i = 0; i < count; i++) for (int i = 0; i < count; i++) yield returnyield return start + i; start + i; }}
static IEnumerable<int> Squares(IEnumerable<int> static IEnumerable<int> Squares(IEnumerable<int> source) {source) { foreach (int x in source) foreach (int x in source) yield returnyield return x * x; x * x; }}
static void Main() {static void Main() { foreach (int i in Squares(Range(0, 10))) foreach (int i in Squares(Range(0, 10))) Console.WriteLine(i);Console.WriteLine(i); }}}}
00114499161625253636494964648181
Partial TypesPartial Typespublic public partialpartial class Customer class Customer{{ private int id;private int id; private string name;private string name; private string address;private string address; private List<Orders> orders;private List<Orders> orders;}}
public public partialpartial class Customer 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 Customerpublic class Customer{{ private int id;private int id; private string name;private string name; private string address;private string address; private List<Orders> orders;private List<Orders> 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; }}}}
Other New FeaturesOther New Features
Static classesStatic classes
Property accessibility controlProperty accessibility control
External aliasesExternal aliases
Namespace alias qualifiersNamespace alias qualifiers
Inline warning controlInline warning control
Fixed size buffersFixed size buffers
Visual Studio 2005Visual Studio 2005
Developer Developer productivityproductivity
Write codeWrite code
Navigate codeNavigate code
Refactor codeRefactor code
Debug codeDebug code
Team productivityTeam productivity
Source code Source code controlcontrol
Build codeBuild code
Visualize codeVisualize code
Migrate codeMigrate code
Productivity
Productivity
Visual Studio 2005Visual Studio 2005
Problem:Problem:Data != ObjectsData != Objects
The LINQ ProjectThe LINQ Project
StandardStandardQueryQuery
OperatorsOperators
ObjectsObjects
DLinqDLinq XLinqXLinq
<book> <title/> <author/> <year/> <price/></book>
XMLXML
.NET Language Integrated Query.NET Language Integrated Query
C#C# VBVB Others…Others…
RelationalRelational
LINQ and C# 3.0LINQ and C# 3.0
Benefits Of LINQBenefits Of LINQ
Unified querying of objects, Unified querying of objects,
relational, XMLrelational, XML
Type checking and IntelliSense for Type checking and IntelliSense for
queries queries
SQL and XQuery-like power in C# SQL and XQuery-like power in C#
and VBand VB
Extensibility model for languages / Extensibility model for languages /
APIsAPIs
http://msdn.microsoft.com/netframework/future/linq/http://msdn.microsoft.com/netframework/future/linq/
© 2005 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.