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
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
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" };
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");
}
}
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
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
}
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;
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;
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