如何在 C# 中返回字符串列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1036829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:40:07  来源:igfitidea点击:

How to return a list of strings in C#

c#visual-c++com

提问by Cute

Can anybody tell me How to store and return List of Strings.

谁能告诉我如何存储和返回字符串列表。

I am asked this Because i have written a function which returns Collection of strings and I want to prepare a COM for that one and need to consume that COM(to get the returned list ) in VC++ where I can extend some functionality using that list of strings. I hope this would be clear.

我被问到这个因为我已经编写了一个返回字符串集合的函数,我想为那个函数准备一个 COM,并且需要在 VC++ 中使用该 COM(以获取返回的列表),我可以使用该列表扩展一些功能字符串。我希望这会很清楚。

采纳答案by Canavar

List<string>or string[] are the best options.

List<string>或 string[] 是最好的选择。

Here is a sample method that returns list of strings :

这是一个返回字符串列表的示例方法:

public static List<string> GetCities()
{
  List<string> cities = new List<string>();
  cities.Add("Istanbul");
  cities.Add("Athens");
  cities.Add("Sofia");
  return cities;
}

回答by Blixt

You can store a fixed list of strings as an array:

您可以将固定的字符串列表存储为数组:

string[] myStrings = {"Hello", "World"};

Or a dynamic list as a List<string>:

或者动态列表作为List<string>

List<string> myStrings = new List<string>();
myStrings.Add("Hello");
myStrings.Add("World");

回答by Brian Rasmussen

In C# you can simply return List<string>, but you may want to return IEnumerable<string>instead as it allows for lazy evaluation.

在 C# 中,您可以简单地 return List<string>,但您可能想要 return ,IEnumerable<string>因为它允许延迟计算。

回答by Daniel Earwicker

Yesterday you asked how to do this via COM interop! Why the step backward?

昨天您询问如何通过 COM 互操作来执行此操作!为什么要退步?

How to return a collection of strings from C# to C++ via COM interop

如何通过 COM 互操作将字符串集合从 C# 返回到 C++

回答by Rony

public static IList<string> GetStrings()
{
  foreach( var item in GetStringItems())
  yield return item;
}

回答by JBRWilkinson

There are many ways to represent a list of strings in .NET, List<string> being the slickest. However, you can't return this to COM because:

在 .NET 中有很多方法来表示字符串列表,List<string> 是最灵活的。但是,您不能将其返回给 COM,因为:

  1. COM doesn't understand .NET Generics

  2. FxCop will tell you that it's bad practice to return an internal implementation of something (List) rather than an abstract interface (IList / IEnumerable).

  1. COM 不理解 .NET 泛型

  2. FxCop 会告诉您,返回某些内容的内部实现(List)而不是抽象接口(IList / IEnumerable)是不好的做法。

Unless you want to get into really scary Variant SafeArray objects (not recommended), you need to create a 'collection' object so that your COM client can enumerate the strings.

除非您想进入非常可怕的 Variant SafeArray 对象(不推荐),否则您需要创建一个“集合”对象,以便您的 COM 客户端可以枚举字符串。

Something like this (not compiled - this is just an example to get you started):

像这样的东西(未编译 - 这只是让您入门的示例):

[COMVisible(true)]
public class CollectionOfStrings
{
  IEnumerator<string> m_enum;
  int m_count;

  public CollectionOfStrings(IEnumerable<string> list)
  { 
    m_enum = list.GetEnumerator();
    m_count = list.Count;
  }

  public int HowMany() { return m_count; }

  public bool MoveNext() { return m_enum.MoveNext(); }

  public string GetCurrent() { return m_enum.Current; }
}

(See http://msdn.microsoft.com/en-us/library/bb352856.aspx for more help)

(有关更多帮助,请参阅http://msdn.microsoft.com/en-us/library/bb352856.aspx