简体   繁体   English

C#中的运算符重载

[英]Operator overloading in C#

        [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) 或如何使运算符!=或==检查空值并返回正确的值(真或假)

如果可以以.NET Framework 4.0为目标,则System.Numerics.Complex看起来像您所需要的。

Inside the == and != overloaded operators (at the very top) put the following: ==!=重载运算符(位于最顶部)中,放置以下内容:

if (src1 == null || src2 == null)
    return false;

By the way, I find your complex number implementation is kind of poor. 顺便说一下,我发现您的复数实现有点差。 You could use some ready example or at least read it through. 您可以使用一些现成的示例,或者至少通读它。

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() . 并且不要忘记,如果您重写Equals() ,那么您还必须重写GetHashCode() A good pattern for doing can also be found on SO . 在SO上也可以找到一个很好的执行模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM