c# - 如何在 C# 中同时返回一个字符串和一个元素列表?

标签 c#

在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/

相关文章:

c# - .Net MVC 中的 URL 重写

C# foreach with string - 忽略最后一个字符

c# - DateTimeFormatter.Format() 和字符串之间的区别

c# - 为什么 Equals 没有按预期工作

c# - 如何在 xamarin 中获得焦点 VisualElement?

c# - 优化 LINQ 将多个列表组合成新的通用列表

c# - 如何将并发包转换为列表?

c# - 复制和粘贴 Excel 数据时,颜色丢失

c# - C# 中的 task.continuewith 与数据流

c# - MVVM WPF : Get usercontrol name from ViewModel