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 9250835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:32:38+00:00 2026-06-18T10:32:38+00:00

I have an object . This object is casting an Items Container (I don’t

  • 0

I have an object.

This object is casting an Items Container (I don’t know what items, but I can check).

But is there any code which can help me find how many items it contains?

I mean

 object[] arrObj = new object[2] {1, 2};
 object o = (object)arrObj;

In this case arrObj is an array so I can check:

((Array)o).Length //2

But what if I have those 2 others ?

 ArrayList al = new ArrayList(2);
           al.Add(1);
           al.Add(2);
 object o = (object)al ;

and

 List<object> lst= new List<object>(2);
 object o = (object)lst;

Is there any general code which can help me find how many items are in this casted object (o in this samples) ?

Of course I can check if (o is ...) { } but Im looking for more general code.

  • 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-06-18T10:32:39+00:00Added an answer on June 18, 2026 at 10:32 am

    Well the most basic interface it could implement would be IEnumerable. Unfortunately even Enumerable.Count from LINQ is implemented for IEnumerable<T>, but you could easily write your own:

    public static int Count(IEnumerable sequence)
    {
        // Shortcut for any ICollection implementation
        var collection = sequence as ICollection;
        if (collection != null)
        {
            return collection.Count;
        }
    
        var iterator = sequence.GetEnumerator();
        try
        {
            int count = 0;
            while (iterator.MoveNext())
            {
                count++;
            }
            return count;
        }
        finally
        {
            IDisposable disposable = iterator as IDisposable;
            if (disposable != null)
            {
                disposable.Dispose();
            }
        }
    }
    

    Note that this is basically equivalent to:

    int count = 0;
    foreach (object item in sequence)
    {
        count++;
    }
    

    … except that because it never uses Current, it wouldn’t need to do any boxing if your container was actually an int[] for example.

    Call it with:

    var sequence = container as IEnumerable;
    if (sequence != null) 
    {
        int count = Count(sequence);
        // Use the count
    }
    

    It’s worth noting that avoiding boxing really is a bit of a micro-optimization: it’s unlikely to really be significant. But you can do it once, just in this method, and then take advantage of it everywhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(I know this can be done in RMI, but I need to do this
I have this object: stdClass Object ( [daily_inventoryID] => 1 [inventory_timestamp] => 2012-06-08 14:35:42
I have this object I'm loading with THREE.objLoader and then create a mesh with
I have this object: class a { public string Application; public DateTime From, To;
I have this object which is an instance of a superclass. I want to
I have this object below: I thought about putting the messages in html like
I have an object with a property called name. This object has a sub
Let's say I have an object - a User object in this case -
I have an object that contains a array. On initialization of this object, the
I'm just wondering .. I have and object like this public class Entry{ public

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.