Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the last object added to a HashSet?

Tags:

c#

hashset

I have a HashSet that is a collection of client sockets. When a client connects, the socket is added to the HashSet. I then need to access that socket, but I don't know how to access it in the HashSet.

... 
clientSockets.Add(listenerSocket.EndAccept(async));
WaitForData(lastAddedSocket);
....

How could I determine what lastAddedSocket is?

like image 830
Booers Avatar asked Jan 27 '26 20:01

Booers


1 Answers

There is no way to ask a HashSet "what was the last thing that was added to you?" The order is all jumbled up.

You should simply keep a separate variable called lastAddedSocket. Every time you add a socket to the HashSet, also assign it to lastAddedSocket. Then you will be able to look it up easily and in constant time.

like image 132
mgiuca Avatar answered Jan 29 '26 08:01

mgiuca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!