News In The Net40

29
News in C# and NET 4.0 Iaşi, 20 februarie 2010 Florin Cardaşim, www.rms.ro

description

These are the slides for the "News in Net4.0" presentation from 20th of feb 2010 in Iasi at Ramada (details at codecamp.ro)

Transcript of News In The Net40

Page 1: News In The Net40

News in C# and NET 4.0

Iaşi, 20 februarie 2010

Florin Cardaşim, www.rms.ro

Page 2: News In The Net40

Agenda

News that we’ll cover today

• Core• C# named & default params• DLR: dynamic, BigInteger, Tuple• Generics co&contra-variance• Corrupted State Exceptions• Dump debugging with VS2010• Code contracts• App Domain profiling• Garbage collector enhancements• Parallel Computing

• Workflow Foundation• Communication Foundation• ASP.NET 4.0 MVC• Visual Studio 2010

News for you to explore

• Improved security model• Application compatibility and

deployment• Networking• Web - ASP.NET4.0• Client• Data

• ADO.NET• Dynamic Data

• Programming languages (C++,VB, F#)

Page 3: News In The Net40

C# 4 named and default params Named method parameters Default method parameter values

Page 4: News In The Net40

C# 4 & DLR Interop via Dynamic Language Runtime

Common Language Runtime

Statically-Typed

C#

VB

RubyPython

Dynamically-Typed

Dynamic Language Runtime

Page 5: News In The Net40

C# 4 & the DLR: dynamic Interop with dynamic languages like Ruby,

Python

Interop with COM

Page 6: News In The Net40

C# 4 – DLR Support: dynamic

Interop always comes

with a performance cost,

so do not use dynamic if

you don’t really need it

Page 7: News In The Net40

C# 4 – DLR Support: dynamic Don’t abuse dynamic in C# because you loose:

• compile time type checking and intellisense• runtime performance

Use dynamic where appropriate

Page 8: News In The Net40

C# 4 & the DLR

System.Numerics.BigInteger System.Numerics.Complex System.Tuples

Tuple.Create(1,2,3,4,5,6,7,8) - maximum 8 members!! Property names are Item1, Item2, .., Item8 – ugly! Can be used as method params and return values Can be inherited Can be extended by extension methods Specifically useful in F# I’ll probably NOT use it in C# http://msdn.microsoft.com/en-us/vcsharp/

ee957397.aspx

Page 9: News In The Net40

Generics Covariance & Contravariance

Page 10: News In The Net40

Generics Co&Contravariance

http://msdn.microsoft.com/en-us/library/dd799517(VS.100).aspx

Page 11: News In The Net40

Exceptions Management This code is bad! WHY?!!!

Page 12: News In The Net40

Exceptions Management

• What is the problem? (Exception ex) might be a Corrupted State Exception (CSE) ,

like Access Violation, Invalid Memory, Divide By Zero, Stack Overflow

• What to do if Exception is CSE? Your process and data are in danger. ABORT. Are other actions possible?

• What is CLR 4.0 doing? CLR 4.0 will NOT deliver CSEs to our code by default catch(Exception ex) will NOT catch the CSE Your process will ABORT

Page 13: News In The Net40

Exceptions Management For backwards compatibility use

Or at application level

Page 14: News In The Net40

Exceptions Management A bit of history:

• CLR1.1: unhandled exceptions (on secondary threads) will NOT abort process

• CLR 2.0: unhandled exception on secondary will ABORT the process

• CLR 4.0: CSEs not handled by catch(Exception), process will ABORT

Conclusions:• Do not handle exceptions if you don’t know how• ABORT is better than a corrupted process

Page 15: News In The Net40

Exceptions Management Conclusions:

• Only catch exceptions that you can handle• ABORT is better than a corrupted process• If your process ABORTs than there is a bug

somewhere. GO FIX IT!• If you really need CSEs in your code use:

[HandleProcessCorruptedStateExceptions]Or

legacyCorruptedStateExceptionsPolicy enabled="true“

Page 16: News In The Net40

Net 4.0 application dump debugging When do we need a dump

Crash, hang, performance analysis etc

How to collect a dump adplus.vbs, DebugDiag etc Task Manager/Create Dump File VS2010/Debug/Save Dump As…

How to analyze the dump Until now windbg only for native apps Now VS2010 knows to debug .NET 4.0 app dump

Page 17: News In The Net40

Net 4.0 dump debugging

Dump debugging with WinDbg

Page 18: News In The Net40

Net 4.0 dump debugging

Page 19: News In The Net40

Code Contracts Until CLR 4.0

• Code comments• Debug.Assert, Argument validation: if-then-throw• Unit tests?

In CLR 4.0 we have System.Diagnostics.Contracts• Preconditions

• Required before a method can run

• Postconditions• True at the completion of a method

• Object invariants• Conditions that are always true

Page 20: News In The Net40

App Domain Profiling

• A host application may profile the activity of the hosted applications (for example add-ins) and attempt to restart it if takes too many resources

DEMO: “MonitoringIsEnabled”

Page 21: News In The Net40

Enhanced GC

Concurrent GC

• Can NOT do Gen0, Gen1 GC (ephemeral segment) at the same time with Gen2 GC

Page 22: News In The Net40

Enhanced GC

Backgound GC

• CAN do a Gen0, Gen1 GC (ephemeral segment) while doing Gen2 GC

Page 23: News In The Net40

Enhanced GC Concurrent GC

Background GC

Latency during application execution

Page 24: News In The Net40

Parallel Computing in .NET 4 Why should we care about parallel computing?

Because we care about application performance Because our CPUs/cores are not used at full capacity

Unified cancellation model• CancellationTokenSource, CancellationToken

System.Threading• Barrier• CountdownEvent

Parallel Extensions• Parallel LINQ (PLINQ)• Task Parallel Library (TPL)•Coordination Data Structures – System.Colllectiions.Concurrent

Page 25: News In The Net40

Demo: System.Threading, UCM

Unified cancellation model• CancellationTokenSource, CancellationToken

System.Threading

• Barrier• CountdownEvent

Parallel Extensions• Parallel LINQ (PLINQ)• Task Parallel Library (TPL)•Coordination Data Structures (CDS)

Page 26: News In The Net40

Demo: Parallel LINQ

Unified cancellation model• CancellationTokenSource, CancellationToken

System.Threading

• Barrier• CountdownEvent

Parallel Extensions• Parallel LINQ (PLINQ)• Task Parallel Library (TPL)•Coordination Data Structures (CDS)

Page 27: News In The Net40

Demo: Task Parallel Library

Unified cancellation model• CancellationTokenSource, CancellationToken

System.Threading

• Barrier• CountdownEvent

Parallel Extensions• Parallel LINQ (PLINQ)• Task Parallel Library (TPL)•Coordination Data Structures (CDS)

Page 28: News In The Net40

References

MSDN MSDN Magazine Channel9 VS2010 Training Kit Msdn.blogs.com/Tess

Page 29: News In The Net40

Please fill the evaluation form !!!

Thank you very much

Florin Cardasim• [email protected]• www.rms.ro• www.codecamp.ro• www.Twitter.com/cardasim