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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:20:42+00:00 2026-05-17T01:20:42+00:00

I am fairly new to C# coming from Java, and I’m wondering if there’s

  • 0

I am fairly new to C# coming from Java, and I’m wondering if there’s a simple way to avoid code repetition involving primitive types like this:


private Boolean AtLeastOneBufferItemIsNonZero(int[] Buffer)
{
    Boolean result = false;
    foreach (int Item in Buffer)
    {
        result = !(Item == (int)0);
        if (result) break;
    }
    return result;
}

private Boolean AtLeastOneBufferItemIsNonZero(float[] Buffer)
{
    Boolean result = false;
    foreach (float Item in Buffer)
    {
       result = !(Item == (float)0);
       if (result) break;
    }
    return result;
}

I can’t find a “Number” supertype so that I can compare “Item” in a generics implementation (I wouldn’t mind the performance penalty of boxing, although I understand that in .NET there is no such thing?):


//SOMETHING LIKE THIS?
private Boolean AtLeastOneBufferItemIsNonZero<T>(T[] Buffer) where T : NUMBERTYPE
{
    Boolean result = false;
    foreach (T Item in Buffer)
    {
       result = !(Item.Equals(0)); //Nope....
       if (result) break;
    }
    return result;
}

Is the only way to create my own Number implementation and having a compare() method? That sounds like overkill doesn’t it?

  • 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-17T01:20:43+00:00Added an answer on May 17, 2026 at 1:20 am

    LINQ makes this pretty simple to do, by relying on the fact that the default value of any numeric type is zero, and they have appropriate equality methods:

    private bool AtLeastOneBufferItemIsNonZero<T>(T[] items)
    {
        T zero = default(T);
        EqualityComparer<T> comparer = EqualityComparer<T>.Default;
        return items.Any(t => !comparer.Equals(t, zero));
    }
    

    Now that doesn’t restrict it to numeric types, but it does avoid repetition. You can go further, by generalizing it to IEnumerable<T> and making it an extension method:

    public static class Extensions
    {
        public static bool ContainsNonDefaultValue<T>(this IEnumerable<T> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            T zero = default(T);
            EqualityComparer<T> comparer = EqualityComparer<T>.Default;
            return items.Any(t => !comparer.Equals(t, zero));
        }
    }
    

    You could restrict this to value types by changing the constraint to

    where T : struct
    

    but that would be a bit pointless IMO. With the change to use EqualityComparer<T>.Default, you can also use the method to check whether any value in a reference type sequence is non-null.

    EDIT: As a side note, another way of look at it is to reverse the condition:

    return !items.All(t => comparer.Equals(t, zero));
    

    It depends whether you’re happier with the concept of “any of them is non-zero” or “they’re not all zero” 🙂

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

Sidebar

Related Questions

I'm fairly new to the STL, so I was wondering whether there are any
Okay: I'm fairly new to C++ and static languages on a whole. Coming from
I'm fairly new to Visual Studio and am wondering how best to plan for
I am fairly new to StructureMap, but my understanding is that there are two
Being fairly new to JavaScript, I'm unable to discern when to use each of
I'm still fairly new to T-SQL and SQL 2005. I need to import a
I'm fairly new to ASP.NET and trying to learn how things are done. I
I'm fairly new to the world of versioning but would like to introduce Subversion
I am fairly new to Emacs and I have been trying to figure out
I'm fairly new at programming, but I've wondered how shell text editors such as

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.