Visual Studio 2010 and .NET Framework 4 Training Workshop
description
Transcript of Visual Studio 2010 and .NET Framework 4 Training Workshop
Visual Studio 2010and
.NET Framework 4
Training Workshop
What’s New InC# 4.0 and Visual Basic
10NameTitleOrganizationEmail
Essence versus Ceremony
Simple, Concise, Expressive
Ceremony in C# 3.0Removing “magic values” via temporary
variables
GenerateChart(20, true);
var processCount = 20; var copyToWord = true; GenerateChart(processCount, copyToWord);
Ceremony in C# 3.0Removing “magic values” via temporary
variables
GenerateChart(20, true);
GenerateChart(20 /* processCount */, true /* copyToWord */);
Ceremony in C# 3.0Optional parameters via
method overloading
void GenerateChart(int processCount) { GenerateChart(processCount, false); }
void GenerateChart(int processCount, bool copyToWord) { // Do Something }
Ceremony in C# 3.0Ugly COM Interop and the ever-present
“ref Missing.Value”
var word = new Word.Application(); word.Documents.Add(ref Missing.Value, ref Missing.Value, ref Missing.Value, ref Missing.Value);
Essence vs. Ceremony in C# 4.0
Ceremony in VB 9Backing fields for properties are explicit
Private m_name As String
Public Property Name() As String Get Name = m_name End Get Set (ByVal value As String) m_name = value End End Property
Ceremony in VB 9Popularity of lambda-based libraries cause
problems for VB developers
Sub MyMethod() LambdaCall(Function(i) TempMethod(i) End Function) End Sub
Function TempMethod(Dim param as Integer) As Nothing Console.WriteLine(param)
Return Nothing End Function
Introduction of temporary functions
“Hacks” since statement lambdas not supported
Ceremony in VB 9Line continuations must be specified
by the developer
SomeLongMethodCall(firstParam, _ secondParam, _ thirdParam, _ fourthParam, _ fifthParam)
Essence vs. Ceremony in VB 10
Why a “Dynamic Language Runtime”?
Common Language Runtime
Statically-Typed
C#VB
RubyPython
Dynamically-Typed
Why a “Dynamic Language Runtime”?
Common Language Runtime
Statically-Typed
C#VB
RubyPython
Dynamically-Typed
Dynamic Language Runtime
Ceremony in C# 3.0Dynamic Language interop is
painful
Calculator calc = GetCalculator(); int sum = calc.Add(10, 20);
Ceremony in C# 3.0Dynamic Language interop is
painful
object calc = GetCalculator(); Type calcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 }); int sum = Convert.ToInt32(res);
Ceremony in C# 3.0Dynamic Language interop is
painful
ScriptObject calc = GetCalculator(); object res = calc.Invoke("Add", 10, 20); int sum = Convert.ToInt32(res);
Essence in C# 4.0Dynamic Language interopdoesn’t have to be painful!
dynamic calc = GetCalculator(); int sum = calc.Add(10, 20);
Statically typed to be
dynamic
Dynamic method
invocationDynamic
conversion
The power of late-binding
New Features in C# 4.0 & VB 10
Feature VB10 C#4Auto-implemented PropertiesCollection InitializersStatement LambdasImplicit Line Continuation N/ANamed/Optional ParametersLatebinding support (dynamic)Omit ref on COM calls
New in Dev10Already exists in VB9/C#3
New Features in C# 4.0 & VB 10
Feature VB10 C#4Auto-implemented PropertiesCollection InitializersStatement LambdasImplicit Line Continuation N/ANamed/Optional ParametersLatebinding support (dynamic)Omit ref on COM callsInterop with Dynamic LanguagesCo/contravariancePIA deployment not needed
New in Dev10Already exists in VB9/C#3
New Features in C# 4.0 & VB 10
Feature VB10 C#4Auto-implemented PropertiesCollection InitializersStatement LambdasImplicit Line Continuation N/ANamed/Optional ParametersLatebinding support (dynamic)Omit ref on COM callsInterop with Dynamic LanguagesCo/contravariancePIA deployment not neededIteratorsXML Literals
New in Dev10Already exists in VB9/C#3
Fixing The Type System…
Covariance and Contravariance…
sounds complicated…
But it’s not!sort of…
Fixing The Type System…How are generic types “broken” today?
var sheep = new List<Sheep>(); Speak(sheep);
void Speak(IEnumerable<Animal> animals) { // Do something with Animals }
class Animal { } class Sheep : Animal { }
Not Allowed:IEnumerable<Animal> != IEnumerable<Sheep>
Break it down…
Covariance – think “out”
Contravariance – think “in”
Fixing The Type System…Covariance and Contravariance
Sounds complicated!
http://tinyurl.com/genericvariance
Essence versus Ceremony