Testing Xamarin Projects using NUnit 3

NUnit Tests on AndroidWhen testing Xamarin using NUnit 3, you have two choices.

  1. A Portable Library that you reference from the test runner for each platform, or
  2. A Shared Project with your tests referenced from the platform specific test runners.

I prefer the Shared Project approach because it also allows you to run your tests from within Visual Studio by also creating a .NET 4.5.x test project. As of NUnit 3.0.1, you cannot run tests that use the portable version of the NUnit Framework from the console or within Visual Studio, so the .NET 4.5.x test project gets around that limitation. (more…)

Read more

C# 6.0 – String Interpolation

This is another post in the series on the new features that are coming in C# 6.0.

If you want to try out any of these features now, check out the Rosyln compiler and then install the Visual Studio 2015 Preview or fire up a VM in Azure with Visual Studio 2015 Preview already installed.

String interpolation in C# is fairly easy if you only have a couple of arguments, but it can be error prone with many arguments or if you need to re-order the arguments. It is also difficult to quickly see which variable gets substituted in where.

Currently, we write code like this,

var msg = string.Format("You ordered {0} {1}{{s}}", line.Quantity, line.Product.Name);

C# 6.0 makes this easier. You can omit the `String.format` and put the variables right in the curly braces. Notice the slash at the start.

var msg = "You ordered \{line.Quantity} \{line.Product.Name}{s}";

Notice that pesky improper pluralization though. It was always a pain to deal with. Now, just put the code you need inline.

var msg = "You ordered \{line.Quantity} \{line.Product.Name}\{line.Quantity > 1 ? "s" : ""}";

The syntax currently uses a slash before the brace. In a later update, it will be switching to a dollar sign before the first quote and no slash, like this,

var msg = $"You ordered {line.Quantity} {line.Product.Name}{s}";

If you want to check out the code for this series of posts, it is on GitHub at https://github.com/rprouse/CSharp60.

Read more

C# 6.0 – Declaration Expressions

UPDATEIt was decided that Declaration Expressions would not make it into the final release of C# 6.0 and they no longer work in Visual Studio 2015 Preview.


This is another post in the series on the new features that are coming in C# 6.0.

This is a complex name for a simple concept. Until now, if you ever wanted to use an out parameter in a method, you had to declare it before using it.

We’ve all written code like this,

int i;
if (Int32.TryParse(value, out i))
{
    // Use i here
}

Now you can write the much simpler,

if (Int32.TryParse(value, out var i))
{
    Assert.That(i, Is.EqualTo(expected));
}

There are a couple of things to note here. First is that i is only in scope within the if statement. You cannot use it outside the if statement. Second, notice that I used var and that the type was correctly infered as int.

It is also useful for casting. You no longer have to cast an object as something, then check if it is null. You can now do it all within the if statement.

if ((string str = obj as string) != null )
{
    Console.WriteLine(str);
}

It can also be useful for quickly supplying default args for a TryParse.

int x = Int32.TryParse(value, out var i) ? i : -1;

Sweet and simple…

If you want to check out the code for this series of posts, it is on GitHub at https://github.com/rprouse/CSharp60.

Read more

C# 6.0 – Null-Conditional Operators

This is another post in the series on the new features that are coming in C# 6.0.

This is one of my favourite new features. Have you ever written code like this?

if (line != null && line.Product != null)
{
    total += line.Product.Price * line.Quantity;
}

C# 6.0 introduces the ?. null-conditional operator that short circuits and returns null if anything in the chain is null. This combines really well with the null-coalescing operator allowing you to rewrite the above as follows.

total += line?.Product?.Price ?? 0 * line?.Quantity ?? 0;

This also works with indexers,

var firstProduct = OrderLines?[0].Product;	// Will return null if OrderLines is null

It also works with methods. This will be really handy for triggering events. Instead of writing,

if (OrderChanged != null)
{
    OrderChanged(this, EventArgs.Empty);
}

You will now be able to write.

OrderChanged?.Invoke(this, EventArgs.Empty);

If you want to check out the code for this series of posts, it is on GitHub at https://github.com/rprouse/CSharp60.

Read more

C# 6.0 – Auto-Property Changes

This is the second in a series of posts on the new features that are coming in C# 6.0. There have been a few small improvements to Auto-Properties.

Auto-Property Initializers

I hinted at this feature in my last post on Primary Constructors. Auto-Properties were introduced way back in C# 3.0, but unlike other members, you had to initialize their values in the constructor.

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }

    public Product()
    {
        Name = "Toy Car";
        Price = 9.99m;
    }
}

In C# 6.0, you can now initialize the properties where they are declared.

public class Product
{
    public string Name { get; set; } = "Toy Car";
    public decimal Price { get; set; } = 9.99m;
}

Getter Only Auto-Properties

You could always declare the setter of an auto-property private or protected to prevent it from being used outside of the class.

public decimal Price { get; private set; }

Now you can omit the setter and it makes the underlying field read-only, so you can only set it in the constructor. This is a great feature for creating and enforcing imutable classes.

public class Product
{
    public Product(string name, decimal price)
    {
        Name = name;
        Price = price;
    }

    public string Name { get; }
    public decimal Price { get; }
}

If you want to check out the code for this series of posts, it is on GitHub at https://github.com/rprouse/CSharp60.

Read more