220 likes | 545 Views
Introduction to C# 2.0. An Advanced Look. Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#. Agenda. Static Classes Generics Partial Types Anonymous Methods Iterators Namespace Alias Qualifier Property Accessor Accessibility. Static Classes.
E N D
Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#
Agenda • Static Classes • Generics • Partial Types • Anonymous Methods • Iterators • Namespace Alias Qualifier • Property Accessor Accessibility
Static Classes • Can contain only static members • No instance members public static class Math { public static double Sin(double x) {…} public static double Cos(double x) {…} … }
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
How are they used? class Dictionary<K,V>: IDictionary<K,V> where K: IComparable<K> where V: IKeyProvider<K>, IPersistable, new() { public void Add(K key, V value) { … } } • 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<K,V> {…} struct HashBucket<K,V> {…} interface IComparer<T> {…} delegate R Function<A,R>(A arg); class Utils{ public static T[] CreateArray(int size) { return new T[size]; } public static void SortArray<T>(T[] array) { … } }
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>
Various other Generic Classes • System.Collections.ObjectModel classes • Collection<T> • KeyedCollection<T> • ReadOnlyCollection<T> • Various Other Classes • Nullable<T> • EventHandler<T> • Comparer<T>
Generics Introduction to C# 2.0
Partial Types • Ability to break up declaration into multiple files • Types supported • Classes • Struct • Interface
Partial Classes 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; } }
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);};
Anonymous Methods • 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. int n = 0; Del d = delegate() { System.Console.WriteLine("Copy #:{0}", ++n); };
Anonymous Methods Introduction to C# 2.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); } Enumerator e = list.GetEnumerator(); while (e.MoveNext()) { object obj = e.Current; DoSomething(obj); }
Iterators • 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]; } } }
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)) {…}
Iterators Introduction to C# 2.0
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"); }
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; } } }
Resources Links http://msdn.microsoft.com/vcsharp/ http://msdn.microsoft.com/vcsharp/programming/language/ http://www.csharp-corner.com/ Books The C# Programming Language Programming Microsoft Visual C# 2005, The Language
Adam Calderon More info on InterKnowlogywww.InterKnowlogy.com Contact Information E-mail: adamc@InterKnowlogy.com Phone: 760-930-0075 x274 Blog: http://blogs.InterKnowlogy.com/AdamCalderon About Adam Calderon • Microsoft® MVP – C# • Microsoft® UI Server Frameworks Advisory Council • Developer / Author / Speaker