9

I'm returning a Json'ed annonymous type:

IList<MyClass> listOfStuff = GetListOfStuff();

return Json(
   new {
       stuff = listOfStuff
   }
);

In certain cases, I know that listOfStuff will be empty. So I don't want the overhead of calling GetListOfStuff() (which makes a database call).

So in this case I'm writing:

return Json(
   new {
       stuff = new List<ListOfStuff>()
   }
);

which seems a bit unnecessarily verbose. I don't care what type of List it is, because it's empty anyway.

Is there a shorthand that can be used to signify empty enumerable/list/array? Something like:

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

Or have I been programming JavaScript too long? :)

fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253

7 Answers7

7

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.

Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128
4

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.

Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
4

dynamic is also a better option when dealing with an anonymous type

Enumerable.Empty<dynamic>()

this worked well for me

Uttam Ughareja
  • 842
  • 2
  • 12
  • 21
2

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.)

Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
2

Yes, there is. You have to define an array with as least one element, and use linq to filter the array leaving no elements. Example:

var foo = new
{
    Code = 1,
    Name = "Bar",
    Value = (float?)5.0
};

//use an empty object (or any object) to define the type of the array
var emptyArrayOfFooType = new[] {
    new
    {
        Code = (int)0,
        Name = (string)null,
        Value = (float?)null
    }
}.Where(e => false).ToArray(); //use linq to filter the array leaving no elements

//you can use an existing anonymous type variable too
var anotherEmptyArray = new[] { foo }.Where(e => false).ToArray();

//this array with one element has the same type of the previous two arrays
var fooArray = new[] { foo };

//all arrays have the same type, so you can combine them
var combinedArrays = emptyArrayOfFooType.Concat(anotherEmptyArray).Union(fooArray);
lmcarreiro
  • 5,312
  • 7
  • 36
  • 63
1

send a generic type?:

List<Object>()

or send an empty object array: new Object[0]

bkaid
  • 51,465
  • 22
  • 112
  • 128
Nealv
  • 6,856
  • 8
  • 58
  • 89
-4

I guess you are talking about C# here. My knowledge is limited in C# but I don't think you can create a new object with no type. Why can't you return a generic new List[] ? (might be mixing Java generics here, am not sure is one can return a generic type list in C#).

Gangadhar
  • 1,893
  • 9
  • 9