Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9266723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:23:04+00:00 2026-06-18T14:23:04+00:00

This is pure curiosity/challenge, no practical importance at all. So I’m not looking for

  • 0

This is pure curiosity/challenge, no practical importance at all. So I’m not looking for alternate solutions that get the job done.

From this question Most efficient way to check for DBNull and then assign to a variable? I found this answer which looks like:

oSomeObject.IntMemeber = oRow["Value"] as int? ?? iDefault;
oSomeObject.StringMember = oRow["Name"] as string ?? sDefault;

Can I move the above expressions to one generic function (or two, or more) so that it accepts both int? and string as well and that I can call it like:

oSomeObject.IntMemeber = oRow.Read<int?>("Value", 0); //iDefault is now 0
//or
oSomeObject.IntMemeber = oRow.Read<int>("Value"); //iDefault is now default(int)

//and 
oSomeObject.StringMember = oRow.Read<string>("Name"); //sDefault is now default(string)

Requirements:

1) I need an option to specify a default value in case of DBNulls. I also need an option to return default(T) in case I dont specify a default value. So this wont work:

public static T Read<T>(this IDataRecord dr, string field, T defaultValue) where T : class
{
    return dr[field] as T ?? defaultValue;
}

public static T? Read<T>(this IDataRecord dr, string field, T? defaultValue) where T : struct
{
    return dr[field] as T? ?? defaultValue;
}

because I cant call oSomeObject.StringMemeber = oRow.Read<string>("Name")

It need not be optional parameter, it can even be an overload:

public static T Read<T>(this IDataRecord dr, string field) where T : class
{
    return dr[field] as T ?? default(T);
}

public static T? Read<T>(this IDataRecord dr, string field) where T : struct
{
    return dr[field] as T? ?? default(T?);
}

public static T Read<T>(this IDataRecord dr, string field, T defaultValue) where T : class
{
    return dr[field] as T ?? defaultValue;
}

public static T? Read<T>(this IDataRecord dr, string field, T? defaultValue) where T : struct
{
    return dr[field] as T? ?? defaultValue;
}

This wont compile, since method 1 and 2 have just the same signature.

2) Generic functions (in case of overloads) should have the same name.

3) as keyword must be used check and cast type. As I stated previously I’m not really looking to solutions to read from IDataRecord or enhance performance or something.


There are similar questions

  1. C# generic class using reference types and nullable value types

  2. Is creating a C# generic method that accepts (nullable) value type and reference type possible?

  3. Can a Generic Method handle both Reference and Nullable Value types?

But this is very specific to as keyword. So the answers there are not applicable.

Addendum: I know there won’t be one single solution to this. I will accept which is most elegant alternative.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:23:05+00:00Added an answer on June 18, 2026 at 2:23 pm

    The answers you linked to seem to pretty clearly indicate that there isn’t a good way to have a generic method handle both reference types and value types in the clean way you would like. It seems the answer, as shown there, is to give them different names.

    You can, however, reduce your code to just two methods instead of four by using a default value for the defaultValue parameter:

    public static T Read<T>(this IDataRecord dr, string field, 
                            T defaultValue = null) where T : class
    {
        return dr[field] as T ?? defaultValue;
    }
    
    public static T? ReadNullable<T>(this IDataRecord dr, string field, 
                                     T? defaultValue = null) where T : struct
    {
        return dr[field] as T? ?? defaultValue;
    }
    

    This way you can use dr.ReadNullable(field) when you want the result to be null when the value can’t be cast, and dr.ReadNullable(field, someValue) when you want to specify a default value.

    As discussed below, the use of default(T) and default(T?) in your original code aren’t necessary, since they will always be null anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question that arose mostly of pure curiosity (and killing some time).
This question exist only because of pure curiosity. Not a homework. Find the fastest
This is pure curiosity. Let's write an object: var display = { res1:{ w:640,
This question is for pure curiosity. Lets say I have lighttpd installed on my
This is not a pure programming question, however it impacts the performance of programs
I have this pure CSS hover that shows a div when you rollover a
While compiling on GCC I get the error: pure-specifier on function-definition , but not
I get this as pure HTML: <label for=txtPais>Pais:</label> <input name=ctl00$ContentPlaceHolder1$txtPais type=text id=ctl00_ContentPlaceHolder1_txtPais class=textInput />
This is pure curiosity... Most 'professionals' will likely never use Form1 as a valid
This question is pure curiosity. In Xcode, why does this work: if (view.class ==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.