Sign in to follow this  
xsirxx

[.net] Hashtable, what is a reference and what isnt?

Recommended Posts

xsirxx    170
ok so I wrote an engine in C++, now I am trying to rewrite a new engine in C# while learning the language. So I came across Hashtables as a good way to do a type of what I used to do with std::maps. So my question is, when I do this: Hashtable EventListenerHTable = new Hashtable(); ArrayList alGet = (ArrayList)EventListenerHTable[ in_iType ]; //int,ArrayList alGet.Add( in_Listener ); does this add in_listener to the EventListenerHTable[in_iType]'s ArrayList? or does alGet become a new reference now and take up twice the space? meaning how can I access EventListenerHTable[in_iType] directly but not have to keep typing in that whole EventListenerHTable[in_iType] everytime? I used to use pointers in C++... So is alGet a reference or is it a new object? Thanks much hope I was clear, Brad (also this might belong in the beginners section, wasnt sure since a C# section was defined)

Share this post


Link to post
Share on other sites
Agony    3452
All classes (as opposed to structs) are always passed by/declared as reference in C#, so despite alGet's declaration looking like an individual instance of an ArrayList to a C++ programmer (along with the copying that is implied), it is actually simply a reference to an ArrayList, more similar to a C++ smart pointer. So no copying is done. When using classes, you can be sure that no copying is done unless you see something explicit like a call to Clone(). (At least as far as I am aware at the moment. I'm not an expert C# programmer, and sometimes my memory is fuzzy. The general idea is definitely valid, though.) A lot of stuff that contrasts C# classes and structs would probably be useful reading, as they'll generally cover when things are treated as a reference, and when they're treated as a value.

Share this post


Link to post
Share on other sites
xsirxx    170
ahh well thats good news and bad news i guess. Not to waste much more of your time here, but how would setting something equal to something else work? Does what you said only apply to anything created with the new operator?

for instance would these mean the same thing in theory?:

------------------------
int i = 0;
int p = 4;
p = i;
i = 5
------------------------p is == 0 or 5?
int i = 0;
int p = i;
i = 5
------------------------p is == 0 or 5?
int i = new 0;
int p = 4;
p = i;
i = 5;
------------------------p is == 0 or 5?

Thanks again VERY VERY much for your reply, btw I picked up A Programmer's Introduction to C# 2.0. Unfortunatly alot of over coverage and I skimmed but couldnt find anywhere that explained in depth about using "=" with and without "new".

Thanks much!
--Brad

Share this post


Link to post
Share on other sites
Nypyren    12063
ints are Value types, so when you assign them as you've written, it's not a reference.

int i = 0;
int p = 5;
i = p; // this assigns the value of 5 to i
p = 6;
// i is still 5.

Share this post


Link to post
Share on other sites
capn_midnight    1707
You may want to check out System.Collections.Generic.Dictionary<T, U>. It's a generic version of the Hashtable, so you won't have to do all of that casting back and forth.

One thing to remember, when using value types (ints, or anything declared as struct) with a collection, the value type gets boxed in a reference type before being stored in the collection. With Hashtable/Dictionary, this applies to the hash key as well.

Share this post


Link to post
Share on other sites
xsirxx    170
right about the ints, I was just using them as an example of some type we all knew, of course a built-in type was just a bad idea because it is never even put on the heap is it? So imagine we were using object I suspose instead of int, what would the outcomes be?

Also I knew about that with boxing so I have been careful with not using structs as class types...which in C++ I could lay back on.... I am DEFINITLY going to lookup the Dictionary because with the Hashtable I dont want ppl throwing things into it that I dont want belonging there, but I couldnt figure a way of locking it down to 1 type. Also the casting would be quite slow after awhile (Id imagine) unless C# handles it differently...

Thanks very much every1,
--Brad

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this