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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:19:03+00:00 2026-05-13T09:19:03+00:00

I look for an equality between two instances of this struct. public struct Serie<T>

  • 0

I look for an equality between two instances of this struct.

public struct Serie<T>
{
    T[]         X; 
    double[]    Y;

    public Serie(T[] x, double[] y)
    {
        X = x;
        Y = y;
    }

    public override bool Equals(object obj)
    {
        return obj is Serie<T> && this == (Serie<T>)obj;
    } 
    public static bool operator ==(Serie<T> s1, Serie<T> s2)
    {
        return s1.X == s2.X && s1.Y == s2.Y;
    }
    public static bool operator !=(Serie<T> s1, Serie<T> s2)
    {
        return !(s1 == s2);
    }

This doesn’t work. What am I missing?

        double[] xa = { 2, 3 };
        double[] ya = { 1, 2 };
        double[] xb = { 2, 3 };
        double[] yb = { 1, 2 };
        Serie<double> A = new Serie<double>(xa, ya);
        Serie<double> B = new Serie<double>(xb, yb);
        Assert.AreEqual(A, B);
  • 1 1 Answer
  • 1 View
  • 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-13T09:19:04+00:00Added an answer on May 13, 2026 at 9:19 am

    You’re comparing the array references rather than their contents. ya and yb refer to different arrays. If you want to check the contents of the arrays, you’ll have to do so explicitly.

    I don’t think there’s anything built into the framework to do that for you, I’m afraid. Something like this should work though:

    public static bool ArraysEqual<T>(T[] first, T[] second)
    {
        if (object.ReferenceEquals(first, second))
        {
            return true;
        }
        if (first == null || second == null)
        {
            return false;
        }
        if (first.Length != second.Length)
        {
            return false;
        }
        IEqualityComparer comparer = EqualityComparer<T>.Default;
        for (int i = 0; i < first.Length; i++)
        {
            if (!comparer.Equals(first[i], second[i]))
            {
                return false;
            }
        }
        return true;
    }
    

    As an aside, your structs are sort of mutable in that the array contents can be changed after the struct is created. Do you really need this to be a struct?

    EDIT: As Nick mentioned in the comments, you should really override GetHashCode as well. Again, you’ll need to get the contents from the arrays (and again, this will cause problems if the arrays get changed afterwards). Similar utility method:

    public static int GetHashCode<T>(T[] array)
    {
        if (array == null)
        {
            return 0;
        }
        IEqualityComparer comparer = EqualityComparer<T>.Default;
        int hash = 17;
        foreach (T item in array)
        {
            hash = hash * 31 + comparer.GetHashCode(item);
        }
        return hash;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Look ath this method of ThreadPoolExcecutor: public void execute(Runnable command) { ... if (runState
Look at this code segment: if(ip_header->protocol == IPPROTO_TCP) { tcp_header = (struct tcphdr*)(packet +
Look at this example class Player { ChessEngine* ce; public: makeMove() { ce.addMove(); }
look at this fiddle: http://jsfiddle.net/ugxNK/ I want that the first list element is in
Look at the code: template <class x> struct Foo { int getX(x *p) {
Look at this: $('form).submit(function (event) { $(':input.required').trigger('blur'); var num = $('.warning', this).length; alert(num);//Focus here!
look at this code, <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <script> function change() {
This is a follow-up to Testing for floating-point value equality: Is there a standard
look at this image folks, instead of using overflow:hidden there is still some text
look the UML image. I've got a generic class called BinaryProblem, this one receives

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.