Why does compiler throw CS0563 error when I overload == in C# ?-- resolved

 Why does compiler throw CS0563 error when I overload  == in C# ?-- resolved


Let's take a look at my code?

Before solved:
using System;

namespace Operator_Overloaded_Comparsion
{
    class Program
    {
        public class Base
        {
            public int num1;
            public int num2;
            public Base(int n1,int n2)
            {
                num1 = n1;
                num2 = n2;
            }
            
        }

        static void Main(string[] args)
        {
            Base b1 = new Base(2,3);
            Base b2 = new Base(2,3);
            Base b3 = new Base(2,8);
            Console.WriteLine(b1 == b2);
            Console.WriteLine(b1 != b2);
            Console.WriteLine(b2 == b3);
            Console.WriteLine(b2 != b3);
            Console.WriteLine(b1 == b3);
            Console.WriteLine(b1 != b3);
        }
        public static bool operator ==(Base b1, Base b2)
        {
            return ((b1.num1 == b2.num1) && (b1.num2 == b2.num2));
        }
        public static bool operator !=(Base b1, Base b2)
        {
            return (!(b1 == b2));
        }
        public static bool Equals(Base b1, Base b2)
        {
            return ((b1.num1 == b2.num1) && (b1.num2 == b2.num2));
        }
    }
}


After solved:
using System;

namespace Operator_Overloaded_Comparsion
{
    class Program
    {
        public class Base
        {
            public int num1;
            public int num2;
            public Base(int n1,int n2)
            {
                num1 = n1;
                num2 = n2;
            }
            public static bool operator ==(Base b1, Base b2)
            {
                return ((b1.num1 == b2.num1) && (b1.num2 == b2.num2));
            }
            public static bool operator !=(Base b1, Base b2)
            {
                return (!(b1 == b2));
            }
            public static bool Equals(Base b1, Base b2)
            {
                return ((b1.num1 == b2.num1) && (b1.num2 == b2.num2));
            }
        }

        static void Main(string[] args)
        {
            Base b1 = new Base(2,3);
            Base b2 = new Base(2,3);
            Base b3 = new Base(2,8);
            Console.WriteLine(b1 == b2);
            Console.WriteLine(b1 != b2);
            Console.WriteLine(b2 == b3);
            Console.WriteLine(b2 != b3);
            Console.WriteLine(b1 == b3);
            Console.WriteLine(b1 != b3);
        }
    }
}

[Answer]

At First, I defined == operator at outside instead of Base class.
Then compiler throws CS06563 error.
After I saw this article
I realized that how to fix it.

[details]

For more details about precautions of implementing the equals method, I recommend that you see this article below. 

I recommend that you understand how to implement equal method and operator overloading.

For more details about CS0563 error , see:

[source]

Comments

Popular posts from this blog

String Interpolation(字串內插)

Format a string -- String.Format()