I've been coding in VB for quite awhile and I can do plenty in C# and F# as well, but one syntax difference between VB and C# continues to throw me off. I'd Google this, but I'm not sure what to call it exactly. Consider the following examples:
In visual basic I would do this:
Dim Request As HttpWebRequest = HttpWebRequest.Create("www.google.com")
However, when I make what seems to be the "logical" conversion to C#:
HttpWebRequest Request = HttpWebRequest.Create("www.google.com");
I get the implicit type conversion error. After looking at some other code I realized this seems to be the proper way to do this:
HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create("www.google.com");
But I'm not exactly clear on what purpose the additional mention of the HttpWebRequest type in the parenthesis accomplishes. Is this some sort of casting syntax I didn't know about? What's going on here that makes this work, and the direct conversion not?
For starters, yes, that syntax is an explicit-cast (sometimes called a C-Style cast).
The reason you need it here is pretty simple. The Create
method is actually inherited from the WebRequest
class, and it returns a WebRequest
(not HttpWebRequest
) object. Note its lack of existence in HttpWebRequest
and the signature of Create
Assigning to a HttpWebRequest
variable then requires downcasting, which is never guaranteed to be safe, so you have to explicitly cast it.
Note that the following code would also compile.
WebRequest Request = HttpWebRequest.Create("www.google.com");
As others have said, the HttpWebRequest.Create
method does not return an object cast as an HttpWebRequest
type. Instead, it returns an HttpWebRequest
object cast as its base WebRequest
type. As you have discovered, in C#, you are required to explicitly cast the object to the desired type.
Your confusion, however, is that you assume that you don't need to perform the cast operation in VB.NET. The only reason that you don't need to cast in VB.NET is because you have Option Strict Off
. If you turned Option Strict On
in VB.NET, as you most likely should, then you would be required to perform the cast, just as in C#:
Dim Request As HttpWebRequest = DirectCast(HttpWebRequest.Create("www.google.com"), HttpWebRequest)
This is the compiler error as displayed by Visual Studio when you have Option Strict On
:
Option Strict On disallows implicit conversions from 'System.Net.WebRequest' to 'System.Net.HttpWebRequest'.
When you have Option Strict Off
, VB.NET will perform the widening conversion for you without any warning. This can be convenient, but it shuts off some of the compiler type-checking which could catch some bugs for you. My rule of thumb is to always turn Option Strict On
except in places where you need it Off
.
In other words, by turning Option Strict On
, as is recommended by most people, VB.NET's type-checking works the same as C#'s. If you don't want to have to cast the object in C#, the you could accomplish similar things by using the var
or dynamic
keywords, but I wouldn't recommend it in most cases. Type-checking is your friend. The fact that the syntax for type-casting in C# is easier just makes it all the more convenient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With