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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:02:37+00:00 2026-06-04T09:02:37+00:00

[Serializable] public class ComplexArray { #region Attributes /// <summary> /// Array Size /// </summary>

  • 0
        [Serializable]
        public class ComplexArray
        {
            #region Attributes

            /// <summary>
            /// Array Size
            /// </summary>
            protected int m_iSize;

            /// <summary>
            /// Real part of the data
            /// </summary>
            protected double[] m_dReal;

            /// <summary>
            /// Imaginary part of the data
            /// </summary>
            protected double[] m_dImag;

            #region Construction

            /// <summary>
            /// Default constructor
            /// </summary>
            public ComplexArray()
            {
            }


            public override bool Equals(object o)
            {
                if (this == (ComplexArray)o)
                    return true;
                else
                    return false;
            }



            public static bool operator ==(ComplexArray src1, ComplexArray src2)
            {
                if (src1.m_iSize != src2.m_iSize)
                    return false;

                for (int ii = 0; ii < src1.m_iSize; ii++)
                {
                    if (src1.Real[ii] != src2.Real[ii])
                        return false;
                    if (src1.Imag[ii] != src2.Imag[ii])
                        return false;
                }

                return true;
            }

            public static bool operator !=(ComplexArray src1, ComplexArray src2)
            {

                if (src1 == src2)
                    return false;
                else
                    return true;
            }

    }

I have a created a class called complex array and intention of this class is to save real and imaginary numbers and different operators have been overloaded like +,*,!=,==

Assume some function returns the instance of this class.

ComplexArray array = GetValue();

I want to check whether the reference is valid or not…

    if(array != null)
    {
      //proceed further....
    }

Issue : When the value is checked against null value, the exception occurs, because internally != overloaded function calls the ==.

How to avoid this kind of situation in operator overloading? Or how to make the operator != or == to check for the null value and return the correct values(true or false)

  • 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-04T09:02:38+00:00Added an answer on June 4, 2026 at 9:02 am

    This can be done by this code:

    public override bool Equals(object obj)
    {
        var other = obj as ComplexArray;
    
        if(ReferenceEquals(other, null))
        {
            return false;
        }
    
        if(ReferenceEquals(this, other)
        {
            return true;
        }
    
        // ToDo: Do all other comparision beyond reference comparision.
    }
    

    Also within the == operator you have to check both sides:

    public static bool operator ==(ComplexArray src1, ComplexArray src2)
    {
        if(ReferenceEquals(src1, null)
           || ReferenceEquals(src2, null))
        {
            return ReferenceEquals(src1, src2);
        }
    
        return src1.Equals(src2);
    }
    
    public static bool operator !=(ComplexArray src1, ComplexArray src2)
    {
        return !(src1 == src2);
    }
    

    And don’t forget, if you override Equals() you have also to override GetHashCode(). A good pattern for doing can also be found on SO.

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

Sidebar

Related Questions

I have a webservice [Serializable] public class DataObject { public int ID { get;
public class StoreMessage extends Thread implements Serializable{ private static long start_nanotime=System.nanoTime(); private static int
I have this class public class wordObject implements java.io.Serializable { String wordName; int occCount;
I have an Abstract class: [Serializable] public abstract class BaseModel { public virtual int
Here is my object [Serializable()] public class PersistentObject { public virtual int ID {
I have a code like this: [Serializable] public class A { public int X
I have a class like this: [Serializable] public class Structure { #region Constants and
Let's suppose I have this object: [Serializable] public class MyClass { public int Age
I have the following object: [Serializable] public class ExampleImage { public int ID {
Have the following structure [Serializable] public class Parent { public int x = 5;

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.