C# 移位运算符重载

发布于 2024-12-06 21:18:35 字数 876 浏览 0 评论 0原文

第二个操作数的类型必须是 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...
...

编辑: 可以肯定的是...我可以为 get 重载运算符*(例如)字符串列表,

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
...

但不能重载左移,这在 C++ std 中众所周知(为输出重载),因为不清楚? 当然,这只是 C# 设计者的决定。 尽管如此,它仍然可能在意外/不清楚的情况下重载(使用 int )。

真的是因为代码写的不清楚吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟燃烟灭 2024-12-13 21:18:35

是的。这是因为语言规范需要它:

声明重载移位运算符时,第一个操作数的类型必须始终是包含该运算符声明的类或结构,第二个操作数的类型必须始终是 int。

语言设计者不必做出这个决定 - 如果他们愿意的话,他们本来可以取消该限制 - 但我认为规范的这一部分解释了他们这样做的理由(并且other) 运算符重载的限制:

虽然用户定义的运算符可以执行任何它喜欢的计算,但强烈建议不要产生与直观预期不同的结果。

他们可能希望位移运算符始终表现得像位移运算符,而不是完全令人惊讶的东西。

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.

眼眸 2024-12-13 21:18:35

因为你希望你的代码看起来像 C++?为什么不使用返回源列表的扩展方法 .Append( item )

public static class ListExtensions
{
    public static List<T> Append<T>( this List<T> source, T item )
    {
        if (source == null)
        {
            throw new NullArgumentException( "source" );
        }
        source.Add(item);
        return source;
    }
}

用作:

var list = new List<string>();
list.Append( "foo" )
    .Append( "bar" )
    .Append( "baz" );

Because you want your code to look like c++? Why not an extension method .Append( item ) that returns the source list.

public static class ListExtensions
{
    public static List<T> Append<T>( this List<T> source, T item )
    {
        if (source == null)
        {
            throw new NullArgumentException( "source" );
        }
        source.Add(item);
        return source;
    }
}

Used as:

var list = new List<string>();
list.Append( "foo" )
    .Append( "bar" )
    .Append( "baz" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文