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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:10:22+00:00 2026-06-14T18:10:22+00:00

I need to count all checked checkboxes and I was meaning to use operator

  • 0

I need to count all checked checkboxes and I was meaning to use operator overloading as this example :

public static int operator +(bool b1, bool b2)
{
    int i1 = 0;
    int i2 = 0;

    if (b1) i1 = 1;
    if (b2) i2 = 1;

    return i1 + i2;
}

And then the total would be simply retrieved by

int countCbx = cbx1.Checked + cbx2.Checked + ...

But it don’t work and I don’t see why. The compilation error is “One of the parameters of a binary operator must be the containing type”.
The logic seems good, but it’s the first time I use operator overload outside example.

Thanks for your help.

  • 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-14T18:10:23+00:00Added an answer on June 14, 2026 at 6:10 pm

    But it don’t work and I don’t see why.

    Because the language specification says so. The compiler is basically giving you the reason.

    The logic seems good, but it’s the first time I use operator overload outside example.

    The logic of the body of the member is fine – but you can’t overload operators for arbitrary types. At least one of the operands (or the return type for conversions) has to be the type that you’re declaring the operator in.

    The C# 5 specification contains this in section 10.10:

    Like other members, operators declared in a base class are inherited by derived classes. Because operator declarations always require the class or struct in which the operator is declared to participate in the signature of the operator, it is not possible for an operator declared in a derived class to hide an operator declared in a base class. Thus, the new modifier is never required, and therefore never permitted, in an operator declaration.

    That rule is then enforced in 10.10.1:

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

    • A unary +, -, !, or ~ operator must take a single parameter of type T or T? and can return any type.
    • …

    …. 10.10.2:

    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.

    … and 10.10.3:

    For a given source type S and target type T, if S or T are nullable types, let S0 and T0 refer to their underlying types, otherwise S0 and T0 are equal to S and T respectively. A class or struct is permitted to declare a conversion from a source type S to a target type T only if all of the following are true:

    • S0 and T0 are different types.
    • Either S0 or T0 is the class or struct type in which the operator declaration takes place.
    • …
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to count all elements who has id like 'hi*'. For example, if
I need to count all distinct values from table LDS where status = 'ok'
I have a table and I need to count all unique users on it.
I need to get a get a count of all the distinct subnets in
I need to count number of repeating and position where they repeat of all
I'm currently counting all the checked checkboxes in an asp.net gridview using: $('#cphMain_gdvSalesOrder').delegate('input:checkbox', 'click',
Need to generate courses list and count all unanswered answered but unchecked Questions. My
Need to generate courses list and count all unanswered answered but unchecked Questions. Here's
I need T-SQL to count Count All incomplete SQL Transactions running on database in
Short Need to generate courses list and count all unanswered answered but unchecked Questions.

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.