有没有办法指定匿名空枚举类型?
我返回一个 Json 化的匿名类型:
IList<MyClass> listOfStuff = GetListOfStuff();
return Json(
new {
stuff = listOfStuff
}
);
在某些情况下,我知道 listOfStuff
将为空。所以我不想要调用 GetListOfStuff()
(这会进行数据库调用)的开销。
所以在这种情况下我写的是:
return Json(
new {
stuff = new List<ListOfStuff>()
}
);
这似乎有点不必要的冗长。我不在乎它是什么类型的 List
,因为它无论如何都是空的。
是否有可用于表示空可枚举/列表/数组的简写?比如:
return Json(
new {
stuff = new []
}
);
或者我编写 JavaScript 的时间太长了? :)
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? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
本质上你想发出一个空数组。 C# 可以从参数推断数组类型,但对于空数组,您仍然需要指定类型。我想你原来的做法已经足够好了。或者你可以这样做:
数组的类型并不重要,因为任何空的枚举都会转换为 JSON 中的
[]
。我想,为了可读性,请指定空数组的类型。这样,当其他人阅读您的代码时,该成员应该是什么就更明显了。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:
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.在处理匿名类型时,
dynamic
也是一个更好的选择,这对我来说效果很好
dynamic
is also a better option when dealing with an anonymous typethis worked well for me
您可以使用
Enumerable.Empty
更明确一点:尽管它并不短并且没有摆脱类型参数。
You could use
Enumerable.Empty
to be a little more explicit:Although it isn't shorter and doesn't get rid of the type argument.
您可能不关心它是什么类型的列表,但这对调用者来说很重要。 C# 通常不会尝试根据存储变量来推断类型(就像无法在返回类型上创建方法重载一样),因此有必要指定类型。也就是说,如果您想返回空数组,可以使用 new ListOfStuff[0] 。这对于调用者来说(长度)是不可变的(如果他们尝试调用长度可变的
IList
方法,他们会得到一个异常。)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-mutatingIList<T>
methods.)是的,有。您必须定义一个至少包含一个元素的数组,并使用 linq 过滤该数组,不留下任何元素。例子:
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:
发送通用类型?:
或发送空对象数组:
new Object[0]
send a generic type?:
or send an empty object array:
new Object[0]
我猜你在这里谈论的是 C#。我的 C# 知识有限,但我不认为你可以创建一个没有类型的新对象。为什么不能返回通用的 new List[] ? (这里可能混合了 Java 泛型,我不确定是否可以在 C# 中返回泛型类型列表)。
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#).