C#运算符重载 [英] C# Operator Overloading

查看:76
本文介绍了C#运算符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这段代码有什么问题?

Hi All,

What is wrong with this bit of code?

public static List<int> operator +(List<int> a, List<int> b)
        {
            if (!(a.Count == b.Count))
            {
                return a;
            }
            else
            {
                return b;
            }



我希望能够从一个List< int>中添加所有值.到另一个,但出现此错误:二进制运算符的参数之一必须是包含类型.

我看不出有什么问题.据我了解,我的参数中至少有一个是包含类型.

在此先感谢您.



I want to be able to add all values from one List<int> to another, but I get this error: One of the parameters of a binary operator must be the containing type.

I cannot see whats wrong. According to what I understand, at least one of my parameters IS the containing type.

Thanks in advance.

推荐答案

首先,您的代码不会将一个列表中的所有元素都添加到另一个列表中.它仅返回指定列表之一.

其次,为什么不使用List.AddRange方法代替(如果您确实要合并列表)?

First, your code doesn''t add all of the elements in one list to another list. It merely returns ONE of the specified lists.

Second, why don''t you use the List.AddRange method instead (if you really want to combine the lists)?

List<string> list1 =  new List<string>();
List<string> list2 =  new List<string>();

// ... add items to both lists

list1.AddRange(list2.ToArray());



现在,如果您尝试将一个列表中的实际值添加到另一列表中的值,我会改写一个扩展方法:



Now, if you''re trying to add the actual values in one list to the values in the other list, I would write an extension method instead:

public static void AddIntValues(this List<int> me, List<int> other)
{
    int max = Math.Min(me.Count, other.Count);
    for (int i = 0; i < max; i++)
    {
        me[i] += other[i];
    }
}



诚然,这是一个非常奇怪的要求.



This is, admittedly, a pretty bizarre requirement.


要重载运算符,您需要至少一个包含类的实例作为参数.也就是说,如果您有一个Customer类,那么您将像这样重载+运算符

To overload an operator you need to have at least one instance of the containing class as a parameter. i.e if you had a class Customer then you would overload the + operator like this

public static Customer operator +(Customer a, Customer b)
            {
                //Do stuff here
            }


您尝试执行此操作的方法是,如果您尝试重载List< int>的二进制运算符,这是不可能的.

如果我是你,我将使用从类List< customer>继承的Customer类推类.然后重载该类的二进制运算符,就像这样


The way you are trying to do it is if you are trying to overload the binary operator for List<int> which is not possible.

If I were you, I would have a class , using the Customer analogy that inherits from List<customer> and then over load the binary operator for that class, something like this

class Customer
        {
        }
        class Customers:List<Customer>
        {
            public static Customers operator +(Customers a, Customers b)
            {
                //Do stuff here
            }
        }


希望这对您有帮助


Hope this helps


您的意思是:
Do you mean this:
class MyList : List<int>
{
    public static MyList operator +(MyList a, MyList b)
    {
        if (!(a.Count == b.Count))
        {
            return a;
        }
        else
        {
            return b;
        }
    }
}



?



?


这篇关于C#运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆