tags:

views:

72

answers:

4

hey there I want to create a stack which will have the following methods:

getValueOf ("key")
getKeyOf ("value")
addKeyAndValue ("key" "value")
delete ("key")

can anyone please help me as I am very new to C sharp

+1  A: 

Did you look at the Dictionary class?
That's no stack, but the methods you describe are not typical for a stack, either.

getValueOf( "key" ):

var value = dictionary[key];

addKeyAndValue( "key" "value" ):

dictionary.Add( key, value );

delete( "key" )

dictionary.Remove( key );

getKeyOf ("value") with linq:

var key = dictionary.Where( pair => pair.Value == 10 )
                    .Select( pair => pair.Key ).FirstOrDefault();
tanascius
thanks I got it
ghostantanieh
+1  A: 

Are you sure you need a stack? What you are describing sounds very much like a Dictionary collection.

A stack is a very specific data structure and would not normally allow you to search by keys of values - it is a "first in last out" data structure that would not have the methods you want.

A dictionary on the other hand has these search capabilities and has methods on it that are pretty close to what you are describing.

Oded
yeah this is one of my hw questions which had asked me to implement such stack please help me I'm ruined as I got no idea
ghostantanieh
@ghostantanieh - You can wrap your class around a dictionary, but without knowing more about the constraints you have (are you allowed to use existing collection types? What are you not allowed to do?) it is difficult to give you a good answer.
Oded
+2  A: 

You need to check Dictionary class or if you want as you said "priority" (sorted) you need to check SortedList Class.

EDIT : If you need to implement the stack by your own. You need to check ICollection, IEnumerable and ICloneable interfaces.

However better to use already exiting classes in Framework if they fit your needs. And based on your description seems you need Dictionary or SortedList.

Incognito
However, Priority would have to be "key" (and unique).
František Žiačik
A: 

There is a stack class in C#, Here is a quick guide to using it:

http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=65

Hope that helps.

Steve