Code Bites C#

Learning C#

Learning C#

I've always wanted to learn to program in the "C" family. C# is very close to JavaScript, and no longer does one have to lift the hefty Visual Studio pricetag to lean it. When you add in my years of JavaScript and Flash, this should be

There is nothing I love more than a good Multidimensional Array. So it is one of the first things I wanted to figure out.


// set the var

public string[][] pass = new string[2][];

// add values
pass[0] = new string[] { "book", "librarian", "quiet","read" };
pass[1] = new string[] { "dog","cop","cuffs","coffee","doughnut","butts" };

// grab a random value
passUse = pass[level - 1][Random.Range(0, pass[level - 1].Length)];
Debug.LogWarning("Shhhh, the password is: " + passUse);

String Abuse

Useful little class (not mine) for creating anagrams. I like the way it's structured, and again--check out how it loops through the array.


public static class StringExtension

    // shuffle the selected string
    public static string Anagram(this string str) 
        string attempt = Shuffle(str);
        // keep doing this till it is all mixed up
        while (attempt == str)
            attempt = Shuffle(str);
        return attempt;

    // rearrange characters in a string
    private static string Shuffle(string str)
        char[] characters = str.ToCharArray();
        System.Random randomRange = new System.Random();
        int numberOfCharacters = characters.Length;
        while (numberOfCharacters > 1)
            int index = randomRange.Next(numberOfCharacters + 1);
            var value = characters[index];
            characters[index] = characters[numberOfCharacters];
            characters[numberOfCharacters] = value;
        return new string(characters);