Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the c# compiler create a PrivateImplementationDetails from this code?

Tags:

c#

I've discovered that the following code:

public static class MimeHelper
    {
        public static string GetMimeType(string strFileName)
        {
            string retval;
            switch (System.IO.Path.GetExtension(strFileName).ToLower())
            {
                case ".3dm": retval = "x-world/x-3dmf"; break;
                case ".3dmf": retval = "x-world/x-3dmf"; break;
                case ".a": retval = "application/octet-stream"; break;
                // etc...
                default: retval = "application/octet-stream"; break;
            }
            return retval;
        } 
    }

causes the compiler to create this namespaceless, internal class (copied from Reflector):

<PrivateImplementationDetails>{621DEE27-4B15-4773-9203-D6658527CF2B}
    - $$method0x60000b0-1 : Dictionary<String, Int32>
    - Used By: MimeHelper.GetMimeType(String) : String

Why is that? How would I change the above code so it doesn't happen (just out of interest)

Thanks

Andrew

like image 631
Andrew Bullock Avatar asked Apr 08 '09 12:04

Andrew Bullock


People also ask

Why does the C exist?

Like the letter G, C emerged from the Phoenician letter gimel (centuries later, gimel became the third letter of the Hebrew alphabet). In ancient Rome, as the Latin alphabet was being adapted from the Greek and Etruscan alphabets, G and C became disambiguated by adding a bar to the bottom end of the C.

Does the letter C need to exist?

So the C is indeed a very important letter and has no reason to feel ashamed because it makes no sound on it own. Like a man and a women come together to make a unique "sound" in their marriage, so "C" marries "H" to produce a special combined sound. CH itself has three different sounds.

What C makes the CH sound?

The last syllable “CI” is spelt CI. So we get the soft “ch” sound at the end.

Why does C have the sound of S?

Here's the rule: When 'c' comes directly before the letters 'e', 'i' or 'y' we use the /s/ sound. in other cases we use a /k/ sound.


1 Answers

It's creating the dictionary to handle the lookups of the various cases in the switch statement instead of making several branching ifs out of it to set the return value. Trust me -- you don't want to change how it's doing it -- unless you want to make the map explicit.

ASIDE: I had originally assumed that the dictionary stored a map from each case to the an index into another map for the return values. According to @Scott (see comments), it actually stores an index to a label for the code that should be executed for that case. This makes absolute sense when you consider that the code that would be executed for each case may differ and may be much longer than in the given example.

EDIT: Based on your comment, I think I might be tempted to store the mappings in an external configuration file, read them in during start up, and construct the actual map -- either a single level map from key to value or a similar multilevel map from key to index and index to value. I think it would be easier to maintain these mappings in a configuration file than to update the code every time you needed to add or remove a particular case.

like image 60
tvanfosson Avatar answered Oct 07 '22 00:10

tvanfosson