Blog

The following features require the C# 6.0 compiler which is included in Visual Studio 2015.  Remarkably however C# 6 new features does not require and updated version of Microsoft .NET Framework (i.e. as long as you compile with VS.NET 2015 the features still work against .NET Framework 4).  This is possible as the features are implemented in the compiler itself and do not have dependencies on the .NET Framework.

1.) Using Static

Using static directives for System.ConsoleColor looks like (as well as System.IO.Directory)

using static System.ConsoleColor;

using static System.IO.Directory;

ForegroundColor = Yellow;

string[] files = GetFiles(directoryPath, searchPattern, System.IO.SearchOption.AllDirectories);

These enable the invocation of numerous methods, properties and enums directly.  In each case, this eliminates the need to qualify the static member with its type.

 

2.) The nameof operator

This is a new contextual keyword to identify a string literal that extracts a constant for (at compile time) the unqualified name of whatever identifier is specified as an argument.  The nameof(filename) returns “filename,” the name of the Encrypt method’s parameter.  However, nameof works with any programmatic identifier.  By leveraging the nameof operator, it’s possible to eliminate the vast majority of “magic” strings that refer to code identifiers as long as they’re in scope. This not only eliminates runtime errors due to misspellings within the magic strings, which are never verified by the complier, but also enables refactoring tools like Rename to update all references to the name change identifier.

private static void Encrypt(string filename)
  {
    if (!Exists(filename)) // LOGIC ERROR: Using Directory rather than File
    {
      throw new ArgumentException("The file does not exist.", 
        nameof(filename));
    }
    // ...
  }

 

3.) String Interpolation – this is an easier one to grasp.  This is the before syntax…

 string.Format("Hello!  My name is {0} {1} and I am {2} years old.", person.FirstName, person.LastName, person.Age);

after…

$"Hello!  My name is {person.FirstName} {person.LastName} and I am {person.Age} years old.";

The string interpolation syntax reduces errors caused by arguments following the format string that are in improper order, or missing altogether and causing an exception.

 

4.) Null-Conditional Operator

C# 6.0 introduces the “?.” operator known as the null-conditional operator.  The null-conditional operator translates to checking whether the operand is null prior to invoking the method or property.  What makes the null-conditional operator especially convenient is that it can be chained. If, for example, you invoke string[] names = person?.Name?.Split(' '), Split will only be invoked if both person and person.Name are not null. When chained, if the first operand is null, the expression evaluation is short-circuited, and no further invocation within the expression call chain will occur.

//after

switch (args?.Length) {

}

//before

(args != null) ? (int?)args.Length : null

 


5.) Auto-Property Improvements

Getter-only auto-properties are a C# 6.0 feature for declaring read-only properties that are backed (internally) by a read-only field. As such, these properties can only be modified from within the constructor.  Rather than the six or so lines needed to declare a read-only property and initialize it prior to C# 6.0, now a single-line declaration and the assignment from within the constructor are all that’s needed.

public ConsoleColor ForegroundColorVerbose { get; }

A second auto-property feature introduced in C# 6.0 is support for initializers.

static private Lazy<ConsoleConfiguration> DefaultConfig{ get; } = new Lazy<ConsoleConfiguration>(() => new ConsoleConfiguration());

 

6.) Expression Bodied Methods and Auto-Properties

This feature exists for both properties and methods and allows the use of the arrow operator (=>) to assign an expression to either a property or method in place of a statement body.

static public ConsoleConfiguration GetDefault() => DefaultConfig.Value;

 

6.) Exception Improvements – ability to use a when clause to do additional filtering when an exception is thrown

try { // Do stuff } catch (Exception e) when ((DateTime.Now.DayOfWeek == DayOfWeek.Saturday) { // Swallow }

 

c6

 

What’s going on?  I want to run my Android emulator (XAP – Xamarin Android Player) and I get the following issue.

“Failed to initialize device”, “VboxMessage command failed: See log for further details”
image

Ok, what’s going on today with my Xamarin Player.  A bit of Google-ing came up with the following information announced at the 2016 Evolve conference.

New Mac OS X Universal Installer

To streamline development setup on Mac OS X, we have introduced a brand new universal installer. This will not only handle updating to the latest version of Xamarin, but also will setup the new and improved Android Emulators from Google, based on x86 HAXM, that are now 10 times faster then before! If you are on Windows using Visual Studio be sure to checkout the Hyper-V enabled Visual Studio Emulators for Android that make debugging Android apps a joy. With these enhancements to the Android emulators for both Mac and PC the Xamarin Android Player is now officially deprecated. We hope that you love this new setup process and the new emulators.

Wow - Xamarin Android Player is now officially deprecated – This honestly was a surprise as it had worked so well for me in the past.

So as instructed checking out the Hyper-V enabled Visual Studio Emulators for Android  I downloaded, installed the emulator promising “The x86 emulator boots and runs at nearly the speed of a physical device, making debugging a breeze on graphics-intensive, processor-hungry apps.”

 image

After the installation, I was required to restart.  I opened my Android-Xamarin app thinking I would find this emulator choice in the Visual Studio Device drop down.  Nope.  I found ‘Visual Studio Emulator for Android’ as a recently installed application.  I soon made that attached to my task bar for ease of finding next time.  Starting it, I was challenged with the next hurdle. “You do not have permission to modify internal Hyper-V network adapter settings, which are required to run the emulator” – I hit ‘Retry’ and it appears to be running with the

 

image

– I hit ‘Retry’ and it appears to be running with the following notification.  Still preparing, I am starting to wonder if anything is going on. 

image

Bang – I am starting to see something with “OS – Starting” and I now see the emulator.  Whew.

image

Checking things out, clicking around I try the web browser, and given another test of my competence.  So the problem is, I cannot browse to the internet via the Android Emulator default browser.

image

I uninstalled Xamarin Android Player – figuring I didn’t need this any more ..moment of silence please.

Ok, so using a few of my Sherlock skills I opened up Hyper-V Manager (found via start search).  Select the emulator then the settings option for that emulator.

image

I then selected the ‘Emulator External Network Adaptor” and chose my physical network adapter, closed/shutdown the emulator and restarted.  Great- challenge completed and all seems to be working.

image

image

Circling back to my Visual Studio – I am not able to use the Visual Studio Android emulator by selecting the appropriate device.  I have successfully removed the Xamarin Android Player, installed the new/improved Visual Studio Emulator and made modifications via Hyper-V Manager to set the network connection of the device to my local hardware on my pc.
image

About Us

Web/Mobile Solutions

Useful Links

Our Contacts

Cincinnati, OH 45069