在c#中有没有办法返回一个字符串参数和一个列表给方法?
List<cCountry> countries = new List<cCountry>();
countries = GetCountries(userProfile.Country);
private List<cCountry> GetCountries(string p)
{
inputCollection = new cDTOCollection<cDTOBase>();
outputCollection = new cDTOCollection<cDTOBase>();
outputCollection = UpdateProfileBizobj.ProcessRequest(ActionConstants.ActionGetCountriesList, null);
List<cCountry> countries = new List<cCountry>();
cDTOSingleValue SelectedCountryID = new cDTOSingleValue();
foreach (cDTOCountry countryItem in outputCollection)
{
if (p == countryItem.CountryName)
SelectedCountryID.Id = countryItem.CountryID;
countries.Add(Mapper.Map<cDTOCountry, cCountry>(countryItem));
}
countries.Remove(countries[0]);
return countries;
}
Like in the method above I need to return a parameter SelectedCountryID and also the countries list.Is there any way to return both?
最佳答案
填充并返回一个对象。
public class ReturnObject
{
public List<cCountry> Countries { get; set; }
public guid SelectedCountryID { get; set; } // don't think you defined what type SelectedCountryID is in your question
}
但是如果您发现自己需要返回超过 1 个东西,这可能表明您的方法做的太多了,应该重构。
为什么不能重用发送给方法的值?
关于c# - 如何在 C# 中同时返回一个字符串和一个元素列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31207950/