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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:36:44+00:00 2026-06-13T23:36:44+00:00

To check whether a string is empty I use var test = string.Empty; if

  • 0

To check whether a string is empty I use

var test = string.Empty; 
if (test.Length == 0) Console.WriteLine("String is empty!");
if (!(test.Any())) Console.WriteLine("String is empty!");
if (test.Count() == 0) Console.WriteLine("String is empty!");
if (String.IsNullOrWhiteSpace(test)) Console.WriteLine("String is empty!");
  1. All the above statements produces the same output. What is the optimal method that I should use?

    var s = Convert.ToString(test);    
    s = test.ToString(CultureInfo.InvariantCulture);
    
  2. Again, both statements does the same thing. What is the best method to use?

I tried debugging and how to benchmark the performance of a C# statement?

  • 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-13T23:36:45+00:00Added an answer on June 13, 2026 at 11:36 pm

    First of all the 4 statemens are not giving the same output on all inputs. Try null and the first 3 will throw an exception. And try whithspaces the last one will give you a failty result. So you really have to think about what you want. The best way to go are normally the:

    string.IsNullOrEmpty
    string.IsNullOrWhiteSpace
    

    Only if you are doing this a few million times you should have a look on how to optimize your code further.

    Here some test result, but this can differ on any .net version:

    Test results for 100 million iterations:

    Equality operator ==:   796 ms 
    string.Equals:          811 ms 
    string.IsNullOrEmpty:   312 ms 
    Length:                 140 ms  [fastest]
    Instance Equals:       1077 ms 
    

    source

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

Sidebar

Related Questions

I need to check whether a string contains any swear words. Following some advice
String urlString=http://myApp:8080/new/bin/save/SellerMyPage/WebHome i want to check whether above string contains the string MyPage between
I want to check whether string is empty or not when i create object=Shared.getLocal(abc);
I want to check whether or not a variable string data contain empty string.
Just a short question out of curiosity. string str = string; Console.WriteLine(str.EndsWith(string.Empty)); //true Console.WriteLine(str.LastIndexOf(string.Empty)
How can I check whether a C# variable is an empty string "" or
How can I check whether a string is not null and not empty? public
I want to check whether a string is a file name (name DOT ext)
I need to know whether check whether a String end with something like .xyz
I need an simple way to check whether a string that is sent to

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.