Introduction to C 2 0 An Advanced Look

  • Slides: 22
Download presentation
Introduction to C# 2. 0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy

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

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

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)

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> Can be used with various

How are they used? class Dictionary<K, V>: IDictionary<K, V> Can be used with various types where K: IComparable<K> where V: IKey. Provider , IPersistable, new() Class, struct , interface and<K> delegate { class Dictionary<K, V> {…} void Add(K key, V value) { public … } struct Hash. Bucket<K, V> {…} } interface IComparer<T> class {…} Utils One base class, multiple interfaces, new() { public delegate R Function<A, R>(A arg); static T[] Create. Array(int size) { return new T[size]; } Can be used with methods, parameters and return types Support the concept of constraints } public static void Sort. Array<T>(T[] array) { … }

Generic Collections and Interfaces System. Collections. Generic classes List<Item. Type> Dictionary<K, V> Stack<Item. Type>

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

Various other Generic Classes System. Collections. Object. Model classes Collection<T> Keyed. Collection<T> Read. Only.

Various other Generic Classes System. Collections. Object. Model classes Collection<T> Keyed. Collection<T> Read. Only. Collection<T> Various Other Classes Nullable<T> Event. Handler<T> Comparer<T>

Generics Introduction to C# 2. 0

Generics Introduction to C# 2. 0

Partial Types Ability to break up declaration into multiple files Types supported Classes Struct

Partial Types Ability to break up declaration into multiple files Types supported Classes Struct Interface

Partial Classes public partial class Customer { public class Customer private int id; private

Partial Classes public partial class Customer { public class Customer private int id; private string name; { private string address; private int id; private string name; private List<Orders> orders; private string address; } private List<Orders> orders; public partial class Customer public void Submit. Order(Order order) { { orders. Add(order); public void Submit. Order(Order order) { } orders. Add(order); } public bool Has. Outstanding. Orders() { return orders. Count > 0; public bool Has. Outstanding. Orders() { } return orders. Count}> 0; } }

Anonymous Methods Allows code block in place of delegate Delegate type automatically inferred Code

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 { Message. Box. Show("Hello"); }; button. Click += delegate(object sender, Event. Args e) { Message. Box. Show(((Button)sender). Text); };

Anonymous Methods Code block can access local variables Lifetime of the outer variable extends

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. Write. Line("Copy #: {0}", ++n); };

Anonymous Methods Introduction to C# 2. 0

Anonymous Methods Introduction to C# 2. 0

Iterators foreach relies on “enumerator pattern” Get. Enumerator() method foreach (object obj in list)

Iterators foreach relies on “enumerator pattern” Get. Enumerator() method foreach (object obj in list) { Do. Something(obj); } Enumerator e = list. Get. Enumerator(); while (e. Move. Next()) { object obj = e. Current; Do. Something(obj); } foreach makes enumerating easy But enumerators are hard to write!

Iterators Method that incrementally computes and returns a sequence of values yield return and

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 Get. Enumerator() { for (int i = 0; i < count; i++) { yield return elements[i]; } } }

Iterators public class List<T> { public IEnumerator<T> Get. Enumerator() { for (int i =

Iterators public class List<T> { public IEnumerator<T> Get. Enumerator() { 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 = Get. Item. List(); 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

Iterators Introduction to C# 2. 0

Namespace Alias Qualifier A: : B looks up A only as namespace alias global:

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. Open. Read("foo. txt"); global: : System. Console. Write. Line("Hello"); }

Property Accessor Accessibility Allows one accessor to be restricted further Typically set {…} more

Property Accessor Accessibility Allows one accessor to be restricted further Typically set {…} more restricted than get {…} public class Customer { private string id; public string Customer. Id { get { return id; } internal set { id = value; } } }

Resources Links http: //msdn. microsoft. com/vcsharp/programming/language/ http: //www. csharp-corner. com/ Books The C# Programming

Resources Links 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 Inter. Knowlogy www. Inter. Knowlogy. com Contact Information E-mail:

Adam Calderon More info on Inter. Knowlogy www. Inter. Knowlogy. com Contact Information E-mail: [email protected] Knowlogy. com Phone: 760 -930 -0075 x 274 Blog: http: //blogs. Inter. Knowlogy. com/Adam. Calderon About Adam Calderon Microsoft® MVP – C# Microsoft® UI Server Frameworks Advisory Council Developer / Author / Speaker