Why didn't someone tell me C# has typedef?

by Matt 19. July 2007 04:42

What do you do if you want a mapping between a string and a guid?

I reached for Dictionary<string, guid>. Job done. Strongly typed goodness.

But it's a bit ugly, isn't it? I need to use the generic parameters everywhere I want to use the type, so if I suddenly decide I want to use a structure instead of a guid, there's a lot of places to change.

If I want to abstract my code away from all of this implementation detail, I can create a derived class:

class SectionMapping : Dictionary<string, guid>
{
}

Which feels a bit klunky - empty classes are not the nicest of things.

So today's bright idea was to use a nice little trick with using:

using CategoryMapping = System.Collections.Generics.Dictionary<string, System.Guid>

And I can now use CategoryMapping wherever I had been using the Dictionary before.

The downside is that this is not a new type, like the SectionMapping class is - it's just an alias. In other words, I can use CategoryMapping and Dictionary<string, guid> interchangeably. If I create another "using" alias for, say, GroupMapping, then I can use an instance of CategoryMapping wherever I use GroupMapping. Contrast this with classes. If I define CategoryMapping and GroupMapping as empty classes that derive from Dictionary<string, guid>, they have the same interface, but the compiler views them as very different types.

Oh and it's only scoped to the C# file in which it's declared.

Tags:

Comments (19) -

suv review
suv review
7/20/2011 10:34:09 PM #

I wonder if he cheated on her? I remember he cheated on his previous wife with JLO so it wouldn’t be surprising.

Reply

best hybrid cars
best hybrid cars
7/22/2011 4:20:49 AM #

Una pagina sarà dedicata agli accessori, una alle giacche e ai giubbotti. Troverai le indicazioni per lo spaccio o negozio Moncler più vicino a casa tua e tutte le offerte più vantaggiose di questo prestigioso marchio.

Reply

iphone 4 g
iphone 4 g
7/23/2011 8:10:01 PM #

Una pagina sarà dedicata agli accessori, una alle giacche e ai giubbotti. Troverai le indicazioni per lo spaccio o negozio Moncler più vicino a casa tua e tutte le offerte più vantaggiose di questo prestigioso marchio.

Reply

philix
philix United States
11/20/2015 10:53:25 AM #

Great Site.

Reply

Philix
Philix United States
11/20/2015 12:41:24 PM #

nice site

Reply

Philix
Philix United States
11/20/2015 9:02:10 PM #

Superb Website.

Reply

Philix
Philix United States
11/21/2015 3:18:50 PM #

Good Site.

Reply

philix
philix United States
11/22/2015 1:16:58 AM #

Superb Site

Reply

philix
philix United States
11/22/2015 10:40:11 AM #

Ace Web Site.

Reply

philix
philix United States
11/22/2015 8:04:20 PM #

great site

Reply

philix
philix United States
11/27/2015 3:10:52 PM #

Good Website.

Reply

Marianne Babers
Marianne Babers United States
1/6/2016 1:49:00 PM #

Congratulations on your success! You deserve it for brightening all of my work days with something to be excited about.

Reply

Rich Bowdry
Rich Bowdry United States
1/15/2016 4:22:57 AM #

Printing out an essay or paper, reading the first line and realizing there aren't any mistakes.<br />AWESOME!

Reply

Charlie Mallat
Charlie Mallat United States
1/15/2016 5:45:57 AM #

Let me be the first to say: Congrats, Neil! I've been following this site from day one, and have loved every single day of awesomeness that it has delivered. You deserve all the success coming your way. Keep it up!AWESOME!

Reply

Raleigh Maarx
Raleigh Maarx United States
1/15/2016 6:58:45 AM #

Let me be the first to say: Congrats, Neil! I've been following this site from day one, and have loved every single day of awesomeness that it has delivered. You deserve all the success coming your way. Keep it up!AWESOME!

Reply

Jerald Petula
Jerald Petula United States
1/19/2016 2:25:51 AM #

I can't wait to read this masterpiece. Ordering off the menu at fast food restaurants and old dangerous playground equipment have to be in the book!

Reply

Jinny Mccleese
Jinny Mccleese United States
2/13/2016 2:54:30 AM #

muse.union.edu/.../

Reply

seo backlinks
seo backlinks United States
5/10/2016 11:28:45 PM #

Wow! Merely remarkable! Thanks so much for the details discussed! I understand you have placed a great deal of effort into this and also I wished to inform you exactly how grateful I am! There have to be a lot more blog sites similar to this on the internet! I will definitely bookmark and also subscribe to your exceptional site! Hope you put out much more spectacular stuff in the near future and also I will certainly come back as well as read it! Keep up the magnum opus!

Reply

Bert Kilarjian
Bert Kilarjian United States
5/12/2016 4:41:38 AM #

This is so great post! Very well written and very informative! I will recommend your site to my friends!

Reply

Add comment

biuquote
  • Comment
  • Preview
Loading

Rel=Me

Month List

RecentComments

Comment RSS