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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:25:15+00:00 2026-05-23T06:25:15+00:00

I have two or more variables of class object in c# which has integer

  • 0

I have two or more variables of class object in c# which has integer values. i want to overload ‘+’ operator so that i won’t have to convert these variables when ever i want to add or subtract them. here is my code below:

public static object operator +( object obj1, object obj2)
{
    object o = Convert.toint32(obj1) + Convert.toint32(obj2);
   return o;
}

no the problem is i am getting an error saying "One of the parameters of a binary operator must be the containing type"

why is this happening? any help is appreciated!

  • 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-23T06:25:16+00:00Added an answer on May 23, 2026 at 6:25 am

    The compiler error tells you exactly what’s wrong – if you’re going to create a custom binary operator, at least one of the parameter types (for the operands) has to be the same as the type you’re declaring the operator in (or a nullable version of it, for value types).

    This is mandated in section 10.10.2 of the C# 4 specification:

    The following rules apply to binary operator declarations, where T denotes the instance type of the class or struct that contains the operator declaration:

    • A binary non-shift operator must take two parameters, at least one of which must have type T or T?, and can return any type.
    • A binary << or >> operator must take two parameters, the first of which must have type T or T? and the second of which must have type int or int?, and can return any type.

    Personally I would try to avoid having variables of type object if you know they’re actually int values. Why not have int variables instead?

    If you’re using C# 4, another alternative would be to make them dynamic variables, where the operator overloading would be applied at execution time rather than compile time.

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

Sidebar

Related Questions

If I have a class Person which has two instance variables: name of type
I have rough ideas - like dealing with singular/plural, two or more words/phrases that
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two classes, and want to include a static instance of one class
D has two types of constness: immutable variables are ones that were declared immutable,
The problem: I often have to update two or more repositories: One for the
I have these two pieces of code, wich one is more readable? foreach decimal
I have Boys and Toys. Two django models. Toys can be owned by more
I have two applications written in Java that communicate with each other using XML
I have a class called ItemBase from which a number of classes inherit. In

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.