110 likes | 124 Views
Learn about the new features introduced in C# 2.0, including generics, iterators, partial types, and anonymous methods. Explore the benefits and limitations of these features, and discover how they can improve your programming experience.
E N D
New Features in C# 2.0 Sergey Baidachni MCT, MCSD, MCDBA
C# ver. 2.0 • Generics • Iterators • Partial types • Anonymous method
Generics • Array of object? • What is it? • How to create Generics
Array of object? • Boxing and Unboxing ArrayList list=new ArrayList(); list.Add(4); list[0]=(int)list[0]+1; • Explicit conversion int j; j=(int)list[0]; //unboxing • Problem with performance and exceptions
Generics (What is it?) • Templates in C++ • No problem with performance List<int> list=new List[int](); list.add(2); list.add(“table”); //compilation error • No problem with conversion int j; j=list[0];
How create Generics (base syntaxes) • Please use any name for your type class MyClass<ItemType> { private ItemType item; } • Limitation: You can use only methods inherited from object
Generics (extensions) • Use Where in order to extend your possibilities Class MyClass<T1,T2> where T1: IMyInterface where T2: IComparable {…..} • Interfaces public interface IMyInterface<T> { void WhatIsIt(T i); }
Iterators • Enumerators vs. Iterators • yield keyword public IEnumerator GetEnumerator() { for(int i = 1;i< 5;i++) { yield return i; if(i > 2) yield break; } } • Benifits
Partial Types • One class – many files? • partial keyword //first file (MyClass_1.cs) public partial class MyClass { private int nCount; . . . . . } //second file (MyClass_2.cs) public partial class MyClass { private bool isPresent . . . . . }
Anonymous method • Too lazy to write a separate method? There’s no need to. Button b=new Button(); b.Click+= new Button.ClickEventHandler(object s,EventArgs e) { Console.WriteLine(“Hello”); }; • You can use local variable • new? Who needs it?
Books • .NET Framework: Secret tips on Windows Applications Creation by Sergey Baidachni • .NET Framework: Secret tips on Web-application creation by Sergey Baidachni (coming up soon)