如何在C#中返回一个字符串数组 [英] How to return a string array in C#
本文介绍了如何在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
由于ArrayList
是一个对象集合,您必须将项目转换为字符串(您可以只返回输入字符串)。
但请注意:返回找到的项目的索引更有意义(或-1
是它没有找到。)
SinceArrayList
is a collection of objects, you have to cast the item to a string (you could just return the input string).
However, please note: it makes more sense to return the index of the found item (or-1
is it is not found).
这篇关于如何在C#中返回一个字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文