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

  • Home
  • SEARCH
  • 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 8305513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:07:57+00:00 2026-06-08T18:07:57+00:00

I would like to have a method that will parse from a nullable database

  • 0

I would like to have a method that will parse from a nullable database column an enum.
I wrote this method below (and had to restrict T to a struct to make it compile).

It does compile, but I believe its wrong as Enums are not structs?
If so, how do I restrict the generic method to say I am expecting a ValueType you don’t have to complain at me that “Only non-nullable value type could be underlying of ‘System.Nullable’

private static T? ParseEnum<T>(DataRow row, string columnName)
    where T : struct
{
    T? value = null;
    try
    {
        if (row[columnName] != DBNull.Value)
        {
            value = (T)Enum.Parse(
                typeof(T),
                row[columnName].ToString(),
                true);
        }
    }

    catch (ArgumentException) { }

    return value;
}
  • 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-08T18:07:58+00:00Added an answer on June 8, 2026 at 6:07 pm

    Unfortunately there’s no constraint available in C# that allows you to restrict that a given type is an enum. In IL there’s such notion though. Jon blogged about it.

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

Sidebar

Related Questions

I have a script with a factory method that I would like to return
I have a HtmlHelper extension method that I would like to apply some logic
I have a static method in my code that I would like somehow to
I have an activity that launches another activity with startActivityForResult method. I would like
This will be a bit abstract but I have a method like this: private
I have a WCF service with many methods. I would like that after executing
I would like to have a method along the lines of setData:(SomeClassName *)data inPosition:(NSInteger)position
I have a method where I would like to return an object instance of
I have a method which I would like to invoke asynchronously: void Foo() {
I have a action result method i would like to run. How can i

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.