Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string[] arrStr = new string[4] { "A","C","D","E"};

//----------------FOR-------------------------------------
for (int i = arrStr.Length - 1; i >= 0; i--)
{
    Console.WriteLine(arrStr[i]);
}
//-------------STACK--------------------------------------
Stack<string> stack = new Stack<string>(arrStr);
foreach (var item in stack)
{
    Console.WriteLine(item);
}


Could you guys please tell me the difference between these approaches ?
Which one would you prefer to and which one give you the most performance as the pros and cons.

Any answer would be appreciated.
Posted

Functionally there is no difference in them, they both do the same thing. They process the array in reverse order.

You could even do this:

C#
arrStr.Reverse().ForEach(s => Console.WriteLine(s));
arrStr.Reverse().ToList().ForEach(s => Console.WriteLine(s));


and get the same effect again.

As far as performance goes, the bare for loop is probably going to be the fastest. Then you will have to benchmark to determine which is faster, the Linq expression or the stack.

I'm assuming that this is just a boiled down version of something you are doing that is much more complicated, in its current form if you just wanted to print a string array in reverse, I wouldn't be worried about which is faster than the other, its the Console.Writeline call that is slow. In this case, it would be quicker to use a StringBuilder and append a line, then at the end write out the string builder to the console.
 
Share this answer
 
v2
Hi,

Actually For would be faster when you run it on an array than stack but my concern is you use array when you know the length, but where you don't know the length then you use stack.

Now if you want to reverse the array or stack then performance would quite same.
 
Share this answer
 
Comments
Benjamin Nguyễn Đạt 21-Jan-14 1:40am    
you could always know the array's length by its property array.Length
Suvabrata Roy 21-Jan-14 3:00am    
you did not get my point, my point was you use array when you know length of that at the time of initialization but you can use stack for dynamic purpose.
Refer below link which is detailed about for and foreach:-
FOREACH Vs. FOR (C#)[^]
Difference between for and foreach loop[^]

foreach[^] and for[^]
 
Share this answer
 
I think you are comparing "apples to oranges:" you use a Stack when you want/need to take advantage of its LIFO features, when the context of your work calls for this kind of data structure. And, when you use a Stack, you give up indexed access [1]; however, the fact that a Stack is IEnumerable means you've got the rich vocabulary of Linq to work with.

An Array is a "lower-level" creature in some ways, but offers speed in many scenarios, as well as, of course, indexed access.

Choose your weapon depending on what you are fighting :)

[1] If you think you get indexed access to a Stack via Linq ElementAt(#) for cheap: wrong; under the hood it's enumerating via a temporary Array (so I read).
 
Share this answer
 
Comments
Benjamin Nguyễn Đạt 21-Jan-14 1:41am    
hi there ! You're rock ! thanks for that!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900