-3

I am trying to get the count value of my list witch is in type object. This is fictive code for demonstrating what i am working with

object test = new List<string>() { "test1", "test2" };

This is what i am trying to achive

test.count
Tobias Møller
  • 64
  • 1
  • 1
  • 8

7 Answers7

2

You can't fetch a property that isn't known to the type of your variable. You should cast it to the right type:

List<string> test = new List<string>() { "test1", "test2" };

int count = test.Count;

If you feel like it, you can use var, which will translate to the exact same as above:

var /*actually List<string>*/ test = new List<string>() { "test1", "test2" };
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
2

At the moment, this is the only solution that does what the author needs without any additional information. Therefore, it is the most universal method that works with object and does not require any other information. All arrays and collections, including List, implement the IEnumerable interface, so you can use foreach to iterate over any collection.

private static int GetCount(this object val)
{
    if (val is IEnumerable enumerable)
    {
        int count = 0;
        foreach (var item in enumerable)
        {
            count++;
        }
        return count;
    }
    else
    {
        throw new ArgumentException("val is not a valid collection type");
    }
}
0

Unless it's a must for your test to be of object type (which I don't see why it need to be in the question).

Changing

object test = new List<string>() {"test1","test2"};

to

List<string> test = new List <string>() {"test1","test2"};

will allow you to use the count method

Stasis
  • 116
  • 7
  • 1
    `Count` is a property. – Patrick Hofman May 07 '18 at 07:22
  • oh yea. Got it confused with Count() – Stasis May 07 '18 at 07:24
  • @PatrickHofman well true, but also properties are technically method + field. – SᴇM May 07 '18 at 07:24
  • 1
    No, properties are properties. That they translate to something else in CIL doesn't matter for C#. – Patrick Hofman May 07 '18 at 07:25
  • @PatrickHofman it will matter in c#, because the methods with the names `int get_Name()/void set_Name(type)` will be reserved by your class and you will not be able to use those names. – SᴇM May 07 '18 at 07:39
  • @Stasis There 1 `Count` property and two extension methods `Count` and `LongCount` which will return the number of the elements contained in the `List`. `Count()` method internally checks if your `IEnumerable` implements `ICollection` and if it does it uses the `Count` property. – SᴇM May 07 '18 at 07:49
0

If you know that your list is string type then you should use string type list instead of object. like below

List<string> lst = new List<string>() { "test1", "test2" };
if (lst.Count()  > 0)
{ 
//Do some thing here
}
Azeem Hafeez
  • 250
  • 4
  • 14
0

You can use following code to get count of object. You can achieve this using Reflection.

object test = new List<string>() { "test1", "test2" };

var property = typeof(ICollection).GetProperty("Count");
int count = (int)property.GetValue(test, null);

NOTE:

Reflection provides facility to metaprogramming. The benefit of using Reflection in SPECIAL cases is it gives developers a power to access Assembly Runtime regardless of any types. I suggest to use Reflection in special scenarios only.

Proper way is to cast object to type

List<string> test = new List<string>() { "test1", "test2" };

int count = test.Count;
Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
  • 2
    Using reflection is serious overkill here, very error prone and hard to understand what is the objective. Just give the variable the right type. This answer is bad advice. – Patrick Hofman May 07 '18 at 07:30
0

Cast object to a particular type while using.

object test = new List<string>() { "test1", "test2" };
int count = ((List<string>) test).Count;

OR
You can simply use var to initialize the list for avoiding casting everytime of usage.

var test = new List<string>() { "test1", "test2" };
int count = test.Count;

OR
You can initialize with a particular type.

 List<string> test = new List<string>() { "test1", "test2" };
 int count = test.Count;
Monis Azhar
  • 1
  • 1
  • 3
-3

It will always be the type object when i get it back from my method. I found a solution

List<string> tt = (List<string>)test;
myCountValue = tt.Count
SᴇM
  • 7,024
  • 3
  • 24
  • 41
Tobias Møller
  • 64
  • 1
  • 1
  • 8