I am migrating code from VB.Net to C# for Windows Mobile 6 applications.

Portion of that code is using Collection() from Microsoft.VisualBasic.

Private tbNew As New Collection

However, when I tried to use Collection() in C#, it gives me error. I guess such type does not exist.

What can I use instead?

有帮助吗?

解决方案

Use List

List<string> tbNew = new List<string>();
tbNew.Add("cat");
tbNew.Add("dog");

Or Dictionary

Dictionary<string, int> tbNew = new Dictionary<string, int>();
tbNew.Add("cat", 1);
tbNew.Add("dog", 4);

Or ArrayList

ArrayList tbNew = new ArrayList();
tbNew.Add("cat");
tbNew.Add(2);
tbNew.Add(false);

其他提示

You can use Collection as

Private Collection<string> dinosaurs = new Collection<string>();

Check out this Collection doc msdn

To initializes a new instance of the Collection<T> class that is empty. You can use this constructor

Collection<T>()

This will give you a whole idea about collections in c# check out this also Collections in c#

Hope this will help you.

You need to give type to your collection in C# like

private Collection<string> tbNew  = new Collection<string>();

i have given string as an example. it is

Collection<T> 

i.e. you can given any type.

For Compact Framework you can use Dictionary as an alternative

Collection is supported in following Frameworks

.NET Framework
   Supported in: 4.5, 4, 3.5, 3.0, 2.0
.NET Framework Client Profile
   Supported in: 4, 3.5 SP1
 Portable Class Library
   Supported in: Portable Class Library
.NET for Windows Store apps
   Supported in: Windows 8
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top