Is there a way to specify an anonymous empty enumerable type?

Essentially you want to emit an empty array. C# can infer the array type from the arguments, but for empty arrays, you still have to specify type. I guess your original way of doing it is good enough. Or you could do this:

return Json(
   new {
       stuff = new ListOfStuff[]{}
   }
);

The type of the array does not really matter as any empty enumerable will translate into [] in JSON. I guess, for readability sake, do specify the type of the empty array. This way when others read your code, it's more obvious what that member is supposed to be.


You could use Enumerable.Empty to be a little more explicit:

return Json(
    new {
        stuff = Enumerable.Empty<ListOfStuff>()
    }
);

Although it isn't shorter and doesn't get rid of the type argument.


You might not care what type of list it is, but it matters to the caller. C# does not generally try to infer types based on the variable to which it is being stored (just as you can't create overloads of methods on return type), so it's necessary to specify the type. That said, you can use new ListOfStuff[0] if you want an empty array returned. This has the effect of being immutable (in length) to the caller (they'll get an exception if they try to call the length-mutating IList<T> methods.)