简体   繁体   English

声明Collection的正确方法是什么 <bool> 对象和不同的数据类型?

[英]What's the proper way to declare Collection<bool> object and of different datatype?

What's the proper way to use Collection object & of different datatype? 使用Collection对象和不同数据类型的正确方法是什么?

I'm getting type or namespace Collection<bool> could not be found error. 我正在获取type or namespace Collection<bool> could not be found错误。 Also found that member variable _isFormData is both bool, int and string at once. 还发现成员变量_isFormData是bool,int和string。 :-/ :-/

Saw this example, under accepted answer, at Web API: how to access multipart form values when using MultipartMemoryStreamProvider? Web API的公认答案下看到了这个示例:使用MultipartMemoryStreamProvider时如何访问多部分表单值?

private Collection<bool> _isFormData = new Collection<bool>();  //bool...

_isFormData.Add(String.IsNullOrEmpty(contentDisposition.FileName));  //string...

for (int index = 0; index < Contents.Count; index++)
{
   if (_isFormData[index]) //int...
   { }
}

You need to have a using System.Collections.ObjectModel; 您需要using System.Collections.ObjectModel; line for the type to referenced correctly in your class. 在您的课程中正确引用的类型的行。

The type is only ever a collection of booleans: 该类型仅是布尔值的集合:

String.IsNullOrEmpty(contentDisposition.FileName)

tests whether contentDisposition.FileName is null or "" and returns true if so; 测试contentDisposition.FileNamenull还是"" ,如果是,则返回true;否则,返回true。 false otherwise. 否则为假。

isFormData[index]

returns the bool value in the collection at element index . 在元素index处返回集合中的布尔值。

Here's an example to get you started. 这是一个入门的示例。 Note the "using System.Collections.ObjectModel" 请注意“使用System.Collections.ObjectModel”

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Program
{
   public static void Main (String[] args)
   {
       IList<Boolean> testCollection = new Collection<Boolean>();
       testCollection.Add(false);
       testCollection.Add(true);
       FillCollection(testCollection);

       for (int index = 0; index < testCollection.Count; index++)
       {
           if (testCollection[index])
           {
               Console.WriteLine("testCollection[{0}] is true", index);
           }
           else
           {
               Console.WriteLine("testCollection[{0}] is false", index);   
           }
       }
   }

   public static void FillCollection (IList<Boolean> collection)
   {
       Random random = new Random();
       for (int i = 0; i < 500; i++)
       {
           Boolean item = Convert.ToBoolean(random.Next(0, 2));
           collection.Add(item);
       }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM