Johnathan
Johnathan

Reputation: 23

C# Is there a loop-free way to write an array?

Something like

char[] a = new char[] { 'a', 'b', 'c', 'd' };<br>
Console.WriteLine(a);

works nicely with C#. If the type of the array is integer this does not work any longer. It has to be coded as

for (int k = 0; k < a.Length; k++) Console.Write(a[k]); Console.WriteLine();

This looks rather lame to me. Is there a more succinct way to do so? For example some way which expands WriteLine(a) in a loop-free way to

WriteLine("{0},{1},{2},...,{a.Length-1}", a[0],a[1],a[2],...,a[a.Length-1]);

Perhaps there is some neat Linq trick?

Upvotes: 2

Views: 205

Answers (7)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59111

Confused as to what the OP is asking, since the sample code does two different things. One prints:

123456

The other prints:

1,2,3,4,5,6

If you want the second, use gimel's solution (using string.Join). If you want the first, here's the simplest method, which is easy to read, even for C# newbies, and will work even in C# 1.0:

foreach(object k in a)
    Console.Write(k);

Why over complicate such a simple thing? While this is not "loop-free", it is just as concise as the rest of them. The rest are still loops, they're just hidden by syntax :)

Upvotes: 0

gimel
gimel

Reputation: 86362

How about String.Join ? Beginning with .Net Framework 4, the second argument is an object[]:

String.Join Method (String, Object[])

Concatenates the elements of an object array, using the specified separator between each element.

Trying a snippet in Visual Studio 2010, targeting framework 4:

    int[] a = new int[] {1,2,3,4,5,6,7 };
    Console.WriteLine(String.Join(",", a));

Produces:

1,2,3,4,5,6,7

Upvotes: 10

Matthew Whited
Matthew Whited

Reputation: 22443

The .Aggregate<TSource, TAccumulate>(source, func) is your friend in this case. You can seed it with a StringBuilder and then just chain Appends from there. Most of the member methods on the StringBuilder class return an instance to the StringBuilder you passed in making it great to use in these types of scenarios.

Sample...

var charArray = new[] { 'a', 'b', 'c', 'd' };
var objectArray = new object[] { 'a', "Hello", 1, null };

Console.WriteLine(new string(charArray));
Console.WriteLine(objectArray.Aggregate(
    new StringBuilder(), 
    (sb,v)=>sb.Append(v+" ")));

Result ...

abcd
a Hello 1

Upvotes: 1

jeroenh
jeroenh

Reputation: 26782

Enumerable.Range(0, 100).ToList().ForEach(Console.Write); Console.WriteLine();

The ForEach() method is specific for List<T>, but you can create your own extension method on IEnumerable<T> for this, if you want:

public static class MyExtensions
{
    public static void ForEach<T>(this IEnumerable<T> list, Action<T> action)
    {
       foreach (var item in list) action(item);
    } 
}

Now you can do this:

 Enumerable.Range(0, 100).ForEach(Console.Write);

Upvotes: 0

theburningmonk
theburningmonk

Reputation: 16051

You can use Enumerable.Select and Aggregate

var intArray = new[] { 1, 2, 3, 4 };

Console.WriteLine(intArray.Select(i => i.ToString())
                          .Aggregate((acc, item) => acc + "\n" + item));

This outputs:

1   
2    
3    
4

Upvotes: 0

mike nelson
mike nelson

Reputation: 22136

I think you can use something like:

a.ForEach(e=>WriteLine(e))

Upvotes: 0

ankitjaininfo
ankitjaininfo

Reputation: 12362

it works because of ToString() method. After all there is a loop inside toString() ! For any of your custom classes you can override toString method.

Upvotes: 0

Related Questions