BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

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

by Roopesh Shenoy on Sep 19, 2011 |

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!

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Not sure if the CallerMemberName example will work by Samuel Jack

I'd love to be proved wrong, but I'm not sure your example for CallerMemberName will work. I'm guessing that the property names will be prefixed with get_ and set_ as those are the actual member names. Though in that case you could of course check and strip the prefix.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT