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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:21:17+00:00 2026-05-11T16:21:17+00:00

Can I achieve if (a == b || c) instead of if (a ==

  • 0

Can I achieve

if (a == "b" || "c")

instead of

if (a == "b" || a== "c")

?

  • 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-11T16:21:17+00:00Added an answer on May 11, 2026 at 4:21 pm

    No, you can do:

    if (new[] { "b", "c" }.Contains(a))
    

    if you have the LINQ extensions available, but that’s hardly an improvement.


    In response to the comment about performance, here’s some basic timing code. Note that the code must be viewed with a critical eye, I might have done things here that skew the timings.

    The results first:

    ||, not found: 26 ms
    ||, found: 8 ms
    array.Contains, not found: 1407 ms
    array.Contains, found: 1388 ms
    array.Contains, inline array, not found: 1456 ms
    array.Contains, inline array, found: 1427 ms
    switch-statement, not interned, not found: 26 ms
    switch-statement, not interned, found: 14 ms
    switch-statement, interned, not found: 25 ms
    switch-statement, interned, found: 8 ms
    

    All the code was executed twice, and only pass nr. 2 was reported, to remove JITting overhead from the equation. Both passes executed each type of check one million times, and executed it both where the element to find was one of the elements to find it in (that is, the if-statement would execute its block), and once where the element was not (the block would not execute). The timings of each is reported. I tested both a pre-built array and one that is built every time, this part I’m unsure how much the compiler deduces and optimizes away, there might be a flaw here.

    In any case, it appears that using a switch-statement, with or without interning the string first, gives roughly the same results as the simple or-statement, which is to be expected, whereas the array-lookup is much more costly, which to me was also expected.

    Please tinker with the code, and correct (or comment) it if there’s problems.

    And here’s the source code, rather long:

    using System;
    using System.Linq;
    using System.Diagnostics;
    namespace StackOverflow826081
    {
        class Program
        {
            private const Int32 ITERATIONS = 1000000;
            static void Main()
            {
                String a;
                String[] ops = CreateArray();
                Int32 count;
                Stopwatch sw = new Stopwatch();
                Int32 pass = 0;
                Action<String, Int32> report = delegate(String title, Int32 i)
                {
                    if (pass == 2)
                        Console.Out.WriteLine(title + ": " + sw.ElapsedMilliseconds + " ms");
                };
    
                for (pass = 1; pass <= 2; pass++)
                {
                    #region || operator
    
                    a = "a";
                    sw.Start();
    
                    count = 0;
                    for (Int32 index = 0; index < ITERATIONS; index++)
                    {
                        if (a == "b" || a == "c")
                        {
                            count++;
                        }
                    }
                    sw.Stop();
                    report("||, not found", count);
                    sw.Reset();
    
                    a = "b";
                    sw.Start();
    
                    count = 0;
                    for (Int32 index = 0; index < ITERATIONS; index++)
                    {
                        if (a == "b" || a == "c")
                        {
                            count++;
                        }
                    }
                    sw.Stop();
                    report("||, found", count);
                    sw.Reset();
    
                    #endregion
    
                    #region array.Contains
    
                    a = "a";
                    sw.Start();
    
                    count = 0;
                    for (Int32 index = 0; index < ITERATIONS; index++)
                    {
                        if (ops.Contains(a))
                        {
                            count++;
                        }
                    }
                    sw.Stop();
                    report("array.Contains, not found", count);
                    sw.Reset();
    
                    a = "b";
                    sw.Start();
    
                    count = 0;
                    for (Int32 index = 0; index < ITERATIONS; index++)
                    {
                        if (ops.Contains(a))
                        {
                            count++;
                        }
                    }
                    sw.Stop();
                    report("array.Contains, found", count);
                    sw.Reset();
    
                    #endregion           
    
                    #region array.Contains
    
                    a = "a";
                    sw.Start();
    
                    count = 0;
                    for (Int32 index = 0; index < ITERATIONS; index++)
                    {
                        if (CreateArray().Contains(a))
                        {
                            count++;
                        }
                    }
                    sw.Stop();
                    report("array.Contains, inline array, not found", count);
                    sw.Reset();
    
                    a = "b";
                    sw.Start();
    
                    count = 0;
                    for (Int32 index = 0; index < ITERATIONS; index++)
                    {
                        if (CreateArray().Contains(a))
                        {
                            count++;
                        }
                    }
                    sw.Stop();
                    report("array.Contains, inline array, found", count);
                    sw.Reset();
    
                    #endregion
    
                    #region switch-statement
    
                    a = GetString().Substring(0, 1); // avoid interned string
                    sw.Start();
    
                    count = 0;
                    for (Int32 index = 0; index < ITERATIONS; index++)
                    {
                        switch (a)
                        {
                            case "b":
                            case "c":
                                count++;
                                break;
                        }
                    }
                    sw.Stop();
                    report("switch-statement, not interned, not found", count);
                    sw.Reset();
    
                    a = GetString().Substring(1, 1); // avoid interned string
                    sw.Start();
    
                    count = 0;
                    for (Int32 index = 0; index < ITERATIONS; index++)
                    {
                        switch (a)
                        {
                            case "b":
                            case "c":
                                count++;
                                break;
                        }
                    }
                    sw.Stop();
                    report("switch-statement, not interned, found", count);
                    sw.Reset();
    
                    #endregion                      
    
                    #region switch-statement
    
                    a = "a";
                    sw.Start();
    
                    count = 0;
                    for (Int32 index = 0; index < ITERATIONS; index++)
                    {
                        switch (a)
                        {
                            case "b":
                            case "c":
                                count++;
                                break;
                        }
                    }
                    sw.Stop();
                    report("switch-statement, interned, not found", count);
                    sw.Reset();
    
                    a = "b";
                    sw.Start();
    
                    count = 0;
                    for (Int32 index = 0; index < ITERATIONS; index++)
                    {
                        switch (a)
                        {
                            case "b":
                            case "c":
                                count++;
                                break;
                        }
                    }
                    sw.Stop();
                    report("switch-statement, interned, found", count);
                    sw.Reset();
    
                    #endregion
                }
            }
    
            private static String GetString()
            {
                return "ab";
            }
    
            private static String[] CreateArray()
            {
                return new String[] { "b", "c" };
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 232k
  • Answers 232k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you care about your data, you will put the… May 13, 2026 at 5:30 am
  • Editorial Team
    Editorial Team added an answer Have a look at this question... cheers, EDIT: Joe -… May 13, 2026 at 5:30 am
  • Editorial Team
    Editorial Team added an answer https://www.codewrecks.com/post/old/2008/07/detecting-if-finally-block-is-executing-for-an-manhandled-exception/ describes a "hack" to detect if your code is… May 13, 2026 at 5:30 am

Related Questions

I'm looking for insight into the heads of HashSet designers. As far as I
I have an UITableView, and in it I have different sections - words that
I'm looking for tips on the best way to reconfigure the Log4Net logging level
I am trying to implement infer_class function that, given a method, figures out the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.