12.2. Implicit Typing

One of the significant aspects of the .NET Framework is the Common Type System, which was introduced in order to separate the concept of a data type from any specific language implementation. This system is used as the basis for all .NET languages, guaranteeing type safety (although VB.NET developers can elect to disable Option Strict and/or Option Explicit, which both reduces the level of static, or "compile-time," type verification and can lead to undesirable runtime behavior).

More recently there has been a move away from statically typed languages toward more dynamic languages where the type checking is done at runtime. While this can improve developer productivity and increase flexibility so that an application can evolve, it also increases the probability that unexpected errors will be introduced.

In a concerted effort to ensure that the .NET Framework remains as the platform of choice, it was necessary for both C# and VB.NET to incorporate some of the productivity features of these more dynamic languages. One such feature is the ability to infer type information based on variable usage. This is often referred to as type inferencing or implicit typing. In Figure 12-2 you can see that we have not defined the type of the variable bob, yet when we use the variable it is clearly defined as a Person object. As this is a compiler feature, we get the added benefit of IntelliSense, which indicates what methods, properties, and fields are accessible, as well as designer indicators if we use a variable in a way that is not type-safe.

Figure 12.2. Figure 12-2

As you can see in Figure 12-2, implicit typing in VB.NET reuses the existing Dim keyword to indicate that a variable is being declared. In C#, the usual format for declaring a variable is to enter the variable type followed by the variable name. However, when using implicit typing you do not want to specify the variable type, as it will be inferred by the compiler; instead, you use the Var keyword.

var bob = new Person { FirstName = "Bob", LastName = "Jane" };

Implicit typing can be used in a number of places throughout your code. In fact, in most cases where you would define a variable of a particular type you can now use implicit typing. For example, the compiler can infer that For statements are of the iteration variable type.

For Each p In people
    MessageBox.Show(p.FirstName & " is " & p.Age & " years old")
Next

You might wonder why you would want to use this feature, as you could easily specify the variable types in these examples. In Chapters 21 to 23 you will be introduced to language-integrated queries, and there you will see the real benefits of implicit typing.

It is important to note that using implicit typing does not reduce the static type checking of your code. Behind the scenes the compiler still defines your variables with a specific type that is verifiable by the runtime engine.

VB.NET only: With its background in VB6, VB.NET has a number of options that can be toggled depending on how you want the compiler to enforce strong typing. These are Option Strict, Option Explicit, and Option Infer, and the following table contains a subset of the different combinations, showing how they affect the way you can write code.

Table 12.1. Toggling Options in VB.NET
ExplicitStrictInferAllowedDisallowedDiscussion
OffOffOffw = 5 w is typed as an object, as it is not possible to infer the type.
OnOffOffDim w = 5w = 5w is still typed as an object; the only condition is that w must be declared by means of the Dim syntax.
OffOffOnDim w = 5 m = 6 In this case the type of w is inferred to be Integer but m remains an Object.
OnOffOnDim w = 5w = 5The type of w is inferred to be Integer.
OnOnOffDim w as Integer = 5w = 5 Dim w = 5The type of w is explicitly set to be Integer.
OnOnOnDim w as

Integer = 5

Dim w = 5
w = 5 Dim ww must be declared and either a type specified or a value specified from which to infer the type.

Essentially, the rules are that Option Explicit requires the variable to be declared by means of the Dim keyword. Option Strict requires that either the type of the variable is specified or can be inferred by the compiler, and Option Infer determines whether type inferencing is enabled. Note that disabling Option Infer can make working with Linq very difficult.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.199