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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:04:14+00:00 2026-05-25T20:04:14+00:00

is there a reason for the type of the second operand must be int?

  • 0

is there a reason for the type of the second operand must be int?

...
// I would like to do this
public static StringList operator<<(StringList list, string s) {
   list.Add(s);
   return list;
}
// but only int is supported...
...

EDIT:
Just for sure… I can overload operator* for get (for example) List of string

class MyString {
   string val;
   public MyString(string s) {
      val = s;
   }
   public static List<string> operator*(MyString s, int count) {
      List<string> list = new List<string>();
      while (count-- > 0) {
         list.Add(s.val);
      }
      return list;
   }
}

...
foreach (var s in new MyString("value") * 3) {
   s.print(); // object extension (Console.WriteLine)
}
// output:
// value
// value
// value
...

but cannot overload left shift, well known from C++ std (overloaded for output), because it was unclear?
Of course, it’s just a decision of C# designers.
Still it can be overloaded on something unexpected/unclear (with int).

Really the reason is that it was made an unclear code?

  • 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-25T20:04:14+00:00Added an answer on May 25, 2026 at 8:04 pm

    Yes. It’s because the language specification requires it:

    When declaring an overloaded shift operator, the type of the first operand must always be the class or struct containing the operator declaration, and the type of the second operand must always be int.

    The language designers didn’t have to make that decision – it would have been possible for them to remove that restriction if the wanted to – but I think this part of the specification explains their reasoning for this (and other) restrictions on operator overloading:

    While it is possible for a user-defined operator to perform any computation it pleases, implementations that produce results other than those that are intuitively expected are strongly discouraged.

    They probably wanted the bitshift operators to always behave like bitshift operators, and not as something completely surprising.

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

Sidebar

Related Questions

Is there ever a reason to use Type parameters over generics, ie: // this...
Is there a reason you would not set -static-link-runtime-shared-libraries to true when compiling an
This works: #include<cstdio> class A{ public: A(int a):var(a){} int var; }; int f(A obj)
for some reason this code throws an error // map<int, AnItem> roomlist; // map<string,
In C++, is there any reason to not access static member variables through a
Is there any (technical) reason that C# requires all generic type parameters to be
There was some code like this: // Convenience to make things more legible in
Is there a reason why I can use a generic function with different type
For some reason there's a variable called d that is defined immediately after I
is there any reason to use the post-redirect-get (prg) for a request that you

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.