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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:56:59+00:00 2026-06-13T11:56:59+00:00

I have defined a class (call it class A) in C# and overloaded some

  • 0

I have defined a class (call it class A) in C# and overloaded some of the arithmetic operators for it. Let’s focus on the addition operator (though I expect the other operators behave similarly). I’ve set up class A so that I can add a scalar to it (i.e. a single Double value), I can add it to a scalar or I can add together two instances of A together.

So in C# I have the “+” operator overloaded with three different signatures. Explicitly the signatures are:

  • A + Double
  • Double + A
  • A + A

When I work with the class in PowerShell it only recognizes one of the three overloads. Thus in PowerShell I can execute A + Double but the other two expressions cause an error.

When I use the Get-Member Cmdlet on an instance of A it looks like it “sees” all three overloads but is only honoring the first overload in the list.

I suspect this is by design. I love PowerShell but it is very different from C# and I expect it only allows one signature for an overloaded operator (based on what I’ve learned about it so far) and that the left hand member of the expression determines the type of addition to perform. Can anyone confirm?

My work around is to code some more verbose static methods (like “AddScalar” and “AddA” methods) in C# for the class. In PowerShell I would call these named methods rather than using the “+” operator. This should work fine but it would be nice to have PowerShell use the appropriate definition for “+” based on what’s being added.

Has anyone had any success using overloaded class operators with different signatures or would this kind of behavior break a fundamental PowerShell design goal?

UPDATE: Dugas nailed it. But I did learn something else along the way and wanted to post it here.

When I defined addition for class A I defined it so that adding two instances of class A would either produce a new instance of class A or produce a NULL. (This was poor design on my part since addition really should be closed. In other words, ideally, addition should always produce something that could be acted on by another addition operator.) In C# this was not an issue since I was never adding more than two objects at a time and checking for NULL when I examined the results.

Instead of returning NULL PowerShell will throw an “Object reference not set …” error for the op_Addition operator if the result of the operation is NULL. I misinterpreted this error by assuming the NULL reference was being created when PowerShell tried to use the wrong overloaded operator and was having trouble performing the type conversion.

I reran the A + A case for objects that, when summed, return another object instance of class A and everything worked great.

To summarize, if a1, a2 and a3 are all instances of class A and d is a Double and if a1 + a2 = NULL and a1 + a3 = aX (where aX is a new instance of class A) then …

  • d + a1 # Fails because PowerShell doesn’t see an operator for Double that takes Class A and Class A cannot be converted to a Double.
  • a1 + d # Works as expected
  • a1 + a2 # Fails. Throws an “Object reference not set …” error because the result is NULL.
  • a1 + a3 # Works as expected.

Thanks for steering me in the right direction!

  • 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-13T11:56:59+00:00Added an answer on June 13, 2026 at 11:56 am

    I tested this out of curiosity. I created a class like so:

    namespace ClassLibrary1
    {
     public class Class1
     {
      public static string operator +(Class1 class1, double d)
      {
       return "a";
      }
    
      public static string operator +(double d, Class1 class1)
      {
       return "b";
      }
    
      public static string operator +(Class1 class1, Class1 class12)
      {
       return "c";
      }
     }
    }
    

    I was able to call both overloads that had Class1 as the first parameter, but could not call the overload that had double as the first parameter. I don’t know why you couldn’t call your overload of A + A, maybe it was for a different reason other than dispatching the correct operator overload?

    $a = New-Object ClassLibrary1.Class1
    ($a + 0) -eq 'a' # Was True
    ($a + $a) -eq 'c' # Was True
    

    Calling the overload:

    ([double]0 + $a) -eq 'b'
    

    Gave error:

    Cannot convert the “ClassLibrary1.Class1” value of type “ClassLibrary1.Class1” to type “System.Double”.

    So I am assuming you can call different operator overloads, as long as the first argument is the type that the operator is overloaded on?

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

Sidebar

Related Questions

I have a class call Monkey, defined in root/ook/monkey.py . Another script, let's call
Let's say I have defined the following class: public abstract class Event { public
I have defined a class FieldProperty with some of properties corresponding to field defined
At work we have a base class, let's call it IntrusiveBase, that acts something
I have defined new member in my class protected COMObject.Call call_ = null; This
I have defined new member in my class that is COMObject protected COMObject.Call call_
First part: call F# from F# Let's say we have the following type defined
I have defined a class A and derived a new class B from A
I have defined a class Listener and created a dictionary of Listener objects. Each
I have defined the following class using COM to use the IGroupPolicyObject using C#.NET:

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.