本文介绍了如何在C#中返回一个字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在c#中使用return返回一个arraylist元素,但是错误是'不能隐式地将类型对象转换为字符串'。我的代码是...
Hi,I want to return an arraylist element using return in c#,But error is 'cannot implicitly convert type object to string'.My code is as...
protected String FindTargetMember(ArrayList MemberList,String TargetMember)
{
for (int i = 0; i < MemberList.Count; i++)
{
if (TargetMember.Equals(MemberList[i]))
{
return MemberList[i];
}
}
return 0;
}
推荐答案
Quote:
无法将类型对象隐式转换为字符串
cannot implicitly convert type object to string
那是因为 MemberList [i]
是一个对象
而不是 string
。
That's because MemberList[i]
is an object
not a string
.
protected List<string> FindTargetMember(ArrayList MemberList,String TargetMember)
{
List<string> list = new List<string>();
for (int i = 0; i < MemberList.Count; i++)
{
if (TargetMember.Equals(MemberList[i]))
{
list.Add(MemberList[i].Name.ToString());
// Assuming Name property is there in MemberList objeect. If it's not you can modify the code by yourself.
}
}
return list;
}
-KR
-KR
这篇关于如何在C#中返回一个字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!