DEV340 Microsoft Visual C# Under the Covers: An In-Depth Look at C# 2.0 Anders Hejlsberg Distinguished Engineer Microsoft Corporation.

Slides:



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

1 Visual C# "Whidbey": Language Enhancements Anders Hejlsberg Distinguished Engineer Microsoft Corporation Anders Hejlsberg Distinguished.
 Anders Hejlsberg Technical Fellow Microsoft Corporation TL16.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Programming Languages and Paradigms The C Programming Language.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Static Members, Structures, Enumerations, Generic Classes, Namespaces Learning & Development Team Telerik Software Academy.
C# Types Tom Roeder CS fa. Administration CMS is up let me know if you can’t see the course Assignments are posted may not be able to do some.
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
Overview of Visual C# 2005 Henrik Westergaard Hansen
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
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.
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 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
DEV485.NET CodeDOM demystified Beat Schwegler Architect Evangelist.NET Developer Group Microsoft Corporation.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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%
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
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.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
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.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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
Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Session 1 C# Basics.
Visual Basic 2005: Advanced Language and IDE Features Amanda Silver Program Manager Visual Basic Session Code: DEV343.
Introduction to C# By: Abir Ghattas Michel Barakat.
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
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.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
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.
Advanced .NET Programming I 2nd Lecture
Introduction to .NET Generics
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2005
Upgrading Your C# Programming Skills to Be a More Effective Developer
Chapter 5: Programming with C#
Computing with C# and the .NET Framework
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Visual C# "Whidbey": Language Enhancements
Visual C# 2005: Language Enhancements
DEV321 Visual C# 2005: Language Enhancements
Getting Ready for Visual Studio 2005
Advanced .NET Programming I 4th Lecture
Visual C# 2005: Language Enhancements
Presentation transcript:

DEV340 Microsoft Visual C# Under the Covers: An In-Depth Look at C# 2.0 Anders Hejlsberg Distinguished Engineer Microsoft Corporation

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

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

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

Generics Type parameters can be applied to Class, struct, interface, delegate types class Dictionary {...} struct HashBucket {...} interface IComparer {...} delegate R Function (A arg); Dictionary customerLookupTable; Dictionary > orderLookupTable; Dictionary wordCount;

Generics Type parameters can be applied to Class, struct, interface, delegate types Methods class Utils { public static T[] CreateArray (T value, int size) { public static T[] CreateArray (T value, int size) { T[] result = new T[size]; T[] result = new T[size]; for (int i = 0; i < size; i++) result[i] = value; for (int i = 0; i < size; i++) result[i] = value; return result; return result; }} string[] names = Utils.CreateArray ("", 10); int[] numbers = Utils.CreateArray (-1, 100); string[] names = Utils.CreateArray("", 10); int[] numbers = Utils.CreateArray(-1, 100); Type inference!

Generics Type parameters can be applied to Class, struct, interface, delegate types Methods Type parameters can have constraints class Dictionary class Dictionary { public void Add(K key, V value) { public void Add(K key, V value) { if (((IComparable)key).CompareTo(x) == 0) {...} if (((IComparable)key).CompareTo(x) == 0) {...} }} class Dictionary where K: IComparable { public void Add(K key, V value) { public void Add(K key, V value) { if (key.CompareTo(x) == 0) {...} if (key.CompareTo(x) == 0) {...} }} class Dictionary : IDictionary class Dictionary : IDictionary where K: IComparable where K: IComparable where V: IKeyProvider, IPersistable, new() where V: IKeyProvider, IPersistable, new() { public void Add(K key, V value) { }}

Generics Zero or one primary constraint Actual class, class, or struct Zero or more secondary constraints Interface or type parameter Zero or one constructor constraint new() class Link where T: class {...} class Nullable where T: struct {...} class Relation where T: class where U: T {...}

Generics default(T)Casts Null checks void Foo () { T x = null; // Error T x = null; // Error T y = default(T); // Ok T y = default(T); // Ok} void Foo (T x) { int i = (int)x; // Error int i = (int)x; // Error int j = (int)(object)x; // Ok int j = (int)(object)x; // Ok} void Foo (T x) { if ((object)x == null) {...} if ((object)x == null) {...}}

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>

Anonymous Methods 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( return accounts.FindAll( new Predicate (IsOverdrawn) new Predicate (IsOverdrawn) ); ); } static bool IsOwerdrawn(Account a) { static bool IsOwerdrawn(Account a) { return a.Balance < 0; return a.Balance < 0; }} public class Bank { List accounts; List accounts; List GetOverdrawnAccounts() { List GetOverdrawnAccounts() { return accounts.FindAll( return accounts.FindAll( delegate(Account a) { return a.Balance < 0; } delegate(Account a) { return a.Balance < 0; } ); ); }}

Anonymous Methods Also known as functions Code block in place of delegate Delegate type automatically inferred Code block may omit parameter list button.Click += delegate { MessageBox.Show("Hello"); }; button.Click += delegate(object sender, EventArgs e) { MessageBox.Show(((Button)sender).Text); };

Anonymous Methods Closures are supported public class Bank { List accounts; List accounts; List GetOverdrawnAccounts() { List GetOverdrawnAccounts() { return accounts.FindAll( return accounts.FindAll( delegate(Account a) { return a.Balance < 0; } delegate(Account a) { return a.Balance < 0; } ); ); }} public class Bank { List accounts; List accounts; List GetOverdrawnAccounts() { List GetOverdrawnAccounts() { return accounts.FindAll( return accounts.FindAll( delegate(Account a) { return a.Balance < 0; } delegate(Account a) { return a.Balance < 0; } ); ); } List GetLargeAccounts(double minBal) { List GetLargeAccounts(double minBal) { return accounts.FindAll( return accounts.FindAll( delegate(Account a) { return a.Balance >= minBal; } delegate(Account a) { return a.Balance >= minBal; } ); ); }} public class Bank { List GetLargeAccounts(double minBal) { List GetLargeAccounts(double minBal) { Helper helper = new Helper(); Helper helper = new Helper(); helper.minBal = minBal; helper.minBal = minBal; return accounts.FindAll(helper.Matches); return accounts.FindAll(helper.Matches); } internal class Helper internal class Helper { internal double minBal; internal double minBal; internal bool Matches(Account a) { internal bool Matches(Account a) { return a.Balance >= minBal; return a.Balance >= minBal; } } } } }

Anonymous Methods Method group conversions Delegate type inferred when possible using System; using System.Threading; class Program { static void Work() {...} static void Work() {...} static void Main() { static void Main() { Thread t = new Thread(new ThreadStart(Work)); Thread t = new Thread(new ThreadStart(Work)); t.Start(); t.Start(); }} using System; using System.Threading; class Program { static void Work() {...} static void Work() {...} static void Main() { static void Main() { Thread t = new Thread(Work); Thread t = new Thread(Work); t.Start(); t.Start(); }}

Generics Performance

Nullable 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

Nullable 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; // T  T? implicit int j = (int)x; // T?  T explicit int? x = GetNullableInt(); int? y = GetNullableInt(); int? z = x + y; int? x = GetNullableInt(); int i = x ?? 0;

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); DoSomething(obj);} Enumerator e = list.GetEnumerator(); while (e.MoveNext()) { object obj = e.Current; object obj = e.Current; DoSomething(obj); DoSomething(obj);}

Iterators public class List { internal object[] elements; internal object[] elements; internal int count; internal int count; public IEnumerator GetEnumerator() { return new ListEnumerator(this); public IEnumerator GetEnumerator() { return new ListEnumerator(this); }} public class ListEnumerator : IEnumerator { List list; List list; int index; int index; internal ListEnumerator(List list) { internal ListEnumerator(List list) { this.list = list; this.list = list; index = -1; index = -1; } public bool MoveNext() { public bool MoveNext() { int i = index + 1; int i = index + 1; if (i >= list.count) return false; if (i >= list.count) return false; index = i; index = i; return true; return true; } public object Current { public object Current { get { return list.elements[index]; } get { return list.elements[index]; } }} public class List { internal object[] elements; internal object[] elements; internal int count; internal int count; public IEnumerator GetEnumerator() { for (int i = 0; i < count; i++) { public IEnumerator GetEnumerator() { for (int i = 0; i < count; i++) { yield return elements[i]; yield return elements[i]; } }}

public class Test { public IEnumerator GetEnumerator() { public IEnumerator GetEnumerator() { yield return "Hello"; yield return "World"; yield return "Hello"; yield return "World"; }} Iterators Method that incrementally computes and returns a sequence of values yield return and yield break Must return IEnumerator or IEnumerable public IEnumerator GetEnumerator() { return new __Enumerator(this); return new __Enumerator(this);} private class __Enumerator : IEnumerator { object current; object current; int state; int state; public bool MoveNext() { public bool MoveNext() { switch (state) { switch (state) { case 0: case 0: current = "Hello"; current = "Hello"; state = 1; state = 1; return true; return true; case 1: case 1: current = "World"; current = "World"; state = 2; state = 2; return true; default: return true; default: return false; return false; } } public object Current { public object Current { get { return current; } get { return current; } }}

public class List public class List { public IEnumerator GetEnumerator() { public IEnumerator GetEnumerator() { for (int i = 0; i < count; i++) for (int i = 0; i < count; i++) yield return elements[i]; yield return elements[i]; } public IEnumerable Descending() { public IEnumerable Descending() { for (int i = count - 1; i >= 0; i--) for (int i = count - 1; i >= 0; i--) yield return elements[i]; yield return elements[i]; } public IEnumerable Subrange(int index, int n) { public IEnumerable Subrange(int index, int n) { for (int i = 0; i < n; i++) for (int i = 0; i < n; i++) yield return elements[index + i]; yield return elements[index + i]; }} Iterators List items = GetItemList(); foreach (Item x in items) {...} foreach (Item x in items.Descending()) {...} foreach (Item x in Items.Subrange(10, 20)) {...}

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; }}

Static Classes Only static members Cannot be used as type of variable, parameter, field, property, … Examples include System.Console, System.Environment public static class Math { public static double Sin(double x) {...} public static double Sin(double x) {...} public static double Cos(double x) {...} public static double Cos(double x) {...}......}

Property Accessors Different accessor accessibility One accessor can be restricted further Typically set {…} more restricted public class Customer { private string id; private string id; public string CustomerId { public string CustomerId { get { return id; } get { return id; } internal set { id = value; } internal set { id = value; } }}

External Aliases Enables use of identically named types in different assemblies namespace Stuff { public class Utils public class Utils { public static void F() {...} public static void F() {...} }} namespace Stuff { public class Utils public class Utils { public static void F() {...} public static void F() {...} }} foo.dll bar.dll extern alias Foo; extern alias Bar; class Program { static void Main() { static void Main() { Foo.Stuff.Utils.F(); Foo.Stuff.Utils.F(); Bar.Stuff.Utils.F(); Bar.Stuff.Utils.F(); }} C:\>csc /r:Foo=foo.dll /r:Bar=bar.dll test.cs

Namespace Alias Qualifiers Enables more version resilient code A::B looks up A only as alias global::X looks up in global namespace using IO = System.IO; class Program { static void Main() { static void Main() { IO::Stream s = new IO::File.OpenRead("foo.txt"); IO::Stream s = new IO::File.OpenRead("foo.txt"); global::System.Console.WriteLine("Hello"); global::System.Console.WriteLine("Hello"); }}

Inline Warning Control #pragma warning using System; class Program { [Obsolete] [Obsolete] static void Foo() {} static void Foo() {} static void Main() { static void Main() { #pragma warning disable 612 Foo(); Foo(); #pragma warning restore 612 }}

Fixed Size Buffers C style arrays in unsafe code public struct OFSTRUCT { public byte cBytes; public byte cBytes; public byte fFixedDisk; public byte fFixedDisk; public short nErrCode; public short nErrCode; private int Reserved; private int Reserved; public fixed char szPathName[128]; public fixed char szPathName[128];}

Want to know more? Whiteboard Cabana Talk Cabana 6, Monday 5:00pm – 6:15pm Book Signing Book Store, Tuesday 10:15am – 10:45am

Your Feedback is Important! Please Fill Out a Survey for This Session on CommNet

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