C#IEnumerable< Object>串 [英] C# IEnumerable<Object> to string

查看:85
本文介绍了C#IEnumerable< Object>串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于记录目的,我想在object []数组上调用每个对象的.ToString()方法.如何以最简单的方式做到这一点?

For logging purposes, I would like to call the .ToString() method of every object on an object[] array. How can I do this in the simplest way?

说我有:

   myArray = new Object[]{"astring",1, Customer}

   Log(????);

如何传递一个字符串,例如它的值等于:

How can I pass a string such as its value is equal to:

"astring".ToString()+1.ToString()+Customer.ToString()

或者更好,每个值之间用逗号分隔.

Or better, with comma between each value.

推荐答案

像这样:

Log(String.Join(", ", myArray.Select(o => o.ToString()).ToArray()));


更新:


Update:

从框架4开始,Join方法也可以采用IEnumerable<string>,因此您不需要ToArray:

From framework 4 the Join method can also take an IEnumerable<string>, so you don't need the ToArray:

Log(String.Join(", ", myArray.Select(o => o.ToString())));

这篇关于C#IEnumerable&lt; Object&gt;串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆