1 / 11

New Features in C# 2.0

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.

reneeg
Download Presentation

New Features in 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. New Features in C# 2.0 Sergey Baidachni MCT, MCSD, MCDBA

  2. C# ver. 2.0 • Generics • Iterators • Partial types • Anonymous method

  3. Generics • Array of object? • What is it? • How to create Generics

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

  5. 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];

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

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

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

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

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

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

More Related