12
.NET 2.0 and Visual .NET 2.0 and Visual Studio 2005 Studio 2005 SigWin 2004 SigWin 2004

NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

Embed Size (px)

Citation preview

Page 1: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

.NET 2.0 and Visual .NET 2.0 and Visual Studio 2005Studio 2005

SigWin 2004SigWin 2004

Page 2: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

OutlineOutline

Language enhancements in C#Language enhancements in C#– GenericsGenerics– Partial typesPartial types– Anonymous methodsAnonymous methods– Nullable typesNullable types

IDE featuresIDE features– Intellisense Intellisense – RefractoringRefractoring– Code snippetsCode snippets– Other awesome stuff!Other awesome stuff!

Page 3: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

GenericsGenerics

MotivationMotivation

Page 4: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

GenericsGenerics

One method:One method:public int AddSeconds(ArrayList myList) {public int AddSeconds(ArrayList myList) {

int total=0;int total=0;DateTime myDate;DateTime myDate;foreach (object myObj in myList) {foreach (object myObj in myList) {

myDate=(DateTime) myObj;myDate=(DateTime) myObj;total+=myDate.Second;total+=myDate.Second;

}}return total;return total;

}}

Page 5: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

GenericsGenerics

Other method is to derive your Other method is to derive your own collection class. Namespace own collection class. Namespace is now cluttered with many is now cluttered with many strongly typed collectionsstrongly typed collections

Answer to problem: GENERICS!!!!!Answer to problem: GENERICS!!!!!

Page 6: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

Generics: ExampleGenerics: Example

System.Collections.Generic:System.Collections.Generic:

private System.Collections.Generic.List<DateTime> private System.Collections.Generic.List<DateTime> myList;myList;

private Dictionary<string, Stack<DateTime>> private Dictionary<string, Stack<DateTime>> myList;myList;

Page 7: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

Generics: old exampleGenerics: old example

public int AddSeconds(List<DateTime> myList) { int total = 0; foreach (DateTime myDate in myList) total += myDate.Second; return total; }

Page 8: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

Partial typesPartial types

Easy way to separate Easy way to separate automatically generated code automatically generated code from your own:from your own:

public partial class myClass {}public partial class myClass {}

public partial class myClass {}public partial class myClass {}

Page 9: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

Anonymous methodsAnonymous methods

Review of delegatesReview of delegates Anonymous delegate=delegate Anonymous delegate=delegate

without a function name, just without a function name, just codecode

Page 10: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

Anonymous method Anonymous method example:example:

button1.Click += delegate { MessageBox.Show("Click"); };

Alternative:

button1.Click+=new EventHandler(myButton1_Click);

private void myButton1_Click(object sender, EventArgs e) {

MessageBox.Show(“Click”);

}

Page 11: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

Nullable typesNullable types

MotivationMotivation Syntax:Syntax:

int? myInt = null; if (myInt.HasValue) { //Do stuff here with myInt.Value } else { //It's null } int? d=1, f=null; Console.WriteLine(f ?? d);

Page 12: NET 2.0 and Visual Studio 2005 SigWin 2004. Outline Language enhancements in C# Language enhancements in C# –Generics –Partial types –Anonymous methods

Visual Studio 2005 IDEVisual Studio 2005 IDE

Demo!Demo!