-3

In debug mode, I'm hovering over my variable, and it shows that it is {object[0]}:

enter image description here

However, this IF statement is never getting triggered.

How do I check for this object[0] type?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • `object[0]` could mean its an empty collection its hard to say. where does this object come from? – Daniel A. White Jan 08 '17 at 02:05
  • What does element.GetType().FullName return in this case? – RQDQ Jan 08 '17 at 02:07
  • You if statement doesn't seem to make much sense. Could you please update the answer an explain why you're using this check? – Soviut Jan 08 '17 at 02:08
  • 1
    Or provide some more code for context. – Soviut Jan 08 '17 at 02:08
  • An array is an `IEnumerable`, so maybe you could change to `element is IEnumerable && ...` – BrunoLM Jan 08 '17 at 02:12
  • Your screenshot shows that while you're inspecting `element`'s value, you're not inspecting it when the execution is on the `if` condition. You also don't show what `ToEnumerable()` does, so there's no telling what that returns. It wouldn't surprise me if it's a collection containing `element`. –  Jan 08 '17 at 12:05
  • And if you happen to be using [Telerik's `CollectionExtensions`](http://docs.telerik.com/devtools/wpf/api/html/m_telerik_windows_controls_collectionextensions_toenumerable__1.htm), that's *exactly* what its `ToEnumerable` does. It always returns an enumerable containing exactly one item, so `!element.ToEnumerable().Any()` will always be false, regardless of the value of `element`. –  Jan 08 '17 at 12:15

3 Answers3

4

To check that an object is a object[], your check element is object[] is already correct.

To check that an object[] is empty, calling Any() is already correct, but make sure to call it on the right instance. Avoid that ToEnumerable() extension method, since it's not doing what you're hoping for.

if (element is object[] && !((object[]) element).Any())
    // element is an empty array of objects

Test:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Tester {
    static class Program {
        static void Test(string name, object element) {
            Console.Write($"{name}: ");
            Console.WriteLine(element is object[] && !((object[])element).Any());
        }

        static void Main(string[] args) {
            Test("new object()",       new object());       // false
            Test("new { }",            new { });            // false
            Test("new object[0]",      new object[0]);      // true
            Test("new object[1]",      new object[1]);      // false

            Test("new List<object>()", new List<object>()); // false
            // Note: object[] o = new List<object>(); wouldn't be allowed.

            Test("new string[0]",      new string[0]);      // true
            // Note: object[] o = new string[0]; would be allowed.

            Test("new int[0]",         new int[0]);         // false
            // Note: object[] o = new int[0]; wouldn't be allowed.
        }
    }
}

This includes some test cases that could be an indication that the check you're attempting to do isn't a good idea, but it also indicates that it gives accurate results.

1

There is no object[0] type. What it's telling you is that it's an empty array of objects.

Soviut
  • 88,194
  • 49
  • 192
  • 260
1

Have you tried:

var test = element as IEnumerable;

If (test != null && test.Any())
{
RQDQ
  • 15,461
  • 2
  • 32
  • 59
  • i have, and it looks like if element is a Guid then it is an IEnumerable – Alex Gordon Jan 08 '17 at 11:47
  • @l--''''''---------'''''''''''' No, [`Guid`](https://msdn.microsoft.com/en-us/library/system.guid(v=vs.110).aspx) doesn't implement `IEnumerable`. Maybe you're looking at a [`string`](https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx) which happens to represent a Guid? `string` implements `IEnumerable`. –  Jan 08 '17 at 11:59