方法:列表和对象哪个更好?

发布于 2024-09-27 17:42:00 字数 471 浏览 1 评论 0原文

当我编程时,我提出了这个问题,

让一个方法接受单个实体或这些实体的列表,哪个更好?

例如我需要一个字符串列表。我可以有:

  • 一个接受列表并返回带有结果的字符串列表的方法。

    列表<字符串>;结果 = methodwithlist(List[objects]); 
    

  • 接受对象并返回字符串的方法。然后在循环中使用此函数并填充列表。

    for int i = 0;我<列表<对象>.Count;i++;)
    {
       结果 = methodwithsingleobject(List[i]);
    }
    

** 这只是一个例子。我需要知道哪一个更好,或更常用以及为什么。

谢谢!

While I was programming I came up with this question,

What is better, having a method accept a single entity or a List of those entity's?

For example I need a List of strings. I can either have:

  • a method accepting a List and return a List of strings with the results.

    List<string> results = methodwithlist(List[objects]); 
    

or

  • a method accepting a object and return a string. Then use this function in a loop and so filling a list.

    for int i = 0; i < List<objects>.Count;i++;)
    {
       results = methodwithsingleobject(List<objects>[i]);
    }
    

** This is just a example. I need to know which one is better, or more used and why.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不知在何时 2024-10-04 17:42:00

嗯,当您有了第二个表单后,构建第一个表单就很容易了,但是使用 LINQ,一旦获得了投影,您实际上不需要编写自己的表单。例如,您可以这样写:

List<string> results = objectList.Select(X => MethodWithSingleObject()).ToList();

一般来说,编写和测试只处理单个值的方法会更容易,除非它实际上需要知道集合中的其余值(例如查找聚合) )。

Well, it's easy to build the first form when you've got the second - but using LINQ, you really don't need to write your own, once you've got the projection. For example, you could write:

List<string> results = objectList.Select(X => MethodWithSingleObject()).ToList();

Generally it's easier to write and test a method which only deals with a single value, unless it actually needs to know the rest of the values in the collection (e.g. to find aggregates).

紅太極 2024-10-04 17:42:00

我会选择第二个,因为当你有一个字符串时它更容易使用(即它更通用)。此外,该方法本身的职责更加明确,因为如果该方法的目的只是修改字符串,则该方法不应该与列表有任何关系。

此外,您还可以使用 Linq 简化调用:

result = yourList.Select(p => methodwithsingleobject(p));

I would choose the second because it's easier to use when you have a single string (i.e. it's more general purpose). Also, the responsibility of the method itself is more clear because the method should not have anything to do with lists if it's purpose is just to modify a string.

Also, you can simplify the call with Linq:

result = yourList.Select(p => methodwithsingleobject(p));
素年丶 2024-10-04 17:42:00

在学习任何语言时都会经常出现这个问题,答案有些没有实际意义,因为标准编码实践是依靠 LINQ 在运行时优化代码。但这假设您使用的是支持它的语言版本。但是,如果您确实想对此进行一些研究,有一些 Stack Overflow 文章深入研究了这一点,并提供了可供查看的外部资源:

  1. 在 .NET 中,“for”或“foreach”哪个循环运行得更快?
  2. ​​C#、For 循环和速度测试...完全相同的循环第二次更快?

不过,我学到的是不要过分依赖 Count 并在类型化集合上使用 Length,因为这样会快得多。

希望这有帮助。

This question comes up a lot when learning any language, the answer is somewhat moot since the standard coding practice is to rely upon LINQ to optimize the code for you at runtime. But this presumes you're using a version of the language that supports it. But if you do want to do some research on this there are a few Stack Overflow articles that delve into this and also give external resources to review:

  1. In .NET, which loop runs faster, 'for' or 'foreach'?
  2. C#, For Loops, and speed test... Exact same loop faster second time around?

What I have learned, though, is not to rely too heavily on Count and to use Length on typed Collections as that can be a lot faster.

Hope this is helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文