Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

Similar presentations


Presentation on theme: "Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#"— Presentation transcript:

1 Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#

2 Agenda Static Classes Generics Partial Types Anonymous Methods Iterators Namespace Alias Qualifier Property Accessor Accessibility

3 Static Classes Can contain only static members No instance members 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) {…} …}

4 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

5 How are they used? Can be used with various types Class, struct, interface and delegate Can be used with methods, parameters and return types Support the concept of constraints One base class, multiple interfaces, new() class Dictionary {…} struct HashBucket {…} interface IComparer {…} delegate R Function (A arg); class Utils { public static T[] CreateArray(int size) { public static T[] CreateArray(int size) { return new T[size]; return new T[size]; } public static void SortArray (T[] array) { public static void SortArray (T[] array) { … }} 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) { … }}

6 Generic Collections and Interfaces System.Collections.Generic classes List<ItemType>Dictionary<K,V>Stack<ItemType>Queue<ItemType> System.Collections.Generic interfaces IList<ItemType>IDictionary<K,V>ICollection<ItemType>IEnumerable<ItemType>IEnumerator<ItemType>IComparable<OperandType>IComparer<OperandType>

7 Various other Generic Classes System.Collections.ObjectModel classes Collection<T>KeyedCollection<T>ReadOnlyCollection<T> Various Other Classes Nullable<T>EventHandler<T>Comparer<T>

8 Generics Introduction to C# 2.0

9 Partial Types Ability to break up declaration into multiple files Types supported ClassesStructInterface

10 Partial Classes 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; }}

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

12 Anonymous Methods int n = 0; Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); }; Code block can access local variables Lifetime of the outer variable extends until the delegates that reference the anonymous methods are eligible for garbage collection Code block cannot access the ref or out parameters of an outer scope.

13 Anonymous Methods Introduction to C# 2.0

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

15 Iterators Method that incrementally computes and returns a sequence of values yield return and yield break Must return IEnumerator or IEnumerable 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() { public IEnumerator GetEnumerator() { for (int i = 0; i < count; i++) { yield return elements[i]; for (int i = 0; i < count; i++) { yield return elements[i]; } }}

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

17 Iterators Introduction to C# 2.0

18 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() { 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"); }

19 Property Accessor Accessibility Allows one accessor to be restricted further Typically set {…} more restricted than get {…} 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; } }}

20 Resources Links http://msdn.microsoft.com/vcsharp/ http://msdn.microsoft.com/vcsharp/programming/language/ http://www.csharp-corner.com/ Books Programming Microsoft Visual C# 2005, The Language The C# Programming Language

21 Adam Calderon More info on InterKnowlogy www.InterKnowlogy.com More info on InterKnowlogy www.InterKnowlogy.com www.InterKnowlogy.com Contact Information Contact Information E-mail: adamc@InterKnowlogy.com adamc@InterKnowlogy.com Phone: 760-930-0075 x274 Blog: http://blogs.InterKnowlogy.com/AdamCalderon http://blogs.InterKnowlogy.com/AdamCalderon About Adam Calderon Microsoft ® MVP – C# Microsoft ® UI Server Frameworks Advisory Council Developer / Author / Speaker

22


Download ppt "Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#"

Similar presentations


Ads by Google