1 / 22

Introduction to C# 2.0

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.

Ava
Download Presentation

Introduction to C# 2.0

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 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? 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) { … } }

  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 • Classes • Struct • Interface

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

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

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

  15. 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]; } } }

  16. 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)) {…}

  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() { IO::Stream s = new IO::File.OpenRead("foo.txt"); 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; public string CustomerId { get { return id; } 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 The C# Programming Language Programming Microsoft Visual C# 2005, The Language

  21. 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

More Related