Other news? async and await Anonymous types (var, dynamic) Tuples Object instantiation Extension methods UCN Teknologi/act2learn1FEN 2014
Asynchronous calls in C# Two new keywords making it easier to do asynchronous calls (methods running in separate threads) have been added to the new version of C# (5.0,.NET 4.5, VS 2012): – async – await UCN Teknologi/act2learn2 demos\CS-code\Async-Web Example by Joe Hummel, PhD FEN 2014
Asynchronous calls in C# The sequential version blocks until the web response is received: UCN Teknologi/act2learn3FEN 2014
Asynchronous calls in C# Methods accessing the remote site are declared async. It will run in its own thread. The return type must be Task. The await suspends the caller until the called method returns. UCN Teknologi/act2learn4FEN 2014
Anonymous types var is strongly typed: – The compiler infers the type. – Used with LINQ (and also misused a lot) dynamic is weakly typed: – Typing is done runtime. – Dangerous! – Useful when interacting with for working with python code, or old VB6 / COM objects, or… – Great for working with objects created through reflection View demos\VarVsDynamicdemos\VarVsDynamic UCN Teknologi/act2learn5FEN 2014
Tuple Tuples represent – tuples (up to 8 values) Can be used when a method wants to return more than one value (for instance a part of an object or a database row). View demos\TupleExampledemos\TupleExample Can also be achieved using structs, but remember: structs are value types. UCN Teknologi/act2learn6FEN 2014
Anonymous types Can be used as an alternative to tuples. Fields may be named View demos\AnoTypesdemos\AnoTypes UCN Teknologi/act2learn7FEN 2014
Easy object instantiation An easy way to call a default constructor View demos\ObjectCreationdemos\ObjectCreation Requires read/write properties. UCN Teknologi/act2learn8FEN 2014
Extension methods An easy way to add a method to an existing class (assemply). View demos\ExtensionMethodExampledemos\ExtensionMethodExample Note, we are not violating encapsulation. The extension method can only access public methods and properties of the class. UCN Teknologi/act2learn9FEN 2014
Other New stuff Highlights… – System.Diagnostics.Contracts: view exampleview example – System.Numerics.BigInteger – System.Numerics.Complex – System.Collections.Concurrent : queue, stack, dictionary, … – … UCN Teknologi/act2learn10FEN 2014
Is it good news? Generics Delegates and lambdas TPL async and await Anonymous types (var, dynamic) Tuples Object instantiation Extension methods System.Diagnostics.Contracts System.Numerics.BigInteger System.Numerics.Complex System.Collections.Concurrent UCN Teknologi/act2learn11FEN 2014
Tak for denne gang UCN Teknologi/act2learn12FEN 2014