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 6817717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:05:23+00:00 2026-05-26T21:05:23+00:00

I have a method with an IEnumerable<T> parameter. T can be one of the

  • 0

I have a method with an IEnumerable<T> parameter.
T can be one of the built-in .NET types like int or string or a custom class like this:

class MyClass
{
    public string Foo{ get; set; }
    public int Bar{ get; set; }
}

How can I recognize programmatically if T is one of the built-in types?

I know that I can do something like this:

switch (typeof(T).Name.ToLower())
{
    case "int":          
    case "string":
    case "...":   // and so on...
        Console.WriteLine("It's a built-in type!");
        break;
    default:
        Console.WriteLine("It's a custom class!");
        break;
}

…but there must be a shorter/simpler way, right?


EDIT:

Okay guys, thank you very much for the answers so far.
But I’m still not sure which one is the best for my situation.

What I actually want to do is this:
I’m writing a library to convert IEnumerable<T>s into ADODB.Recordsets.
At the beginning of each conversion, I need to create an empty Recordset and add fields to it.

If T is a custom class, I have to loop through its properties and create a field in the Recordset for each property of T (with the property’s name and type).

But looping through the properties only works properly if T is a custom class.
For example, it T is a string, I get the properties of the string (Chars and Length), which are of no use for me in this case.

This means that only checking if it’s a primitive type is not enough – I need to recognize things like DateTime and GUID as well, and there are probably even more.
(I have to admit, I didn’t notice that DateTime is not in the list of built-in types).

So I guess what I actually want is:
Tell if T has user-defined properties which I can loop, or not.
(no matter if it has no properties at all like int , or properties which I don’t care about like string has)
Does this make sense?

However, I’m still not sure which answer to pick.
driis’ and Jon Skeet’s answer both mean that I basically have to list a lot of types (more in Jon’s answer than driis’ answer).
At the moment, I tend to pick Ron Sijm’s answer (even though people apparently liked the other answers more), because I guess simply checking "System." is the shortest way to do what I want, even if it does not look, well, that elegant…

  • 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-05-26T21:05:23+00:00Added an answer on May 26, 2026 at 9:05 pm

    What you determine to be a “built-in type” is likely to be context-specific – you’ve listed the types built into the C# language, for example, but that really is specific to C#. It includes decimal (which isn’t a CLR primitive type) for example, but not DateTime (which other languages could support explicitly).

    So, I’d just use a HashSet<Type> which you create appropriately:

    private static readonly HashSet<Type> BuiltInTypes = new HashSet<Type>
    {
        typeof(object), typeof(string), typeof(byte), typeof(sbyte),
        // etc
    };
    
    // Then:
    if (BuiltInTypes.Contains(typeof(T)))
    {
        Console.WriteLine("It's a built-in type!");
    }
    else
    {
        Console.WriteLine("It's a custom class!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
I have this method to transfer files using a FTP Server: private void TransferNeededFiles(IEnumerable<string>
the ToSelectList method I have: public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty,
I have a method that starts like this: public static UnboundTag ResolveTag(Type bindingType, string
Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0;
I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the
I have this method: public virtual IEnumerable<Invoice> GetHomePageInvoices(IList<Area> areas, FinancialYearLookup financialYear) { var homePageInvoices
I have a class, and I declare it like this: public class WhereOrCondition<T> :
I have a class Exporter which has a generic method which accepts an IEnumerable<T>
I have this delegate declaration: public delegate IEnumerable<T> SearchInputTextStrategy<T, U>(string param); Lets assume 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.