Sorting a list by a specific field

Use Linq. Not the fastest, but quick and easy…

using System.Linq;
List<NodePos> sortedNodes = nodes.OrderBy(o => o.distance).ToList();



C# Modifier readonly

Modifier: readonly

As the name suggests, the readonly modifier is used to make a member readonly. The content of the member can only be set either during the initial declaration (A) or in the constructor for the class that uses the member (B).

Example A:

readonly int foo = 5;

Example B:

class Foo {
    readonly int bar;
        bar = 5;


C# Modifier async / await

Modifier: async / await

Used to mark a method as being asynchronous and to await results from the asynchronous processing. Available from .NET 4.5 (C# 5).


using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;

namespace Examples
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        /// <summary>
        /// Standard WPF initialization
        /// </summary>
        public MainWindow()
            Console.WriteLine("Finished Initializing.");

        /// <summary>
        /// Async download webpage
        /// </summary>
        /// <returns></returns>
        async Task<int> DownloadWeb()
            Console.WriteLine("Started Async.");
            HttpClient client = new HttpClient();
            Task<string> getStringTask = client.GetStringAsync("");

            Console.WriteLine("Starting await.");
            string urlContents = await getStringTask;
            Console.WriteLine("Finished await.");
            return urlContents.Length;

        /// <summary>
        /// Handle a button press as an async task
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void Button_Click(object sender, RoutedEventArgs e)
            int length = await DownloadWeb();
            Console.WriteLine("Got Result: " + length);


<Window x:Class="Examples.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <Button Content="Button" HorizontalAlignment="Left" Margin="305,134,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>


C# Modifier: abstract

Modifier: abstract

Used to declare an incomplete implementation of a class, method, property, indexer or event.

By declaring a class abstract, it can be used as a template when creating other classes. In the example below, the abstract class A forces any classes that are derived from it to include a ‘Test’ method. Failure to include the Test method will result in an error when compiling.

    abstract class A
        public abstract void Test();
    class B : A
        public override void Test()


C# Operator ‘as’

Operators: as

Used to cast without triggering an exception. More efficient than using the ‘is’ operator as the cast value is returned if the operation is successful while the ‘is’ operator only returns a bool to denote success or failure.


              Car c = lotus as Car;
              If(c != null)
                            System.Console.WriteLine(“Cast worked”);