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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:36:50+00:00 2026-06-13T13:36:50+00:00

I wanted to overload an operator in a class , and have it private

  • 0

I wanted to overload an operator in a class , and have it private so that it could only be used from within the class.
However, when I tried to compile I got the error message “User-defined operator … must be declared static and public:

Why do they have to be public?

  • 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-13T13:36:51+00:00Added an answer on June 13, 2026 at 1:36 pm

    To answer half part of your question you may see the blog post by Eric Lippert.

    Why are overloaded operators always static in C#?

    Rather, the question we should be asking ourselves when faced with a
    potential language feature is “does the compelling benefit of the
    feature justify all the costs?” And costs are considerably more than
    just the mundane dollar costs of designing, developing, testing,
    documenting and maintaining a feature. There are more subtle costs,
    like, will this feature make it more difficult to change the type
    inferencing algorithm in the future? Does this lead us into a world
    where we will be unable to make changes without introducing backwards
    compatibility breaks? And so on.

    In this specific case, the compelling benefit is small. If you want to
    have a virtual dispatched overloaded operator in C# you can build one
    out of static parts very easily. For example:

    public class B {
        public static B operator+(B b1, B b2) { return b1.Add(b2); }
        protected virtual B Add(B b2) { // ...
    

    And there you have it. So, the benefits are small. But the costs are
    large. C++-style instance operators are weird. For example, they break
    symmetry. If you define an operator+ that takes a C and an int, then
    c+2 is legal but 2+c is not, and that badly breaks our intuition about
    how the addition operator should behave.

    For your question’s other part: Why they should be public.

    I was not able to find any authentic source, but IMO, if they are not going to be public, and may be used inside a class only, then this can be done using a simple private method, rather than an overloaded operator.

    You may see the blog post by RB Whitaker

    Operator Overloading

    All operator overloads must be public and static, which should make
    sense, since we want to have access to the operator throughout the
    program, and since it belongs to the class as a whole, rather than any
    specific instance of the class.

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

Sidebar

Related Questions

Wanted to know if there was a way one could query shelveset details from
just wanted to ask where I define initial class properties? From other languages I
The following code prints: generic overload But what I wanted is that the overload
I have an 'optional' parameter on a method that is a KeyValuePair. I wanted
I have a PHP Yii application that uses an RSS feed reader. I wanted
I am trying to overload the = operator on a simple C++ class called
I have a class called RawReader that reads the bytes of some resource, parses
I wanted to know that if I am doing correctly, regarding retain and release
I have defined a class (call it class A) in C# and overloaded some
So can you overload operators to handle objects in multiple classes (specifically private members.)

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.