I saw this piece of code:
var request = (HttpWebRequest) WebRequest.Create("http://www.google.com");
Why do you need to cast (HttpWebRequest)? Why not just use HttpWebRequest.Create? And why does HttpWebRequest.Create make a WebRequest, not a HttpWebRequest?
The
Createmethod is static, and exists only onWebRequest. Calling it asHttpWebRequest.Createmight look different, but its actually compiled down to callingWebRequest.Create. It only appears to be onHttpWebRequestbecause of inheritance.The
Createmethod internally, uses the factory pattern to do the actual creation of objects, based on theUriyou pass in to it. You could actually get back other objects, like aFtpWebRequestorFileWebRequest, depending on theUri.