list的问题

chaye12 2008-10-23 08:09:16
下面是我看的一个帖子
using System;
using System.Collections.Generic;

namespace Hello
{
class Program
{
static void Main()
{
string[] arr ={ "aa", "bb", "cc" };
string s = "bb";

Console.WriteLine("此字符{0}在arr中", new List<string>(arr).Contains(s) ? "" : "不"); //注释

}
}
}

其中我加注释的那条语句,我不太明白为什么是new List<string>(arr).Contains(s),我想换成arr.Contains(s)不知道是否能运行成功,由于家里没有运行环境,还请大家帮忙测试下!
...全文
152 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复
new List<string>() 是调用 List<T> 类的构造函数。
chaye12 2008-10-23
  • 打赏
  • 举报
回复
结贴,继续开贴,我还没彻底明白!
霜寒月冷 2008-10-23
  • 打赏
  • 举报
回复
空军已经说的非常清楚了啊.佩服啊.
lz应该多多思考下.在问!
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复
没问题,加上 new List<string> 转换为 List<T> 后就可以用 Contains() 方法了。
chaye12 2008-10-23
  • 打赏
  • 举报
回复
我晕,理解能力怎么了??你不会的时候不问吗?我就是不懂为什么加上new List <string>,这样也有问题吗?
vrhero 2008-10-23
  • 打赏
  • 举报
回复
lz的理解能力实在是...无语...
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 chaye12 的回复:]
为什么不new List <string>.Contains(s)而是要加上(arr)强转下呢?
[/Quote]

new List <string>.Contains(s) 这样写的话 arr 到哪里去了?
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复
况且 LZ 的原始问题是

[Quote=引用楼主 chaye12 的帖子:]
我想换成arr.Contains(s)不知道是否能运行成功
[/Quote]

我在2楼就已经明确回答了:

[Quote=引用 2 楼 wuyi8808 的回复:]
Array 类没有 Contains() 方法,不能运行成功。
[/Quote]

怎么能说:

[Quote=引用 10 楼 chaye12 的回复:]
恩,我想问的现在还没人解决下
[/Quote]
chaye12 2008-10-23
  • 打赏
  • 举报
回复
这样的用法我没用过,你上面回复的意思我看明白了,但是为什么要new List<string>(arr).Contains(s)

也就是说为什么不new List<string>.Contains(s)而是要加上(arr)强转下呢?或者说用arr 构造一个 List <T>啥意思?
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复
因为 Array 类没有 Contains() 方法,而 List <T> 类有 Contains() 方法,所以。。。。

new List <string>(arr).Contains(s)


。。。。用 arr 构造一个 List<T>,然后调用 Contains() 方法。
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复


[Quote=引用 3 楼 wuyi8808 的回复:]
http://msdn.microsoft.com/zh-cn/library/system.array_methods.aspx

这里在线的 msdn 网页是 Array 类的方法,你可以看一下,其中并没有 Contains() 方法。
[/Quote]

[Quote=引用 5 楼 wuyi8808 的回复:]
http://msdn.microsoft.com/zh-cn/library/s6hkc2c4.aspx

这里在线的 msdn 网页是 List <t> 类的方法,你可以看一下,其中有 Contains() 方法。
[/Quote]

[Quote=引用 10 楼 chaye12 的回复:]
我想问这句new List <string>(arr).Contains(s)

为什么这么写!!
[/Quote]


我在3楼和5楼已经回答了:

因为 Array 类没有 Contains() 方法,而 List<T> 类有 Contains() 方法,所以。。。。
chaye12 2008-10-23
  • 打赏
  • 举报
回复
恩,我想问的现在还没人解决下,我想问这句new List<string>(arr).Contains(s)

为什么这么写!!
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 min_jie 的回复:]
都是空军惹的祸。。呵呵。。。
[/Quote]

[Quote=引用 6 楼 slimfeng 的回复:]
楼上的,楼主问的不是array类
arr是定义的字符串数组,数组当然没有Contains方法
[/Quote]

[Quote=引用 8 楼 slimfeng 的回复:]
那是通过arr数组自定义了一个list对象,调用的list对象的Contains方法
[/Quote]

http://topic.csdn.net/u/20081023/19/059072c2-aaa4-4951-a90e-9361c26d9532.html
请参见上面这个帖子的7楼我的回复。
slimfeng 2008-10-23
  • 打赏
  • 举报
回复
那是通过arr数组自定义了一个list对象,调用的list对象的Contains方法
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 slimfeng 的回复:]
楼上的,楼主问的不是array类
arr是定义的字符串数组,数组当然没有Contains方法
[/Quote]

任何数组都是 Array 类。
slimfeng 2008-10-23
  • 打赏
  • 举报
回复
楼上的,楼主问的不是array类
arr是定义的字符串数组,数组当然没有Contains方法
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/zh-cn/library/s6hkc2c4.aspx

这里在线的 msdn 网页是 List<t> 类的方法,你可以看一下,其中有 Contains() 方法。
hao1hao2hao3 2008-10-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 min_jie 的回复:]
都是空军惹的祸。。呵呵。。。
[/Quote]


记忆力够好啊!
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/zh-cn/library/system.array_methods.aspx

这里在线的 msdn 网页是 Array 类的方法,你可以看一下,其中并没有 Contains() 方法。
wuyi8808 2008-10-23
  • 打赏
  • 举报
回复
Array 类没有 Contains() 方法,不能运行成功。
加载更多回复(1)
List<String>不能赋值给List List<String>不能赋值给List<Object> 1.首先,二者不是父子类关系。 如果是普通的父子类关系,由于Java的多态性,底层的后期绑定机制会在运行时检索子类方法列表,从而实现多态。 2.List<T>属于泛型。(也算是多态的...

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧