generally, to use dictionary type in .net.
Dictionary<string, int> anyDic;
in above, If I want to define the key and value's meaning,
Meaning to whom?
How can I do that?
in my short thinking,
Dictionary<string name, int studentIdNumber> anyDic;
but the above arise compile error?
Yes, it would.
Is any idea about this? not just comment.
There's nothing in the language that gives you that degree of
descriptiveness. If there were, it would be highly inconsistent with the
use of types everywhere else in the language (or other similar languages
for that matter).
To some extent, you can address the issue through variable naming, which
is the standard way to present usage information in code (i.e. _not_
writing that information as part of the type itself). I personally like
the Hungarian naming convention (the true original one, not the
basterdized "Systems Hungarian" most people are familiar with though
Microsoft's Win32 API), and it addresses this question by defining a "map"
naming format: "mp<from><to>" where "<from>" and "<to>" are the type tags
used for the map.
Of course, for that to apply, you need to be using Hungarian elsewhere and
have appropriate type tags for the types used in the dictionary. But,
supposing you did, and supposing you were using the tag "name" for the
names and "sid" for the student ID number, then the variable name would be
"mpnamesid", or possibly "mpnamesid<Qualifier>" where "<Qualifier>" is
some capitalized word that further describes the mapping (useful mainly if
you have multiple such dictionaries). (*)
Even in absence of Hungarian though, you can a lot better than "anyDic"
for a variable name. So do so. You should be naming your variables in
ways that usefully convey the purpose and usage of the variable, and if
you do that, you wouldn't need to describe the key or value in the type of
the variable declaration.
Pete
(*) I actually struggle with this aspect of the Hungarian convention,
because of the awkwardness of the construction. In the olden days, this
kind of data mapping didn't come up very often, and when it did it was
usually an indexed array rather than a hash-based structure. But
hash-based structures are much more feasible now that the hardware is so
fast and memory so plentiful, and of course you have built-in dictionary
structures in pretty much every mainstream programming environment now.
When one was using this aspect of naming infrequently, the fact that it's
awkward wasn't such a big deal. But IMHO it does get a bit out of hand
when you have a lot of dictionaries running around in the code. On the
one hand, at least if you're using VS's Intellisense, it's not really as
much extra typing as it might seem. But the variables names are still
lengthy and I've never really been happy with the way that the tags run
into each other without any easy-to-read delimiter.
There's a lot about Hungarian I like and I find it extremely valuable as a
naming convention. But it does have some warts and IMHO this is one of
them (the "mp" tag). I've yet to decide on an alternative I like better,
but in the meantime I admit that I often wind up describing the dictionary
in only a pseudo-Hungarian way, using the tag "dict" and then providing
some qualifiers that more-completely describe the usage of the object.
Anyway, that's a long way of saying that if you don't find this use-case
sufficiently compelling enough a reason to switch your convention over to
Hungarian, I wouldn't be surprised. But if you're already using
Hungarian, it's something to consider. At the very least, whether you're
using Hungarian or not, take the underlying motivation to heart and just
use some variable naming convention to incorporate the information you're
trying to present.