Friday 15 March 2013

c# - How to create IEnumerable.ToString -


If I have a IEnumerable , then this is the perfect way to create a toasting method Does the value comma delimit?

  var enumOfObjects = myEnumerable as IList & lt; Object & gt; ?? MyEnumerable.Cast & LT; Object & gt; () ToList () .; Var enumOfStrings = enumOfObjects.Select (x = & gt; x.ToString ()); Var final string = string .joined (",", enmofstringings); Or, what a foreach loop would be better? Or is there a cleaner way of doing this?   

The following is small (and can be considered cleaner):

  EnumOfObjects.Aggregate ((a, b) => a + "," + b))   

Something looks like this:

  IEnumerable & Lt; Object & gt; EnumOfObjects = New list & lt; Object & gt; {1, "cat", 3, 4}; Console.lightline (enumOfObjects.Agregate ((a, b) = & gt; a + "," + b)); // Output 1, Cat, 3,4   

If you really want to use it as a string, you can call it to ToString () Or to put it to string.

No comments:

Post a Comment