Introduction to c 2 0
Download
1 / 22

- PowerPoint PPT Presentation


  • 258 Views
  • Updated On :

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '' - Ava


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Introduction to c 2 0 l.jpg
Introduction to C# 2.0

An Advanced Look

Adam Calderon

Principal Engineer - Interknowlogy

Microsoft MVP – C#


Agenda l.jpg
Agenda

  • Static Classes

  • Generics

  • Partial Types

  • Anonymous Methods

  • Iterators

  • Namespace Alias Qualifier

  • Property Accessor Accessibility


Static classes l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
Various other Generic Classes

  • System.Collections.ObjectModel classes

    • Collection<T>

    • KeyedCollection<T>

    • ReadOnlyCollection<T>

  • Various Other Classes

    • Nullable<T>

    • EventHandler<T>

    • Comparer<T>


Generics8 l.jpg

Generics

Introduction to C# 2.0


Partial types l.jpg
Partial Types

  • Ability to break up declaration into multiple files

  • Types supported

    • Classes

    • Struct

    • Interface


Partial classes l.jpg
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 l.jpg
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 methods12 l.jpg
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 methods13 l.jpg

Anonymous Methods

Introduction to C# 2.0


Iterators l.jpg
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);

}


Iterators15 l.jpg
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];

}

}

}


Iterators16 l.jpg
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)) {…}


Iterators17 l.jpg

Iterators

Introduction to C# 2.0


Namespace alias qualifier l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
Adam Calderon

More info on InterKnowlogywww.InterKnowlogy.com

Contact Information

E-mail: [email protected]

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


ad