【发布时间】:2021-04-29 17:48:55
【问题描述】:
我正在尝试将文本文件读入 ArrayList。如果我在此行中包含:“ArrayList<String> messages = new ArrayList();”,则会收到非通用错误,但如果我删除它,则会在返回行中收到此错误:
无法隐式转换类型“System.Collections.ArrayList”
using System.Collections;
using System;
using System.IO;
namespace mhpreader
{
internal class NewBaseType
{
public ArrayList messages = new ArrayList();
internal NewMhpReader ReadMessages()
{
{
throw new NotImplementedException();
}
}
}
internal class NewMhpReader : NewBaseType
{
private string _FilePath;
public NewMhpReader(string FilePath)
{
this._FilePath = FilePath;
}
private string line;
public NewMhpReader[] ReadMessages(string nMessages)
{
ArrayList<String> messages = new ArrayList();
//List<TextReader> messages = new List<string>;
using (StreamReader stre = new StreamReader(_FilePath))
{
while ((line = stre.ReadLine()) != null)
{
messages.Add(line);
Console.WriteLine(messages);
}
}
return messages;
}
}
}
【问题讨论】:
-
使用List<T> 而不是
ArrayList,后者已过时。请参阅ArrayList文档中的 the remarks。 -
我将消息切换为 List
,但在返回消息时仍然出现此错误;行“不能隐式转换类型'System.Collections.Generic.List '” -
你没有返回你告诉你的方法期望的类型..