Do you want callers to be able to mutate the dictionary? If so, having a static one sounds like a very odd idea. If not, you really only need to be able to response to requests for Accept and AcceptCharset, which I'd probably do in a simple switch statement.
You say you really want to be use a Hashtable - why? What's the bigger picture here?
Exposing mutable data structures statically is almost always a bad idea. If you want a helper to build a hashtable with some initial values, then I'd make it a method rather than a property. If you don't need mutation, I'd write a method to fetch the value for a specific HttpRequestHeader
rather than exposing a collection. For example:
public static class HTTP
{
public static string GetHeaderString(HttpRequestHeader header)
{
// Use a dictionary here if you want. The API is the important bit
switch (header)
{
case HttpRequestHeader.Accept: return "Accept";
case HttpRequestHeader.AcceptCharset: return "Accept-Charset";
default: throw new KeyNotFoundException(header.ToString());
}
}
}
Another option would be to have a Java-like enum of headers:
public sealed class RequestHeader
{
public static RequestHeader Accept = new RequestHeader("Accept");
public static RequestHeader AcceptCharset =
new RequestHeader("Accept-Charset");
private readonly string name;
private RequestHeader(string header)
{
this. name = name;
}
public string Name
{
get { return name; }
}
}
You'd need to do checks against null
, but that would be the only invalid value of RequestHeader that you could get. (Enums aren't range-checked, so someone could easily write ((HttpRequestHeader)-1)
in your current code... in other words, it doesn't fix argument validation anyway.)
EDIT: In response to the comment, if you're using C# 3 and want eager initialization (to make life easier) you could write:
public static class HTTP
{
private static readonly Dictionary<HttpRequestHeader, string> Headers =
new Dictionary<HttpRequestHeader, string>
{
( HttpRequestHeader.Accept, "Accept" ),
( HttpRequestHeader.AcceptCharset, "Accept-Charset" )
};
public static string GetHeaderString(HttpRequestHeader header)
{
return Headers[header];
}
}