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;
    Foo()
    {
        bar = 5;
    }
}

 

Advertisement

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).

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>

 

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.

Example:
    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.

Example:

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