连接对象的字符串表示的好方法?
好的,
我们的代码中有很多 where 子句。我们有多种方法来生成表示 in 条件的字符串。我试图想出一种干净的方法,如下所示:
public static string Join<T>(this IEnumerable<T> items, string separator)
{
var strings = from item in items select item.ToString();
return string.Join(separator, strings.ToArray());
}
它可以按如下方式使用:
var values = new []{1, 2, 3, 4, 5, 6};
values.StringJoin(",");
// result should be:
// "1,2,3,4,5,6"
所以这是一个很好的扩展方法,可以完成非常基本的工作。我知道简单的代码并不总是能够快速或高效地执行,但我只是好奇这个简单的代码可能会错过什么。我们团队的其他成员认为:
- 它不够灵活(无法控制字符串表示)
- 可能内存效率不高
- 可能速度不快
有专家插话吗?
问候,
埃里克。
Ok,
We have a lot of where clauses in our code. We have just as many ways to generate a string to represent the in condition. I am trying to come up with a clean way as follows:
public static string Join<T>(this IEnumerable<T> items, string separator)
{
var strings = from item in items select item.ToString();
return string.Join(separator, strings.ToArray());
}
it can be used as follows:
var values = new []{1, 2, 3, 4, 5, 6};
values.StringJoin(",");
// result should be:
// "1,2,3,4,5,6"
So this is a nice extension method that does a very basic job. I know that simple code does not always turn into fast or efficient execution, but I am just curious as to what could I have missed with this simple code. Other members of our team are arguing that:
- it is not flexible enough (no control of the string representation)
- may not be memory efficient
- may not be fast
Any expert to chime in?
Regards,
Eric.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关于第一个问题,您可以添加另一个“formatter”参数来控制每个项目到字符串的转换:
关于后两个问题,我不会担心它,除非您后来遇到性能问题并发现它是一个问题。然而,这不太可能成为瓶颈......
Regarding the first issue, you could add another 'formatter' parameter to control the conversion of each item into a string:
Regarding the second two issues, I wouldn't worry about it unless you later run into performance issues and find it to be a problem. It's unlikely to much of a bottleneck however...
出于某种原因,我认为
String.Join
是根据StringBuilder
类实现的。但如果不是,那么以下代码可能对于大型输入表现更好,因为它不会为迭代中的每个连接重新创建String
对象。编辑:这是分析何时适合使用
StringBuilder
和String.Join
。For some reason, I thought that
String.Join
is implemented in terms of aStringBuilder
class. But if it isn't, then the following is likely to perform better for large inputs since it doesn't recreate aString
object for each join in the iteration.EDIT: Here is an analysis of when it is appropriate to use
StringBuilder
andString.Join
.为什么不使用 StringBuilder,并自己迭代集合并追加。
否则,您将创建一个字符串数组(var strings),然后进行连接。
Why don't you use StringBuilder, and iterate through the collection yourself, appending.
Otherwise you are creating an array of strings (var strings) and then doing the Join.
您缺少对序列和序列项的空检查。是的,这不是最快和最有效的内存方式。人们可能只是枚举序列并将项目的字符串表示形式呈现到
StringBuilder
中。但这真的重要吗?您遇到性能问题吗?需要优化吗?You are missing null checks for the sequence and the items of the sequence. And yes, it is not the fastest and most memory efficient way. One would probably just enumerate the sequence and render the string representations of the items into a
StringBuilder
. But does this really matter? Are you experiencing performance problems? Do you need to optimize?这也可以工作:
this would work also: