Generic method for HTTP GET/POST parameter conversion I just made a method that should handle GET and POST parameters (primitive types) sent by AJAX requests to the server:

protected T GetParam&lt;T&gt;(string key) 
{
    string value = HttpContext.Current.Request[key];

    if (string.IsNullOrEmpty(value))
        return default(T);

    return (T)Convert.ChangeType(value, typeof(T));
}


Do you have any suggestions regarding improvements?
First point, small but important: give your method a proper name. GetParam doesn't mean much and doesn't tell what it returns. Take a name like GetValueFromParameter or GetHttpRequestValue.

Secondly, did you test this method properly? I changed the code a bit to be able to test it, I removed the HttpContext value and just use the parameter as value:

protected T GetHttpRequestValue&lt;T&gt;(string value) 
{
    if (string.IsNullOrEmpty(value))
        return default(T);

    return (T)Convert.ChangeType(value, typeof(T));
}


When I call it like this:

var longFromString = GetHttpRequestValue&lt;long?&gt;("0");


I get following exception:


  Invalid cast from 'System.String' to 'System.Nullable`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.


I went looking for a cause and it seems that Convert.ChangeType has problems with nullable types. You have to use the underlying type of the nullable in order to make it work:

protected T GetHttpRequestValue&lt;T&gt;(string key) 
{
    string value = HttpContext.Current.Request[key];

    if (string.IsNullOrEmpty(value))
        return default(T);

    var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
    var convertedValue = (value == null) ? null : Convert.ChangeType(value, type);
    return (T)convertedValue ;
}


Now I get following results:

GetHttpRequestValue&lt;long?&gt;("0");    // 0
GetHttpRequestValue&lt;long?&gt;("");     // null
GetHttpRequestValue&lt;long&gt;("1");     // 1
GetHttpRequestValue&lt;bool&gt;("true");  // True
GetHttpRequestValue&lt;bool&gt;("");      // False

GetHttpRequestValue&lt;long?&gt;(null);   //null
GetHttpRequestValue&lt;bool&gt;(null);    //False


You also made your method dependent of an HttpContext, you cannot reuse it. Better would be to rename it to something like ConvertTo and leave out the HttpContext-line:

protected T ConvertTo&lt;T&gt;(string value) 
{
    if (string.IsNullOrEmpty(value))
        return default(T);

    var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
    var convertedValue = (value == null) ? null : Convert.ChangeType(value, type);
    return (T)convertedValue;
}


Example usage:

protected void Page_Load(object sender,EventArgs e)
{
    var value = HttpContext.Current.Request["YourKey"];
    var converted = ConvertTo&lt;long?&gt;(value);
}


Your code is still clean and the method is reusable. Notice that I use var instead of explicitly declaring variables. This is not mandatory, I just prefer to use it. Hope this helps!

Update:

I updated the method to handle a fail on the conversion, in case you want to change qwerty to a bool for example. :)

protected T ConvertTo&lt;T&gt;(string value) 
{
    if (String.IsNullOrEmpty(value))
        return default(T);

    var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
    T convertedValue = default(T);

    try
    {
        convertedValue = (T)Convert.ChangeType(value, type);
    }
    catch {}

    return convertedValue ;
}


This way of working swallows the exception. In case one occurs the default of T will be returned, otherwise the converted value. Results:

ConvertTo&lt;int&gt;("qwerty");    //0
ConvertTo&lt;bool&gt;("qwerty");   //False


Update:

As stated in the comments below, you can also take the code and throw it in an extension method. Also nice for reusability and your could will also look a bit cleaner. Here goes:

public static class Extensions
{
    public static T ConvertTo&lt;T&gt;(this string value) 
    {
        if (String.IsNullOrEmpty(value))
            return default(T);

        var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
        T convertedValue = default(T);

        try
        {
            convertedValue = (T)Convert.ChangeType(value, type);
        }
        catch {}

        return convertedValue ;
    }
}


Usage:

var intFromString = "2".ConvertTo&lt;int&gt;();
//intFromString is an integer with value 2