Each iteration of the .NET Framework brings more features into the language set with the intention of making.NET more powerful and easier to use, and the .NET 4.0 release continues the trend. As we look through the individual features, you should begin to see how Microsoft is incorporating language features from C# to VB.NET and vice-versa in pursuit of their promise of co-evolution.
Dynamic Lookup (New to C#)
As mentioned earlier, C# added a new static type named dynamic. Although there is a lot that goes into making this new type work, there's not a lot that goes into using it. You can think of the dynamic type as an object that supports late binding.
dynamic Car = GetCar(); //Get a reference to the dynamic object
Car.Model = "Mondeo"; //Assign a value to a property (also works for fields)
Car.StartEngine(); //Calling a method
Car.Accelerate(100); //Call a methods with parameters
dynamic Person = Car["Driver"]; //Getting with an indexer
Car["Passenger"] = CreatePassenger(); //Setting with an indexer
At compile time, calls to fields, properties, and methods of dynamic objects are essentially ignored. In other words, you won't get a compiler error about a member not being available. Since that information is only available at runtime, .NET knows to use the DLR to resolve the dynamic members. Now, C# is a statically typed language for a reason, and that reason is performance. The new dynamic type is not a licence to forgo static typing altogether; it's just another tool in case you need to interact with dynamic types. Also remember that VB.NET already supports Dynamic Lookups.
Named and Optional Parameters (New to C#)
Named and optional parameters have been around in VB.NET for quite some time, and they are finally being incorporated into C#. As the name suggests, optional parameters are parameters that you can opt to pass into a method or constructor. If you opt not to pass a parameter, the method simply uses the default value defined for the parameter. To make a method parameter optional in C#, you just give it a default value:
public void CreateBook(string title="No Title", int pageCount=0, string isbn = "0-00000-000-0")
this.Title = title;
this.PageCount = pageCount;
this.ISBN = isbn;
You can call the CreateBook method defined above in the following ways:
CreateBook("Some Book Title");
CreateBook("Some Book Title", 600);
CreateBook("Some Book Title", 600, "5-55555-555-5");
Notice that optional parameters are dependent on position. Title must appear as the first parameter, page count as the second, and ISBN as the third. If you wanted to call CreateBook and only pass in the ISBN number, then you have two options. The first is to create an overloaded method that accepts ISBN as a parameter, which is the time tested and tedious option. The second is to use named parameters, which is much more succinct. Named parameters allow you to pass parameters into a method in any order you want, as long as you provide the name of the parameter. So you can call the method in the following ways:
Please note that you can start with positional parameters, and then start using named parameters as shown in the second Create Book method call above, but once you start using Named Parameters you have to keep using them.
Anonymous Method Support (New to VB.NET)
Another long sought feature being introduced into VB.NET is the Inline or Anonymous Method. Anonymous Method is an appropriate name because it allows you to define Subs and Functions without needing to add a top-level method to your class, which keeps the method hidden (i.e. anonymous). Anonymous Methods also have access to all of the variables available to the code block in which the inline method resides, so you don't even have to bother defining method parameters to get data into and out of an anonymous method. You can define an anonymous method anywhere you would normally use the AddressOf keyword to point to a method, so the most prominent use is likely going to be on event handles, as demonstrated in the following example:
Dim MyTimer As New System.Timers.Timer(1000)
Dim Seconds As Integer = 0
Seconds += 1
Console.WriteLine(Seconds.ToString() & " seconds have elapsed")
Console.WriteLine("Press any key to exit")
Notice that the event handler for the Timer's Elapsed event is written inline, and the method has access to the Seconds variable defined outside of the inline sub. You can also define inline functions:
Dim f = Function(a As Integer, b As Integer)
Return a + b
Dim x = 10
Dim y = 20
Dim z = f(x, y)
Inline functions are great if a function makes sense in the context of a code block, but isn't really reusable enough to make it a class-level function.
Co-variance and Contra-variance (New to C# and VB.NET)
One of the most annoying issues with generics has been resolved in .NET 4.0. Previously, if you had an object that supports IEnumerable and you wanted to pass it into a method requiring an IEnumerable
Post a Comment