下面是我看的一个帖子 
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 构造一个 List <T>,然后调用 Contains() 方法,就可行了呢?难道是先强制转换成LIST<T>的意思???只不过()放在了后面?

解决方案 »

  1.   

    等于以下代码:using System;
    using System.Collections.Generic;namespace Hello
    {
        class Program
        {
            static void Main()
            {
                string[] arr ={ "aa", "bb", "cc" };
                string s = "bb";            List<string> lst;             //定义一个string类型的List
                lst=new List<string>(arr);    //用现有的数组arr来构建一个List
              Console.WriteLine("此字符{0}在arr中", lst.Contains(s) ? "" : "不"); //注释
               
            }
        }

      

  2.   

    空军在上个帖子的回复是这样的:
    new List <string>() 是调用 List <T> 类的构造函数为什么这样调用呢?我list<T>看了很多遍就是不理解,我操!
      

  3.   

    list <T> 是泛型不理解,就先跳过……很少用到……
      

  4.   

    发表于:2008-10-23 21:09:14   21楼 得分:0 new List <string>() 是调用 List <T> 类的构造函数。 那个回复是在 21:09:14 ,在 LZ 发这个帖之前。
      

  5.   

    List<T> 你可以当作一个整体来理解。
      

  6.   

    List<T> 你可以当作一个整体来理解。new List<T>() 就是调用 List<T> 类的构造函数,就象 new Button() 就是调用 Button 类的构造函数。
      

  7.   

    我知道是泛型,以前也用过,就是用的比较少!看了2楼的明白了,彻底明白了,就是list<T>()中,()里面的参数很少用到!!!
      

  8.   

    以 arr 作为参数调用 List<T> 的构造函数,然后再调用 Contains() 方法。
      

  9.   

    new List <string>(arr).Contains(s)等价于(new List <string>(arr)).Contains(s)因为 C# 的 new 运算符和 . 运算符优先级相同,且是从左到右结合,所以可以省略 ()。
      

  10.   

    http://msdn.microsoft.com/zh-cn/library/6a71f45d.aspx上面的 msdn 网址列出了 C# 运算符的优先级,new 和 . 都在第1组,属于最高优先级。