The Future of C# with Mads Torgersen: C# 6

 

Copy/paste from http://adamralph.com/2013/12/06/ndc-diary-day-3/

The NDC Wi-Fi was put to the test during this talk with the C# Twitter stream seeing an explosion in traffic as Mads revealed, for the first time publicly, a range of features either set to be delivered in the next version of C# or at least under strong consideration.

Unfortunately I was too caught up in the excitement to note everything down but after mining my memory and the Twitter feed, here is the list of implemented/potential language features (I’m sure I’m missing at least one of them):

  • primary constructors – public class Point(int x, int y) { }
  • read only auto-properties – public int X { get; } = x;
  • static type using statements – using System.Math;
  • property expressions – public double Distance => Sqrt(X * X + Y * Y);
  • method expressions – public Point Move(int dx, int dy) => new Point(X + dx, Y + dy);
  • params for enumerables – public Point Average(params IEnumerable<Point> points) { }
  • monadic null checking – if (points?.FirstOrDefault()?.X ?? -1) { }
  • constructor type parameter inference – var t = new Tuple(1,2); // infers Tuple<T1, T2>
  • inline declarations for out params – public void Foo(out var x, out var y) { }

I love all these features perhaps with the exception of the last one. Whist it doesn’t negatively effect the language (although some of the audience were concerned about encouraging the use of out parameters) it doesn’t really do much to achieve the stated goal of improving syntax for multiple return values. I was hoping for something like:

public (int x, int y) Foo() { }

But still, the rest of the features look awesome and I can’t wait to get my hands on C# vNext!

More info at http://adamralph.com/2013/12/06/ndc-diary-day-3/

Fixing comctl32.dll WIN32Exception

A C# project was recently upgraded from .NET 3.5 to .NET 4.0.
The program worked fine, except on a Windows 8 Pro x64 machine where this error was given:

Win32Exception: Cannot load the “comctl32.dll” DLL into memory.

The solution that worked for us (after a long search) was removing CorFlags to set the application to 32-bits mode. When a new build was created without CorFlags and setting Visual Studio to build the project to x86 instead of x64 solved the problem.

It looks like CorFlags is not reliable…

Get information from Team Foundation Server (TFS) via C# code

This code shows you how you could interact with your team foundation server. In my case a TFS 2010.

Open a new console application in Visual Studio and add these references:

  • Microsoft.TeamFoundation.Client
  • Microsoft.TeamFoundation.VersionControl.Client
TeamFoundationServer tfs = new TeamFoundationServer("http://yourtfsserver:port/something");
List<string> changedFiles = new List<string>();

VersionControlServer VCServer = (VersionControlServer)tfs.GetService<VersionControlServer>();

try
{
string path = @"C:\Users\<username>\Documents\Visual Studio 2010\Projects\just the path to your solution";
VersionSpec version = VersionSpec.Latest;
int deletionId = 0;
RecursionType recursion = RecursionType.Full;
string user = @"domain\yourusername";

foreach (Changeset item in VCServer.QueryHistory(path, version, deletionId, recursion, user, null, null, Int32.MaxValue, true, false, true))
{
  foreach (Change c in item.Changes)
  {
	// c.Item.ServerItem;
  }
}
}
catch { }

Happy coding ;)

C# Linq: Zip method

string[] brands = new string[] { "BMW", "Mercedes", "Audi" };
string[] websites = new string[] { "www.bmw.com", "www.mercedes.com", "www.audi.com" };

var output = brands.Zip(websites, (b, w) => string.Format("{0} can be found at {1}", b, w));

foreach (var line in output)
  Console.WriteLine(line);

This gives you:

BMW can be found at www.bmw.com 
Mercedes can be found at www.mercedes.com 
Audi can be found at www.audi.com

More information at: http://msdn.microsoft.com/en-us/library/dd267698.aspx

Rx (Reactive Extensions)

Reactive Extensions (Rx) was added to Codeplex.com:

The project is actively developed by the Microsoft Open Technologies, inc., in collaboration with a community of open source developers.

The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators. Using Rx, developersrepresent asynchronous data streams with Observables , query asynchronous data streams usingLINQ operators, and parameterize the concurrency in the asynchronous data streams usingSchedulers. Simply put, Rx = Observables + LINQ + Schedulers.

Find out more on: https://rx.codeplex.com/

Making Instant C# Viable – Visualization

A project from Ermau: Research into instant feedback in software engineering.

Check out the cool video at http://ermau.com/making-instant-csharp-viable-visualization/.

Find out more at:

C#: Save a canvas as an image

This C# code saves a Canvas as a *.PNG image:

private void CreateSaveBitmap(Canvas canvas, string filename)
     {
       RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
        (int)canvas.Width, (int)canvas.Height,
        96d, 96d, PixelFormats.Pbgra32);
      // needed otherwise the image output is black
       canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
       canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));

renderBitmap.Render(canvas);

//JpegBitmapEncoder encoder = new JpegBitmapEncoder();
       PngBitmapEncoder encoder = new PngBitmapEncoder();
       encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

using (FileStream file = File.Create(filename))
       {
         encoder.Save(file);
       }
     }

Use it like this:

CreateSaveBitmap(myCanvas, @"C:\temp\out.png");