Creating a generic method in C

0 votes

I'm attempting to integrate several comparable techniques into a single generic method. I have a few methods that return the value of a querystring, or null if the querystring doesn't exist or isn't formatted properly. This would be simple if all of the types were natively nullable, however for numbers and dates, I have to use the nullable generic type.

Here's what I've got right now. However, if a numeric number is invalid, it will return a 0, which is regrettably an acceptable value in my cases. Is there anyone who can assist me? 

public static T GetQueryString<T>(string key) where T : IConvertible
{
    T result = default(T);

    if (String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]) == false)
    {
        string value = HttpContext.Current.Request.QueryString[key];

        try
        {
            result = (T)Convert.ChangeType(value, typeof(T));  
        }
        catch
        {
            //Could not convert.  Pass back default value...
            result = default(T);
        }
    }

    return result;
}
Jun 11, 2022 in C# by pranav
• 2,590 points
557 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C#

0 votes
1 answer

Is a Library a project or a C# source code file in Visual Studio?

A library project is a collection of ...READ MORE

answered Jun 7, 2022 in C# by rajiv
• 1,620 points
476 views
0 votes
1 answer

Encapsulation of subclasses from a constructor in C#

If you think about it in terms ...READ MORE

answered Jun 11, 2022 in C# by pranav
• 2,590 points
712 views
0 votes
1 answer

How do I turn a C# object into a JSON string in .NET

Newtonsoft.json can be used to accomplish this. ...READ MORE

answered Jun 13, 2022 in C# by krishna
• 2,820 points
719 views
0 votes
1 answer

How to find the extension of a file in C#

Go to here https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getextension?redirectedfrom=MSDN&view=net-6.0#System_IO_Path_GetExtension_System_String_ string myFilePath = @"C:\MyFile.txt"; string ext ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
641 views
0 votes
1 answer

What does the extension method do in c# and why do you need it

Extension Methods allow you to add additional ...READ MORE

answered Jun 21, 2022 in C# by krishna
• 2,820 points
654 views
0 votes
0 answers

Learning C from a background in C#

I'd like to increase my programming expertise, ...READ MORE

Jun 11, 2022 in C# by jyoti
• 1,240 points
319 views
0 votes
1 answer

Why isn't Array a generic type

They basically copied the concept of arrays ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
481 views
0 votes
1 answer

What is cool about generics, why use them

Okay. For simplification, I am listing it ...READ MORE

answered Jun 23, 2022 in C# by krishna
• 2,820 points
496 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
5,723 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,114 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP