2

I need to convert a list of string to a list of object, but the thing is I am receiving this list as an object, because it's a parameter and I don't know what type it is.

This is the function that receives the parameter:

public static bool IsNotEmpty(object obj)
{
    if (obj is ICollection)
    {
        IList<object> collection = (IList<object>)obj; // The cast throws error here
        return IsNotEmpty(collection);
    }

    return true;
}

And this is the one this one uses:

public static bool IsNotEmpty<T>(IList<T> aList)
{
    return aList != null && aList.IsNotEmpty();
}

What can I do to cast it as a List of objects, so then I can pass it to the other function? (if there is a way)

tobi
  • 167
  • 1
  • 3
  • 12
  • 2
    Why would you want IsNotEmptyString to take an object instead of a string? – EpicKip Feb 15 '17 at 14:02
  • 2
    IsNotEmptyString is an awfully misleading name for that function – BugFinder Feb 15 '17 at 14:03
  • Sorry, I changed the function so I could display it here! Now it's actually converting to List of string. – tobi Feb 15 '17 at 14:03
  • 1
    Possible duplicate of [Shorter syntax for casting from a List to a List?](http://stackoverflow.com/questions/5115275/shorter-syntax-for-casting-from-a-listx-to-a-listy) – PaulF Feb 15 '17 at 14:04
  • 2
    why don't you use `aList.Count >0`? [It's in the interface.](https://msdn.microsoft.com/en-us/library/system.collections.ilist(v=vs.110).aspx) – Dawnkeeper Feb 15 '17 at 14:05
  • @PaulF I don't think so, because I'm casting from object. – tobi Feb 15 '17 at 14:09
  • FFS, ICollection has a Count. Just check it to be not null and that count is > 0. –  Feb 15 '17 at 14:10
  • I'm not sure about why I'm getting a bunch of down votes for answering the question... But my solution doesn't throw any errors. – Svek Feb 15 '17 at 14:30
  • @Svek it does. It throws the same error I'm facing, sorry. – tobi Feb 15 '17 at 14:31
  • oh... my collection was a List in my test code rather than List... no wonder. – Svek Feb 15 '17 at 14:34
  • @tobi - I updated the answer, although it's not very elegant. It does resolve your casting issue. – Svek Feb 15 '17 at 14:53
  • Can you show some more code regarding the source of your collection to provide greater context? I'd like to understand why you only have an `object` and not some more strongly typed collection. That seems like the real issue. – TheInnerLight Feb 15 '17 at 15:03
  • @tobi: ((List)obj).Cast().ToList() seems to return a List isn't that what you want – PaulF Feb 15 '17 at 15:09
  • @PaulF No, it doesn't. It throws error casting from List to List. I've already solved it with one of the answers given here! – tobi Feb 15 '17 at 15:17
  • Interesting - it works for me. At least you have solved your problem. – PaulF Feb 15 '17 at 15:56

2 Answers2

8

You are doing what we call an "XY question": you have a (wrong) solution in mind for your problem and you're asking about your wrong solution rather than seeking a solution to your actual problem.

You do not need to convert a list of string to a list of object in the first place, so there's no need to ask how to do that. The right solution to your problem is:

static class MyExtensions 
{
  public static bool Any(this IEnumerable sequence) 
  {
     if (sequence == null) 
       throw new ArgumentNullException ... etc ...
     if (sequence is ICollection)
       return ((ICollection)sequence).Any();
     foreach(object item in sequence)
       return true;
     return false;
   }
   public static bool Any(this ICollection collection)
   {
      if (collection == null) blah blah blah
      return collection.Count > 0;
   }
}

Great. Now your method is:

public static bool IsNotEmpty(object obj)
{
  if (obj is IEnumerable)
    return ((IEnumerable)obj).Any();
  else
    return true; 
}

The idea here is to first go to collection because that avoids enumerating the sequence unnecessarily. That can be expensive. But if we have a sequence that has items but is not a collection, then enumerate its first element. If we can do so successfully then it is not empty; if we cannot, then it is empty.

But more generally: the signature of your method is unfortunate. Don't get into the situation where you have an object in hand in the first place if you can possibly avoid it. How did you get into this situation?

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 1
    The last paragraph is I think the most significant. I'd really like to know why the OP has an `object` rather than some typed collection to work with. I suspect that is where this issue should really be being resolved. – TheInnerLight Feb 15 '17 at 14:30
  • If you use as - IsNotEmpty will be shorter – Alexander Egorov Feb 15 '17 at 14:36
5
    public static bool IsNotEmpty(object obj)
    {
        var collection = obj as ICollection;
        return collection == null || collection.Count > 0;
    }
Alexander Egorov
  • 593
  • 10
  • 14