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

The Archive Base Latest Questions

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

I’m working with some code that returns a code to indicate the type of

  • 0

I’m working with some code that returns a code to indicate the type of user that they are (e.g. “A”, “B”, “C”, “D”, etc.). Each code corresponds to a specific role and/or scope (e.g. across the entire application or just for the object being worked on).

In some code that I’m looking at, I see calls to check if the user’s code is one of several in order to allow them to perform some action. So I see calls like:

//"B" would come from the database
string userCode = "B";

//some more work...

//if the user's code is either A or C...
if("AC".IndexOf(userCode) >= 0) {
  //do work that allows the user to progress
} else {
  //notify user they can't do this operation
}

Is this an efficient way of performing this check? Are there more efficient ways?

Thanks in advance!

  • 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-25T01:14:38+00:00Added an answer on May 25, 2026 at 1:14 am

    Looking at the de-compiled code for Contains(), it just calls IndexOf() with StringComparison.Ordinal, so I’d say IndexOf() is most efficient (by a very small hair) i if used in the same way (Ordinal) since it has one less method call, but Contains() is more readable and therefore more maintainable…

    public bool Contains(string value)
    {
        return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
    }
    

    As in all things, I’d go with what’s more readable and maintainable then splitting hairs on performance. Only do micro-optimization when you know there’s a bottleneck at this point.

    UPDATE: Over 1,000,000 iterations:

    • Contains(value) – took 130ms
    • IndexOf(value, StringComparison.Ordinal) – took 128 ms

    So as you can see, very, very NEAR same. Once again, go with what’s more maintainable.

    UPDATE 2: If your code is always a single char (not a 1-char string), IndexOf() is faster:

    • Contains(char value) – took 94 ms
    • IndexOf(char value) – took 16 ms

    If you know your char codes are always a single char, it is about an order of magnitude faster to use IndexOf() with a char argument.

    This is because Contains(char value) is an extension method off of IEnumerable<T> and not a first class method of string.

    But once again ~100 ms over 1,000,000 iterations is really, truly, quite negligible.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I need to clean up various Word 'smart' characters in user input, including but
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.