簡體   English   中英

聲明Collection的正確方法是什么 <bool> 對象和不同的數據類型?

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

使用Collection對象和不同數據類型的正確方法是什么?

我正在獲取type or namespace Collection<bool> could not be found錯誤。 還發現成員變量_isFormData是bool,int和string。 :-/

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...
   { }
}

您需要using System.Collections.ObjectModel; 在您的課程中正確引用的類型的行。

該類型僅是布爾值的集合:

String.IsNullOrEmpty(contentDisposition.FileName)

測試contentDisposition.FileNamenull還是"" ,如果是,則返回true;否則,返回true。 否則為假。

isFormData[index]

在元素index處返回集合中的布爾值。

這是一個入門的示例。 請注意“使用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