Although in this book I am mainly covering C#, for completeness I will cover changes to VB.NET as well.
One of the aspects of VB.NET I really hated was the line continuation character (an underscore with a space before it). This particularly annoyed me when writing LINQ queries, because it made them less readable. Also, the underscore character is kind of awkward to type (OK, that's not that big an issue):
Dim numbers() As Integer = {1, 2, 3, 4, 5} Dim query = from n in numbers _ Select n _ Where n>5
In .NET 4.0, in the majority of situations, this will no longer be necessary, as the compiler is now clever enough to work out how lines will be split. This then allows the above LINQ query to be rewritten without the underscore character:
Dim query = from n in numbers Select n Where n>5
You can of course continue to use the line continuation character in your code so you will not have to do any modifications to existing projects. You may also need to use it in the unlikely situation that the compiler cannot infer where a line break exists.
According to Microsoft, the inferred line continuation works in the following situations:
After an attribute
After a comma
After a dot (such as for method invocation)
After a binary operator
After a LINQ query clause
After (, {, or <%=
Before ), }, or %>
Anonymous methods allow you to declare an inline function and can make code more readable. This can be done as follows (note how we don't need line continuation underscores):
Dim add = Function(x as integer, y as integer) Return x+y End function
It is very common to need to create private variables with public accessor methods. In VB.NET, you probably have written something like the following (although you probably haven't had the need to create a Tiger class):
Public Class Tiger Private _Name As String Property Name() As String Get Return _Name End Get Set(ByVal Name As String) _Name = Name End Set End Property End Class
You can now use the following syntax to get the compiler to generate a private backing field like in C#:
Public Class AdvancedTiger Property Name() As String End Class
The old syntax is of course still supported.
Adding items to a collection in VB was previously slightly tedious, and would be done with code such as the following:
Dim ThingsNotFoundInEmploymentAgents As New List(Of String) ThingsNotFoundInEmploymentAgents.Add("technical knowledge") ThingsNotFoundInEmploymentAgents.Add("honesty") ThingsNotFoundInEmploymentAgents.Add("a reflection")
Collections can now be initialized using the following syntax with the new From keyword:
Dim TraitsNotFoundInJobAgents As New List(Of String) From { "technical knowledge", "honesty", "a reflection" }
Array literals allow the compiler to infer an array. In the following example, the array will be automatically typed as an integer:
Dim myArray = {2, 3, 5}
Personally, I prefer to specify the type, as I think it makes your intention clearer, but the decision is now yours.
Until this release, you could not declare jagged arrays in VB.NET easily and would have to resort to code similar to the following:
Dim firstSetOfValues() As Integer = {1, 2, 3} Dim seondSetOfValues() As Integer = {4, 5} Dim allValues()() As Integer = {firstSetOfValues, seondSetOfValues}
However, you can now use the following syntax:
Dim allValuesNewWay = {({"1", "2", "3"}), ({"4", "5"})}
Optional parameters can now be nullable:
Public Sub MyFunction(ByVal Val1 As String, Optional ByVal z As Integer? = Nothing) End Sub
3.139.97.161