Al's BrainDump

RSS

Navigation





Quick Search
»
Advanced Search »

PoweredBy

Yield

When returning an iterator from a method, use yield to return 1 element at a time. This enables a foreach statement to loop through it as opposed to return a collection and then iterate it.
public class List
{
    public static IEnumerable Power(int number, int exponent)
    {
        int counter = 0;
        int result = 1;
        while (counter++ < exponent)
        {
            result = result * number;
            yield return result;
        }
    }

    static void Main()
    {
        // Display powers of 2 up to the exponent 8:
        foreach (int i in Power(2, 8))
        {
            Console.Write("{0} ", i);
        }
    }
}

IsNullOrEmpty

Use this as a shortcut.
if (IsNullOrEmpty(planRec.approved_by) 

Using

using (Font font1 = new Font("Arial", 10.0f))
{
  //code here
}
//font 1 has just been disposed of

C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

A using statement can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.

Lock

synchronize access to shared resources is the C# lock keyword. This keyword allows you to define a scope of statements that must be synchronized between threads. By doing so, incoming threads cannot interrupt the current thread, preventing it from finishing its work. The lock keyword requires you to specify a token (an object reference) that must be acquired by a thread to enter within the lock scope.

// Use the current object as the thread token.
lock(this)
{
   // All code within this scope is thread-safe.
}

Arrays

Still used very much - no need to ditch in favour of generics.

Cannot be resized without recreating the array. Handy methods:
  • Sort()
  • BinarySearch()
  • Reverse()
  • Clear()
  • Length()

        //ignore dodgy wiki formatting - weirdness should be double square brackets

        string[] days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };

        // Declare a single-dimensional array 
        int">5"> array1 = new int[5;

        // Declare and set array element values
        int">"> array2 = new int[ { 1, 3, 5, 7, 9 };

        // Alternative syntax
        int[] array3 = { 1, 2, 3, 4, 5, 6 };

        // Declare a two dimensional array
        int, multiDimensionalArray1 = new int2, 3;

        // Declare and set array element values
        int, multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

        // Declare a jagged array
        int">"> jaggedArray = new int[6[">6"> jaggedArray = new int[6[;

        // Set the values of the first array in the jagged array structure
        jaggedArray0 = new int4 { 1, 2, 3, 4 };




Strings

Actually a reference type, not a value type - the only one of the main base type to be a ref.
IMMUTABLE - As a string can never be changed a call such as
mystring.ToUpper();
will not result in mystring being changed - if printed after that line it will still be the same. That method will return the upper case version though, so:
Console.Write(mystring.ToUpper());
Will write the upper case version.

System.Text.StringBuilder should be used for large string manipulation jobs

Break & Continue

In a loop the break keyword will exit the loop while the continue keyword will exit that iteration and start the next.

Output Parameters

if a function marks one of its params as 'out' and the calling function also marks the param as 'out' then the variable passed in will be given a value by the function AS WELL as the function return the expected variable:

//call func with 'out'
myBool = GetPhoneButton(character, out button));
console.write(button.ToString());


//func definition
static bool GetPhoneButton(char character, out char button){
    button = "x";
    return true;
}









ScrewTurn Wiki version 3.0.5.600. Some of the icons created by FamFamFam.