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

  • Home
  • SEARCH
  • 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 7512245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:48:17+00:00 2026-05-29T23:48:17+00:00

I have the following code: class Program { static void Main(string[] args) { Test

  • 0

I have the following code:

class Program
{
    static void Main(string[] args)
    {
        Test test = new Test();
        object objTest = test;

        Console.WriteLine(test.GetType());    // Output: "OperatorOverload.Test"
        Console.WriteLine(objTest.GetType()); // Output: "OperatorOverload.Test"

        Console.WriteLine(test == null);    // Output: "True"
        Console.WriteLine(objTest == null); // Output: "False"

        test.Equals(null);    // Output: "Hi!"
        objTest.Equals(null); // Output: "Hi!"

        Console.ReadLine();
    }
}

Test looks like this:

class Test
{

    public static bool operator ==(Test obj1, Test obj2)
    {
        return true;
    }

    public static bool operator !=(Test obj1, Test obj2)
    {
        return !(obj1 == obj2);
    }

    public override bool Equals(object obj)
    {
        Console.WriteLine("Hi!");
        return true;
    }

}

It appears that operator overloading only works when the type of the variable you’re dealing with is the class the operator overload is defined in. I can see why that would be the case, as I’m looking for a safe way of checking whether an object equals null.

My questions are:

Do overloaded operators only work if the type of the variable is the class the operator is defined in (my code tells me yes, but I could be making a mistake)?

Is the following code a safe way of checking whether an object equals null?

SomeClass obj = new SomeClass(); // may have overloaded operators
if ((object)obj == null)
{
    // ...
}
  • 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-29T23:48:18+00:00Added an answer on May 29, 2026 at 11:48 pm

    In C# overriding overloading an operator essentially creates a static method with its name like op_xxx(e.g. op_Add, op_Equality). In your question, test == null behaviors like Test.op_Equality(test, null) which returns true obviously. While objTest == null calls Object.op_Equality(test, null) that returns false. On the other hand, you override the virtual method Equals, so test.Equals and objTest.Equals have the same result.

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

Sidebar

Related Questions

I have the following code: class Program { static void Main(string[] args) { new
Hi I have following test code: class Program { static void Main(string[] args) {
I have the following code: class Program { static void Main(string[] args) { Console.WriteLine(SqrtRoot(0));
I have the following code: class Program { static void Main(string[] args) { using
I have the following code class Program { static void Main(string[] args) { List<A>
I have the following code Snippet. class Program { static void Main(string[] args) {
I have the following code: class Program { static void Main(string[] args) { string
Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
I have following code public class TEST { public static void main(String arg[]){ try
Consider the following code: class Program { static void Main(string[] args) { try {

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.