Thursday, October 17, 2013

10 THINGS YOU MAYBE DIDN’T KNOW ABOUT C#

For my first ever post, I thought I'd list some neat things that I know about C# that might surprise the average reader, inspired by damieng's similar post from 2012.

Before you start to read, I should point out that, at best, one or two of the ten points I show below are useful for anything other than niche applications. I just take pleasure in finding these ways to stretch the language. :) I'm not advocating you use ANY of these in production code.

Comments, suggestions, corrections and discussion are all welcome below.


1: You can create non-zero based arrays
Okay, I reckon at least a few people will know this one already, but in case you're not one of them, the Array class has an overload of CreateInstance() that lets you specify your own lower-bounds on the array. Here's an example that shows all the big events in my life up to now:

static void Go() {
    string[] thingsThatHappenedInMyLife = (string[]) Array.CreateInstance(typeof(string), new[] { 24 }, new[] { 1990 });
 
    thingsThatHappenedInMyLife.SetValue("I was born", 1990);
    thingsThatHappenedInMyLife.SetValue("I dislocated my shoulder", 1996);
    thingsThatHappenedInMyLife.SetValue("I was allowed to buy alcohol (big mistake)", 2008);
    thingsThatHappenedInMyLife.SetValue("I started a programming blog", 2013);
 
    for (int i = thingsThatHappenedInMyLife.GetLowerBound(0); i <= thingsThatHappenedInMyLife.GetUpperBound(0); ++i) {
        string lifeEvent = (string)thingsThatHappenedInMyLife[i];
        if (lifeEvent == null) lifeEvent = "not much happened to me";
        Console.WriteLine("In " + i + ", " + lifeEvent);
    }
}

...
...

9: You can prevent people from using ILDASM to inspect your code
Eventually, of course, all attempts to protect your IP are futile; determined hackers/engineers will probably get at your juicy creativity one way or another! But one of the first steps you can take is to prevent people using the popular IL disassembler tool to investigate your assemblies.

Here's a screenshot of the assembly I've been using to test the code in this blog disassembled:

Inline images 2


Now… By adding a single line, and recompiling, I can try that again and see this:

Inline images 3

You can do this by adding the assembly attribute SuppressIldasm. Be warned that this won't prevent reflection-like tools from perusing your libraries. But it's one extra step that's possibly worth taking if you're going to try and protect your IP.

An example follows. The relevant line has been highlighted.

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CSharpAdvancedTricks")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CSharpAdvancedTricks")]
[assembly: AssemblyCopyright("Copyright ©  2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: SuppressIldasm]

...
...

Read more: MEMORY FENCES
QR: Inline images 1