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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:37:34+00:00 2026-05-25T21:37:34+00:00

Possible Duplicate: Is there a better alternative than this to 'switch on type'? I

  • 0

Possible Duplicate:
Is there a better alternative than this to 'switch on type'?

I need to iterate through all properties of my class and to check if its type of int the i need to do something, if its string .. then do something. I need it using switch-case. Here i am using switch in the following manner, but it asks for some constant. see the code below:

 public static bool ValidateProperties(object o)
{
    if(o !=null)
    {
        var sourceType = o.GetType();
        var properties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Static);
        foreach (var property in properties)
        {
            var type = property.GetType();
            switch (type)
            {
                *case typeof(int):* getting error here
                    // d
            }
        }
    }
}

Also i want to know , what check should I use, typeof(int) or typeof(Int32)?

  • 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-25T21:37:35+00:00Added an answer on May 25, 2026 at 9:37 pm

    You cannot use a switch block to test values of type Type. Compiling your code should give you an error saying something like:

    A switch expression or case label must be a bool, char, string,
    integral, enum, or corresponding nullable type

    You’ll need to use if–else statements instead.

    Also: typeof(int) and typeof(Int32) are equivalent. int is a keyword and Int32 is the type name.

    UPDATE

    If you expect that most types will be intrinsic you may improve performance by using a switch block with Type.GetTypeCode(...).

    For example:

    switch (Type.GetTypeCode(type))
    {
        case TypeCode.Int32:
            // It's an int
            break;
    
        case TypeCode.String:
            // It's a string
            break;
    
        // Other type code cases here...
    
        default:
            // Fallback to using if-else statements...
            if (type == typeof(MyCoolType))
            {
                // ...
            }
            else if (type == typeof(MyOtherType))
            {
                // ...
            } // etc...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# - Is there a better alternative than this to 'switch on
Possible Duplicate: C# - Is there a better alternative than this to ‘switch on
Possible Duplicate: Is there a better way than parsing /proc/self/maps to figure out memory
Possible Duplicate: How to select an element by class inside “this” in Jquery I'm
Possible Duplicate: how to provide a swap function for my class? There are some
Possible Duplicate: Ruby: Any gems for threadpooling? Is there a better ruby lib thread
Possible Duplicate: Getting the “diff” between two arrays in C#? Is there a better
Possible Duplicate: Is there an “exists” function for jQuery Is there a better way
Possible Duplicate: Is there a tool that can display a SVN repository visually (
Possible Duplicate: Is there a php library for email address validation? Hi there, 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.