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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:21:21+00:00 2026-05-30T18:21:21+00:00

I currently have a user defined type class MyType { double x; double y;

  • 0

I currently have a user defined type

class MyType {
    double x;
    double y;
    double z;

    //This class has operator overloading implemented
    public static MyType operator + (double a,MyType b){
        ...
    }
}

At some point later I will have an array (object[]) some of them double and some MyType. I would like to do evaluation over them but compiler is not allowing operator ‘+’ to be applied to type object and object. What should I do here?

  • 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-30T18:21:22+00:00Added an answer on May 30, 2026 at 6:21 pm

    When resolving a + in code the compiler needs to bind it to a specific operation. The type object has no + operation and the compiler has no idea that the underlying types are double and MyType and hence the compiler errors out.

    In order to fix this you will need to either

    • statically convert the elements to double and MyType so the compiler can properly bind the + operator
    • use dynamic and rely on the runtime binding to the correct operator+.

    Here is an example of the latter method.

    class MyType {
        double _value;
    
        internal MyType(double value) {
            _value = value;
        }
    
        public static MyType operator +(double left, MyType right) {
            return new MyType(left + right._value);
        }
    
        public static MyType operator +(MyType left, double right) {
            return new MyType(left._value + right);
        }
    
        public static MyType operator +(MyType left, MyType right) {
            return new MyType(left._value + right._value);
        }
    
        public static implicit operator MyType(double other) {
            return new MyType(other);
        }
    }
    
    class Program
    {
        static void Main(string[] args) {
            object[] them = new object[] {
                42,
                new MyType(13),
                12,
                new MyType(12)
            };
    
            dynamic all = them;
            MyType sum = 0;
            foreach (dynamic element in all) {
                sum += element;
            }
    
            Console.WriteLine(sum);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined an object that has several attribute.. class thing(object): def __init__(self, type,
I currently have form that checks if a user has unsubmitted changes when they
This has been driving me nuts for a week now. I have a class
Right now, I have a class defined as such: public class Task { public
I have created a user defined type to contain some data that I will
I currently have a user control that is used on both an /Address/Edit and
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I currently have a wysiwyg iframe where the user can submit input to another
I currently have built a system that checks user IP, browser, and a random-string
I currently have code in my ApplicationController to check if a user is logged

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.