List<String> comnameList = new List<string>();
        List<String> comipList = new List<string>();
        List<String> comcomList = new List<string>();public void GetList(out List<string>comnameList, out  List<string>comipList, out List<string>comcomList)
        {
           con.ConnectionString = @"User id=sa;pwd=.;database=db; ;server=(local)";
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "select comname, ip, community from comindex";
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                comnameList.Add(String.Format("{0}", reader[0]));
                comipList.Add(String.Format("{0}", reader[1]));
                comcomList.Add(String.Format("{0}", reader[2]));
            }
            reader.Close();
            con.Close();
        }出现错误:使用了未赋值的 out 参数“comcomList”
          使用了未赋值的 out 参数“comipList”
          使用了未赋值的 out 参数“comcomList”
是为何呢?c#List<string>SQLout

解决方案 »

  1.   


    定义方法:
      public List<CompanyEmployee> GetEmployees(string sql, out List<string> listKeys)
            {
                listKeys = new List<string>();
                listKeys.Clear();
                Context.CompanyEmployee.Name = "a";
                var query = Context.CompanyEmployee.Where(sql).ToList();
                foreach (var item in query)
                {
                    listKeys.Add(item.EmpId);
                }
                return query;
            }
    调用:
     List<string> listTemp = null;
    GetEmployees(sql, out listTemp);
      

  2.   

    因为while (reader.Read())条件有可能不满足,
    这个时候返回的list是空的,而out要求返还前要赋值的
      

  3.   


            public void GetList(out List<string> comnameList, out  List<string> comipList, out List<string> comcomList)
            {
                //加上这几个试试呢、、、
                comnameList = new List<string>();
                comipList = new List<string>();
                comcomList = new List<string>();
                           con.ConnectionString = @"User id=sa;pwd=.;database=db; ;server=(local)";
                con.Open();
                cmd.Connection = con;
                cmd.CommandText = "select comname, ip, community from comindex";
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    comnameList.Add(String.Format("{0}", reader[0]));
                    comipList.Add(String.Format("{0}", reader[1]));
                    comcomList.Add(String.Format("{0}", reader[2]));
                }
                reader.Close();
                con.Close();
            }
      

  4.   

    对Out而言,必须在方法中对其完成初始化。