Creating Inline Data Driven Tests in xUnit

xUnit.net allows the creation of data-driven tests. These kind of tests get their test data from outside the test method code via parameters added to the test method signature.

Say we had to test a Calculator class and check it’s add method returned the correct results for six different test cases. Without data-driven tests we’d either have to write six separates tests (with almost identical code) or some loop inside our test method containing an assert.

Regular xUnit.net test methods are identified by applying the [Fact] attribute. Data-driven tests instead use the [Theory] attribute.

To get data-driven features and the [Theory] attribute, install the xUnit.net Extensions NuGet package in addition to the standard xUnit.net package.

Creating Inline Data-Driven Tests

The [InlineData] attribute allows us to specify test data that gets passed to the parameters of test method.

So for our Calculator add test we’d start by defining the test method:

More...

Improving Struct Equality Performance in C#

The equality performance of struct equality comparisons can be improved by overriding .Equals(). This is especially true if the structs we are comparing contain reference type fields.

By default, the equality of structs is automatically determined by doing a byte-by-byte comparison of the two struct objects in memory – only when the structs don’t contain any reference types. When the structs contain reference type fields then reflection is used to compare the fields of the two struct objects which results in slower performance.

This chart show the relative performance of the default equality of a struct that contains only value types against a struct that also contains a reference type:

struct equality performance chart

More...

New Pluralsight Course: The xUnit.net Testing Framework

My latest Pluralsight course on the xUnit.net testing framework has just been released.

Course Description

Learn the latest in unit testing technology for C#, VB.NET (and other .NET languages) created by the original inventor of NUnit.

xUnit.net is a free, extensible, open source framework designed for programmers that aligns more closely with the .NET platform.

 

You can check it out now on Pluralsight.com.

You Don’t Have to Write a Generic Class to Use Generic Methods in C#

We can create generic classes such as:

class ThingWriter<T>
{
    public void Write(T thing)
    {
        Console.WriteLine(thing);
    }
}

Here we’re defining a generic class, then using the type T as a method parameter in the Write method. To use this class we’d write something like:

var w = new ThingWriter<int>();

w.Write(42);

We don’t however have to be working in a generic class to make use of generic methods.

More...

Converting Chars to Doubles in C#

If we’ve got chars with numeric digits in them, we can convert them to numeric (double) values using the char.GetNumericValue method.

The following code:

double d = char.GetNumericValue('5');

Console.WriteLine(d);

Outputs the value: 5

So why does GetNumericValue return a double when a char can only be a single character and hold a single “number” (‘0’ to ‘9’)? Because char holds Unicode characters.

So the following code:

double d = char.GetNumericValue('⅔');

Console.WriteLine(d);

Outputs the value: 0.666666666666667

Hence the need for GetNumericValue to return a double.

 

For more C# tips check out my Pluralsight courses: C# Tips and Traps and C# Tips and Traps 2.

What’s the Difference Between & and && in C# ?

My previous article “Non Short Circuiting C# Conditional Operators” garnered some comments which suggested to me that I should expand on it. There can sometimes be some confusion over the & and && operator in C# so hopefully this article goes some way to helping clarify the differences between them.

The && Operator

Works with: bools

The && operator performs a logical AND on Boolean values.

For example, the following code:

bool b1 = true;
bool b2 = false;

bool isBothTrue = b1 && b2;

Console.WriteLine(isBothTrue);

Outputs:

False

If we get our two bool values from the result of a couple of methods instead, and the first method returns false, the && operator won’t execute the second method. The following code:

More...

Non Short Circuiting C# Conditional Operators

Most of the time when writing C# programs we use the short-circuiting conditional operators. These operators do not continue evaluation once the outcome has been determined.

Take this code:

bool b = false && CheckName(name);

b = false & CheckName(name);

Here we’re performing a logical AND with the value false and the result of the CheckName method. Because we have a false, the logical AND can never be true.

The first statement using the short-circuiting AND operator (“&&”) will not execute the CheckName method because as soon as it’s determined the outcome (the first value is false and we are AND-ing) the remaining terms are not even evaluated.

More...

New Pluralsight Course: C# Tips and Traps 2

My latest Pluralsight course has just been released. It’s the follow-up course to the popular first C# Tips and Traps course.

Course Description

Whether you're still learning C# or you already have some experience, it's sometimes hard to know what you don't know. This is the follow-up course to C# Tips and Traps and is designed to further short-circuit your C# learning and provides a whole host of useful information about the sometimes under-used or unknown features of both the C# language and the .Net framework. It's suitable for those who are brand new to C# as well as experienced developers looking to "round off" their C# skills and "fill in the gaps".

Check out the course table of contents for more details.

Using C# Keywords for Variable Names

It’s possible to use C# keywords for variable names, etc.

For example if we wanted a variable called “namespace” (which is a C# keyword) we can prefix the declaration with an @ and use this prefix whenever we refer to the variable:

var @namespace = "hello";

@namespace += " world";

 

More...