无法将List ‘隐式转换为’double’

继续投掷,我的代码的这部分有什么问题,当我想返回单元格时收到此错误不能隐式地将类型’System.Collections.Generic.List’转换为’double’:

public double readFileToList(string Path) { var cells = new List(); string path = label3.Text; if (File.Exists(path)) { double temp = 0; cells.AddRange(File.ReadAllLines(path) .Where(line => double.TryParse(line, out temp)) .Select(l => temp) .ToList()); int totalCount = cells.Count(); cellsNo.Text = totalCount.ToString(); } return cells; } 

很难说没有看到你的整个函数,但我的猜测是你的函数的返回类型设置为double而不是List 。 这会导致您看到的错误。


编辑

确认看你的编辑,这是你的问题。 将函数的返回类型更改为List ,您将很高兴! 您的代码应如下所示:

 public List readFileToList(string Path) { var cells = new List(); string path = label3.Text; if (File.Exists(path)) { double temp = 0; cells.AddRange(File.ReadAllLines(path) .Where(line => double.TryParse(line, out temp)) .Select(l => temp) .ToList()); int totalCount = cells.Count(); cellsNo.Text = totalCount.ToString(); } return cells; }