![](/img/trans.png)
[英]TreeView: Cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.IEnumerable
[英]C#, cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'
我有一些C#代碼:
var oldLines = System.IO.File.ReadAllLines(path);
var newLines = oldLines.Where(line => !line.Contains(wordToDelete));
System.IO.File.WriteAllLines(path, newLines);
該代碼可在新的Windows應用程序中使用。 但是,當我將該代碼粘貼到現有應用程序中時,出現以下錯誤:
Error 2 Argument 2: cannot convert from
'System.Collections.Generic.IEnumerable<string>' to 'string[]'
Error 1 The best overloaded method match for
'System.IO.File.WriteAllLines(string, string[])' has some invalid
arguments
為什么會在新項目中引發此錯誤,而不是在我的舊項目中引發該錯誤?
oldLines.Where(line =>!line.Contains(wordToDelete)); 返回IEnumerable <字符串>
System.IO.File.WriteAllLines(path, newLines.ToArray());
會解決的
這可能是由另一個框架版本目標引起的。
newLines
是IEnumerable<string>
而不是string[]
,但是您的.NET版本(我認為是3.5)沒有接受 .NET 4中引入的IEnumerable<String>
的重載 。
因此,您只需要為File.WriteAllLines
創建一個string[]
或至少使用.NET 4:
System.IO.File.WriteAllLines(path, newLines.ToArray());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.