News in C# and NET 4.0Iaşi, 20 februarie 2010Florin Cardaşim, www.rms.ro
Agenda
C# 4 named and default paramsNamed method parametersDefault method parameter values
C# 4 & DLRInterop via Dynamic Language RuntimeDynamically-TypedRubyPythonStatically-TypedVBDynamic Language RuntimeC#Common Language Runtime
C# 4 & the DLR: dynamicInterop with dynamic languages like Ruby, PythonInterop with COM
C# 4 – DLR Support: dynamicInterop always comes with a performance cost, so do not use dynamic if you don’t really need it
C# 4 – DLR Support: dynamicDon’t abuse dynamic in C# because you loose:compile time type checking and intellisense
runtime performanceUse dynamic where appropriate
C# 4 & the DLRSystem.Numerics.BigIntegerSystem.Numerics.ComplexSystem.TuplesTuple.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 valuesCan be inheritedCan be extended by extension methodsSpecifically useful in F#I’ll probably NOT use it in C# http://msdn.microsoft.com/en-us/vcsharp/ee957397.aspx
Generics Covariance & Contravariance
Generics Co&Contravariancehttp://msdn.microsoft.com/en-us/library/dd799517(VS.100).aspx
Exceptions ManagementThis code is bad!WHY?!!!
Exceptions ManagementWhat is the problem?(Exception ex) might be a Corrupted State Exception (CSE) , like Access Violation, Invalid Memory, Divide By Zero, Stack OverflowWhat 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 defaultcatch(Exception ex) will NOT catch the CSEYour process will ABORT
Exceptions ManagementFor backwards compatibility useOr at application level
Exceptions ManagementA 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 ABORTConclusions:Do not handle exceptions if you don’t know how
ABORT is better than a corrupted processExceptions ManagementConclusions: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“
Net 4.0 application dump debuggingWhen do we need a dumpCrash, hang, performance analysis etcHow to collect a dumpadplus.vbs, DebugDiag etcTask Manager/Create Dump FileVS2010/Debug/Save Dump As…How to analyze the dumpUntil now windbgonly for native appsNow VS2010 knows to debug .NET 4.0 app dump
Net 4.0 dump debuggingDump debugging with WinDbg
Net 4.0 dump debugging
Code ContractsUntil CLR 4.0Code comments
Debug.Assert, Argument validation: if-then-throw
Unit tests?	In CLR 4.0 we have System.Diagnostics.ContractsPreconditions
Required before a method can run
Postconditions

News In The Net40

  • 1.
    News in C#and NET 4.0Iaşi, 20 februarie 2010Florin Cardaşim, www.rms.ro
  • 2.
  • 3.
    C# 4 namedand default paramsNamed method parametersDefault method parameter values
  • 4.
    C# 4 &DLRInterop via Dynamic Language RuntimeDynamically-TypedRubyPythonStatically-TypedVBDynamic Language RuntimeC#Common Language Runtime
  • 5.
    C# 4 &the DLR: dynamicInterop with dynamic languages like Ruby, PythonInterop with COM
  • 6.
    C# 4 –DLR Support: dynamicInterop always comes with a performance cost, so do not use dynamic if you don’t really need it
  • 7.
    C# 4 –DLR Support: dynamicDon’t abuse dynamic in C# because you loose:compile time type checking and intellisense
  • 8.
  • 9.
    C# 4 &the DLRSystem.Numerics.BigIntegerSystem.Numerics.ComplexSystem.TuplesTuple.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 valuesCan be inheritedCan be extended by extension methodsSpecifically useful in F#I’ll probably NOT use it in C# http://msdn.microsoft.com/en-us/vcsharp/ee957397.aspx
  • 10.
    Generics Covariance &Contravariance
  • 11.
  • 12.
  • 13.
    Exceptions ManagementWhat isthe problem?(Exception ex) might be a Corrupted State Exception (CSE) , like Access Violation, Invalid Memory, Divide By Zero, Stack OverflowWhat 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 defaultcatch(Exception ex) will NOT catch the CSEYour process will ABORT
  • 14.
    Exceptions ManagementFor backwardscompatibility useOr at application level
  • 15.
    Exceptions ManagementA bitof history:CLR1.1: unhandled exceptions (on secondary threads) will NOT abort process
  • 16.
    CLR 2.0: unhandledexception on secondary will ABORT the process
  • 17.
    CLR 4.0: CSEsnot handled by catch(Exception), process will ABORTConclusions:Do not handle exceptions if you don’t know how
  • 18.
    ABORT is betterthan a corrupted processExceptions ManagementConclusions:Only catch exceptions that you can handle
  • 19.
    ABORT is betterthan a corrupted process
  • 20.
    If your processABORTs than there is a bug somewhere. GO FIX IT!
  • 21.
    If you reallyneed CSEs in your code use: [HandleProcessCorruptedStateExceptions]Or legacyCorruptedStateExceptionsPolicy enabled="true“
  • 22.
    Net 4.0 applicationdump debuggingWhen do we need a dumpCrash, hang, performance analysis etcHow to collect a dumpadplus.vbs, DebugDiag etcTask Manager/Create Dump FileVS2010/Debug/Save Dump As…How to analyze the dumpUntil now windbgonly for native appsNow VS2010 knows to debug .NET 4.0 app dump
  • 23.
    Net 4.0 dumpdebuggingDump debugging with WinDbg
  • 24.
    Net 4.0 dumpdebugging
  • 25.
    Code ContractsUntil CLR4.0Code comments
  • 26.
  • 27.
    Unit tests? In CLR4.0 we have System.Diagnostics.ContractsPreconditions
  • 28.
    Required before amethod can run
  • 29.