I'm not sure where you got the idea that the C# code you provided was *the*
C# code for what you're doing. This would work just as well:
HttpWebRequest httpRequest;
httpRequest =
WebRequest.Create("http://www.winisp.net/goodrich/default.htm");
-or -
HttpWebRequest httpRequest =
WebRequest.Create("http://www.winisp.net/goodrich/default.htm");
The "(HttpWebRequest)" is a cast. It is casting the result of the method as
an HttpWebRequest. However, it is unnecessary in this case. The
WebRequest.Create method returns an instance of one of the classes that
inherit WebRequest, depending upon the URL passed to it. In this case, it
returns an HttpWebRequest, so it is not necessary to cast it.
The equivalent in VB would be:
httpRequest = CType(WebRequest.Create( _
"http://www.winisp.net/goodrich/default.htm"), HttpWebRequest)
And as you've already observed, that is not necessary.
It is important to note that while VB.Net and C# are doing the same things
under the covers (they both compile to MSIL), the syntax and process may
differ from time to time. For example, in C#, there is a distinct different
between casting and converting (which is actually true). VB.Net hides this
by using the CType() (and related) methods, which are conversion methods.
The difference between casting and converting is a bit murky. Basically,
casting in C# is done when an instance of a class is of the same type or an
inherited type of another class. For example, take the ICloneable interface.
This interface defines a single method (Clone()) which is defined as
returning a type of Object. This is because it can be implemented by any
class, and the interface cannot know what type of class it will be
implemented on. So, in the implementation of ICloneable for any class, the
return type is going to be object. However, as the Clone method makes a copy
of a class, it will return an instance of the cloned class. Because of
strong typing, you must explicitly cast the returned instance as the class
that it (already)is:
Bitmap bmp = Bitmap.FromFile(filePath)
Bitmap bmpClone = (Bitmap)bmp.Clone();
Otherwise, it will be treated as an Object (which it is), but the extra
members of the Bitmap class will not be available, as they are not part of
the Object class. In other words, an object is what it is, but for other
objects and code to know how to work with it, they have to know what it is
also. Casting is how they are told.
Conversion is used when the types are not inherited from one another, but
can be converted with a little work. For example, an Int32 and a String are
2 entirely different types, with little in common. But an integer can always
be represented as a string, so you can convert an integer into a string,
using the ToString method, or the Convert class.
Another difference is that converting does not return the original object,
but an object created by the conversion. Casting simply puts a "sign" on the
object that advertises how it is to be treated.
As for the "ellipsis," I didn't see any. An ellipsis is a series of several
marks that indicates an omission in text. Did you mean parentheses? If so,
you should get used to the fact that C# comes from a family of languages
(the C family) which are well-known for being succinct (compact). The
parentheses in this case simply indicate a cast.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist
What You Seek Is What You Get.
"hharry" <pa*********@nyc.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
hello all,
switching to c# from vb.net and had quick syntax question...
in vb:
Dim httpRequest as HttpWebRequest
httpRequest = WebRequest.Create( _
"http://www.winisp.net/goodrich/default.htm")
in c#:
HttpWebRequest httpRequest;
httpRequest = (HttpWebRequest)
WebRequest.Create("http://www.winisp.net/goodrich/default.htm");
================================================
what is the purpose of (HttpWebRequest) and why the ellipsis ?
thanks in advance