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

Slides:



Advertisements
Similar presentations
Developer Knowledge Sharing Eric Sun Dec, What programming language did you learn in school and since then? Now, its time to refresh …
Advertisements

The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
1 Visual C# "Whidbey": Language Enhancements Anders Hejlsberg Distinguished Engineer Microsoft Corporation Anders Hejlsberg Distinguished.
Developing Event Driven State Machine Workflows S1 S2 S3 S4 Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#
LINQ and Collections An introduction to LINQ and Collections.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Language Fundamentals in brief C# - Introduction.
Static Members, Structures, Enumerations, Generic Classes, Namespaces Learning & Development Team Telerik Software Academy.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
DEV340 Microsoft Visual C# Under the Covers: An In-Depth Look at C# 2.0 Anders Hejlsberg Distinguished Engineer Microsoft Corporation.
Overview of Visual C# 2005 Henrik Westergaard Hansen
New Features in C# 2.0 Sergey Baidachni MCT, MCSD, MCDBA.
Introduction to the C# Programming Language for the VB Programmer.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
C#C# C#C# Kit Colbert Student Consultant representing Microsoft
What’s New in Visual Studio 2005: Security &.NET Framework 2.0 Mike Pelton Developer & Platform Group Microsoft Ltd.
Iterator Pattern Dr. Neal CIS 480. Iterator An iterator pattern can be used when one class is a collection of things and would like to provide a standardized.
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Adam Calderon – C# MVP Application Development Practice Lead Interknowlogy.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
.NET 2.0 and Visual Studio 2005 SigWin Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods.
Introduction to Building Windows 8.1 & Windows Phone Applications.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Jon Shemitz Complicated stuff, quickly..NET 2.0. ● Generics50% & Nullable Types ● Iterators 10% ● Delegate Enhancements35% ● Partial Types5%
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Visual C# 2005: IDE Enhancements Dan Fernandez C# Product Manager
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Adam Calderon – C# MVP Application Development Practice Lead Interknowlogy.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
[ISRAR ALI] Hammad Khan. The namespace keyword is used to declare a scope. Making software components reusable can result in naming collisions (two classes.
C Sharp Web & Internet Programming Group Diana, Aren, Jeff, & Adam, Farrin 5/5/20081CS 311.
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Advanced C#, part I Niels Hallenberg IT University of Copenhagen BAAAP – Spring 2009.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
Introduction to Object-Oriented Programming Lesson 2.
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
© Copyright SELA software & Education Labs Ltd Baruch Hirsch St.Bnei Brak Israel 1 בס " ד.
1 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Advanced .NET Programming I 2nd Lecture
Advanced .NET Programming I 4th Lecture
Chapter 5: Programming with C#
MGB 2003 Adam Calderon – C# MVP Principal Engineer Interknowlogy
Visual C# "Whidbey": Language Enhancements
Visual C# 2005: Language Enhancements
DEV321 Visual C# 2005: Language Enhancements
Getting Ready for Visual Studio 2005
Advanced .NET Programming I 4th Lecture
Visual C# 2005: Language Enhancements
Presentation transcript:

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 Sin(double x) {…} public static double Cos(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? 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) { … }}

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 ClassesStructInterface

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

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

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

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

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

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

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

Resources Links Books Programming Microsoft Visual C# 2005, The Language The C# Programming Language

Adam Calderon More info on InterKnowlogy More info on InterKnowlogy Contact Information Contact Information Phone: x274 Blog: About Adam Calderon Microsoft ® MVP – C# Microsoft ® UI Server Frameworks Advisory Council Developer / Author / Speaker