Use Linq. Not the fastest, but quick and easy…
using System.Linq; List<NodePos> sortedNodes = nodes.OrderBy(o => o.distance).ToList();
Use Linq. Not the fastest, but quick and easy…
using System.Linq; List<NodePos> sortedNodes = nodes.OrderBy(o => o.distance).ToList();
The sealed modifier is used to stop a class from being inherited or to stop a virtual method from being overridden.
Example:
class A {} sealed class B : A {} class C : B {} // This will fail at compile time with CS0509
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; Foo() { bar = 5; } }
Used to mark a method as being asynchronous and to await results from the asynchronous processing. Available from .NET 4.5 (C# 5).
Example: 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("Initializing..."); InitializeComponent(); 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("http://msdn.microsoft.com"); 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); } } } XAML <Window x:Class="Examples.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Examples" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="Button" HorizontalAlignment="Left" Margin="305,134,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> </Grid> </Window>
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.
Example: abstract class A { public abstract void Test(); } class B : A { public override void Test() { } }
Used to check whether a cast from type A to type B. It is often more efficient to do the cast using ‘as’ as it will return the casted content instead of just telling you that it would work if you tried.
Example: If(lotus is Car) System.Console.WriteLine(“A Lotus is a Car”);
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.
Example:
Car c = lotus as Car; If(c != null) System.Console.WriteLine(“Cast worked”);