Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6113211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:49:58+00:00 2026-05-23T14:49:58+00:00

How do i convert a collection to count? Where when a collection is passed

  • 0

How do i convert a collection to count?

Where when a collection is passed the converter should be able to return the count for say the following collections,

Dictionary, ObservableCollection or List

right now i have the following but doesn’t work,

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return ((System.Collections.ICollection)value) != null ? ((System.Collections.ICollection)value).Count : 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:49:59+00:00Added an answer on May 23, 2026 at 2:49 pm

    If we define a “collection” as something that implements ICollection or ICollection<T> then the Count property is available to tell us the number of elements in the collection if that is what we need to know.

    If we wish to calculate some value based upon ICollection.Count but modified in some way then we can create a Generic Method to calculate the value for us. For example, a generic method Convert<T> could take an ICollection<T> value formal parameter. Such a function could be invoked using anything that implements ICollection<T> as an actual parameter.

    Because the compiler can infer the generic type argument we don’t need to explicitly specify the type argument when invoking the generic method (although we can do so if we want or need to).

    For example…

    class Program
    {
        static public int Convert<T>(ICollection<T> value, Type targetType, object parameter, CultureInfo culture)        {
            return value.Count;
        }
    
        static void Main(string[] args)
        {
            Dictionary<int, string> di = new Dictionary<int,string>();
            di.Add(1, "One");
            di.Add(2, "Two");
            di.Add(3, "Three");
            Console.WriteLine("Dictionary count: {0}", di.Count);
            Console.WriteLine("Dictionary Convert: {0}", Convert(di, null, null, null));
    
            ObservableCollection<double> oc = new ObservableCollection<double>();
            oc.Add(1.0);
            oc.Add(2.0);
            oc.Add(3.0);
            oc.Add(4.0);
            Console.WriteLine("ObservableCollection Count: {0}", oc.Count);
            Console.WriteLine("ObservableCollection Convert: {0}", Convert(oc, null, null, null));
    
            List<string> li = new List<string>();
            li.Add("One");
            li.Add("Two");
            li.Add("Three");
            li.Add("Four");
            li.Add("Five");
            Console.WriteLine("List Count: {0}", li.Count);
            Console.WriteLine("List Convert: {0}", Convert(li, null, null, null));
    
            Console.ReadLine();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What kind of collection I should use to convert NameValue collection to be bindable
I would like to ask: how do you convert a Collection to a List
I need to convert a passed in argument (single object or collection) to an
I have a collection of domain objects that I need to convert into another
I'm receiving this error in my Linq statement --- Cannot implicitly convert type 'System.Collections.Generic.IEnumerable'
Please suggest some elegant way to convert arrays of arrays to collections of collections
I have an array of strings. How can I convert it to System.Collections.ArrayList?
How do you convert the following C# code to VB.NET? private static readonly ICollection<string>
FeatureEvents.bit_Activate = Convert.ToBoolean(collection[bit_Activate]); bit_Activate is checkbox ,how would i convert it to boolean above,collection
Im trying to return a list to a display template containing data from two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.