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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:53:57+00:00 2026-05-13T00:53:57+00:00

Using C# and .NET 3.5, what’s the best way to handle this situation. I

  • 0

Using C# and .NET 3.5, what’s the best way to handle this situation. I have hundreds of fields to compare from various sources (mostly strings). Sometimes the source returns the string field as null and sometimes as empty. And of course, sometimes there is text in the fields. My current comparison of strA != strB isn’t cutting it because strA is null and strB is “”, for example. I know I could do the string.IsNullOrEmpty which results in a double comparison and some ugliness. Is there a better way to handle this? I thought extension methods, but you can’t extend operators.

I guess I’m looking for a sexy way to do this.

  • 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-13T00:53:57+00:00Added an answer on May 13, 2026 at 12:53 am

    Since you’ve got hundreds of comparisons to do, it sounds like you want a single function to call so that you can reduce the clutter and repetition in your code. I don’t think there is a built-in function to do a null/empty string/comparison check all in one, but you could just make one yourself:

    static class Comparison
    {
        public static bool AreEqual(string a, string b)
        {
            if (string.IsNullOrEmpty(a))
            {
                return string.IsNullOrEmpty(b);
            }
            else
            {
                return string.Equals(a, b);
            }
        }
    }
    

    Then you could just use a single call to your function for each comparison:

            if(Comparison.AreEqual(strA[0], strB[0])) { // ... }
            if(Comparison.AreEqual(strA[1], strB[1])) { // ... }
            if(Comparison.AreEqual(strA[2], strB[2])) { // ... }
            if(Comparison.AreEqual(strA[3], strB[3])) { // ... }
    

    This approach is also easier to expand if you later find that you need to worry about additional situations, such as ignoring whitespace at the beginning or ending of strings; you can then just add more logic to your function to do some trimming or whatever and you won’t have to make any modifications to the hundreds of lines of code calling your function.

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

Sidebar

Related Questions

Using .NET's Office interop libraries, does anybody know the best way to convert back
Using .NET 4, what is the best way to save an app's window size
Using .NET 3.5, is there a way to retrieve a value from Hashtable corresponding
I'm using net.sf.json.xml.XMLSerializer And I have this error 25 déc. 2012 18:20:38 net.sf.json.xml.XMLSerializer getType
Using .net XsltCompiled transforms, whats the best way to perform date diffs? Is defining
(using .Net 3.5) I have a routine that imports settings from an external datasource,
I'm using .NET 4.5/VS2012, and I have a ListView looks something like this <ListView
Using .Net and Oracle 11g - I've been returning dataTables from a procedure inside
using .NET 3.5. I have a function I want to make multithreaded calls to.
Using .Net how can I check which users in our domain have resources (files,

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.