BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News C#, VB.NET To Get Windows Runtime Support, Asynchronous Methods

C#, VB.NET To Get Windows Runtime Support, Asynchronous Methods

This item in japanese

Bookmarks

C# and VB.NET will soon be getting new features like Windows Runtime Support, Asynchronous Methods, Caller Info attributes and more. Also, compiler will get APIs which will expose what the compiler knows about the code to the IDE and the developers.

Microsoft’s Anders Hejlsberg presented the new features in C# and VB.NET 5.0 at the Microsoft BUILD conference. Samuel Jack does a good job of listing out the details in his blog post “What’s new in C# 5.0 and VB.NET?”. Below is a brief summary of what’s new -

  • Windows Runtime Support – C# and .NET now have deep integration with the Windows Runtime – a C# project can compiled into a WinMD file and then referenced from a HTML/JavaScript project. We have covered this in more detail on InfoQ earlier.
  • Asynchronous methods – this simpler way to write asynchronous tasks with the async and await keywords was first demonstrated in PDC last year and then previewed in an Async CTP. These articles explore this feature in more detail
  • Caller Info Attributes – new attributes CallerFilePath, CallerLineNumber and CallerMemberName that can be used against optional method parameters, which will help in getting more details about the caller without having to pass it from the calling method
  • Compiler APIs – This one is supposed to come after C# 5.0 – the APIs will expose whatever knowledge the compiler has about the code to the IDE and the developers through Syntax Tree APIs, Symbol APIs, Binding and Flow analysis APIs and Emit APIs. You can get more details in this Microsoft Research video.

These new features are going to make things easier for .NET developers. For instance, this is a common pattern for property changed notifications -

public class Customer : INotifyPropertyChanged

{

   public event PropertyChangedEventHandler PropertyChanged;

   private string _firstName;

   public string FirstName

   {

     get { return _firstName; }

     set { Set(ref _firstName, value, "FirstName"); }

   }

 

   private void Set<T>(ref T field, T value, string memberName)

   {

      if (!object.Equals(field, value))

      {

         field = value;

         if (PropertyChanged != null)

         PropertyChanged(this, new PropertyChangedEventArgs(memberName));

       }

   }

}

The problem with this pattern is that it is real easy to mess it up. If you misspell the property name in the setter, or rename the property, then it will silently fail. 

Using the new macro-like attributes you can remove the failure point: 

public class Customer : INotifyPropertyChanged

{

   public event PropertyChangedEventHandler PropertyChanged;

   private string _firstName;

   public string FirstName

   {

      get { return _firstName; }

      set { Set(ref _firstName, value); }

   }

 

   private void Set<T>(ref T field, T value,[CallerMemberName] string memberName = null)

   {

      if (!object.Equals(field, value))

      {

         field = value;

         if (PropertyChanged != null)

         PropertyChanged(this, new PropertyChangedEventArgs(memberName));

      }

   }

}

Similarly, asynchronous programming will now be much easier and cleaner to use, and will probably be used more often – a good thing consider touch interfaces will need most methods to be asynchronous. Silverlight developers especially will get reprieve. And like the “Paste as VB” and “Paste as C#” demo that Anders shared, Compiler APIs will bring in a lot of possibilities for .NET tooling vendors. Looks like interesting times ahead!

Rate this Article

Adoption
Style

BT