Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Stack has a reverse method - which does not seem to work
Message
De
23/06/2009 11:16:50
 
 
À
23/06/2009 10:56:45
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01407785
Message ID:
01407804
Vues:
35
>>To reverse a stack, simply create another stack
>>
>>		static void TestStack2()
>>		{
>>			var xx = new Stack<int>();
>>			xx.Push(3);
>>			xx.Push(2);
>>			xx.Push(1);
>>
>>			var yy = new Stack<int>(xx);
>>
>>			Console.WriteLine("Stack2");
>>
>>			while (yy.Count != 0)
>>				Console.Write("{0} ", yy.Pop());
>>			Console.WriteLine();
>>		}
>>
>
>I am curious about why it didn't work as stack.reverse. Unlike the list method reverse on a stack is an extension method but if the extension was in scope it seems it should work. Did you figure that out? Just curious
>Tim

Tim,

I see that for a List[T], there is both a class method Reverse() and an extension method Reverse()

For a Stack[T], there's only an extension method

Ok - the extension method you mentioned rings a bell - it returns IEnumerable[T] in reverse order and hence does not alter the stack
-		yy	{System.Linq.Enumerable.ReverseIterator<int>}	System.Collections.Generic.IEnumerable<int> {System.Linq.Enumerable.ReverseIterator<int>}
eg
		static void TestStack3()
		{
			var xx = new Stack<int>();
			xx.Push(3);
			xx.Push(2);
			xx.Push(1);

			var yy = xx.Reverse();

	
			//xx.Reverse(); // should get 3, 2, 1 but still get 1,2,3

			Console.WriteLine("Stack3 ");

			foreach( var v in yy)
				Console.Write("{0} ", v);
			Console.WriteLine();
		}
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform