清单< T> ==操作符在C#语言规范第4版 [英] List<T> operator == In the C# Language Specification Version 4

查看:140
本文介绍了清单< T> ==操作符在C#语言规范第4版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#语言规范第4版,1.6.7.5运营商约信息列表< T> 运营商: == != 。但我不能找到这样定义运营商名单< T> ? ?从1.6.7.5运营商我缺少的东西。



示例代码:

 列表与LT; INT>一个=新名单< INT>(); 
a.Add(1);
a.Add(2);
名单,LT; INT> B =新的List< INT>();
b。增加(1);
b。增加(2);
Console.WriteLine(一== b)条; //输出真=>在这里,我得到错误,以及
b。增加(3);
Console.WriteLine(一== b)条; //输出假


解决方案

该规范是确实是正确的虽然混乱。该规范定义了一类名为List(可怜的命名选择)。




下表显示了名为List泛型类,它实现可增长的对象列表。该类包含最常见的函数成员的几个例子。




本类可以在规范的1.6.7节中可以看出。等于运算符重载和匹配的输出上面的解释。也许一个更好的名字已选定该类

 静态布尔等于(列表< T>一种,列表< T> b){
如果(A == NULL)回复b == NULL;
如果(二== NULL || a.count = b.count!)返回false;
的for(int i = 0; I< a.count;我++){$​​ B $ B如果{$ B(的Object.Equals(a.items [I],b.items [I])!) $ b返回FALSE;
}
}
返回真;
}


In the C# Language Specification Version 4, 1.6.7.5 Operators is information about List<T> operators: == and !=. But I can't find such operators defined in List<T>? Am I missing something?

Sample code from 1.6.7.5 Operators:

List<int> a = new List<int>();
a.Add(1);
a.Add(2);
List<int> b = new List<int>();
b.Add(1);
b.Add(2);
Console.WriteLine(a == b); // Outputs "True" => here I get False as well
b.Add(3);
Console.WriteLine(a == b); // Outputs "False"

解决方案

The spec is indeed correct, although confusing. The spec defines a class called List (poor naming choice).

The following table shows a generic class called List, which implements a growable list of objects. The class contains several examples of the most common kinds of function members.

This class can be seen in the spec at section 1.6.7. The Equals operator is overloaded and matches the output explained above. Perhaps a better name should have been chosen for that class.

static bool Equals(List<T> a, List<T> b) {
    if (a == null) return b == null;
    if (b == null || a.count != b.count) return false;
    for (int i = 0; i < a.count; i++) {
        if (!object.Equals(a.items[i], b.items[i])) {
            return false;
        }
    }
  return true;
}

这篇关于清单&LT; T&GT; ==操作符在C#语言规范第4版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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