المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : The WebRequest class is abstract, so how are we able to instantiate an object from it?



C# Programming
05-31-2010, 08:14 AM
In the following code, which I have found to be common practice amongst the examples I have found on the web, the WebRequest class is instantiated like it's a regular class yet it's an abstract class. Underneath the hood, the http object is a really a HttpWebRequest object, but doesn't this defy the principle that you cannot instantiate abstract classes through it's syntax? This is the line that doesn't make sense to me: WebRequest http = WebRequest.Create(url);. WebRequest (abstract class) is being instantiated as object http. If the underlying class that is being instantiated is the HttpWebRequest than shouldn't that only be allowed?? such as HttpWebRequest http = HttpWebRequest.Create(url)?

class ScanUrl
{
/// /// Scan the URL and display headers.
/// /// The URL to scan. public StringBuilder Scan(String u)
{
StringBuilder sb = new StringBuilder();
Uri url = new Uri(u);
WebRequest http = WebRequest.Create(url);
WebResponse response = http.GetResponse();

int count = 0;
String key, value;

for (count = 0; count <span class="code-keyword">