Automated property with getter only, can be set, why?

This is a new C# 6 feature, “Getter-only auto-properties”, also known as “Auto-Property Initializers for Read-Only Properties” as discussed in this MSDN magazine article ‘C# : The New and Improved C# 6.0’ by Mark Michaelis and in the C# 6.0 draft Language Specification. The read-only field’s setter is only accessible in the constructor, in all … Read more

Await in catch block

Update: C# 6.0 supports await in catch Old Answer: You can rewrite that code to move the await from the catch block using a flag: WebClient wc = new WebClient(); string result = null; bool downloadSucceeded; try { result = await wc.DownloadStringTaskAsync( new Uri( “http://badurl” ) ); downloadSucceeded = true; } catch { downloadSucceeded = … Read more

String Interpolation with format variable

No, you can’t use string interpolation with something other than a string literal as the compiler creates a “regular” format string even when you use string interpolation. Because this: string name = “bar”; string result = $”{name}”; is compiled into this: string name = “bar”; string result = string.Format(“{0}”, name); the string in runtime must … Read more

Is nameof() evaluated at compile-time?

Yes. nameof() is evaluated at compile-time. Looking at the latest version of the specs: The nameof expression is a constant. In all cases, nameof(…) is evaluated at compile-time to produce a string. Its argument is not evaluated at runtime, and is considered unreachable code (however it does not emit an “unreachable code” warning). From nameof … Read more

What is the purpose of nameof?

What about cases where you want to reuse the name of a property, for example when throwing exception based on a property name, or handling a PropertyChanged event. There are numerous cases where you would want to have the name of the property. Take this example: switch (e.PropertyName) { case nameof(SomeProperty): { break; } // … Read more

TFS 2013 building .NET 4.6 / C# 6.0

People using TFS 2012 have reported success using: /tv:14.0 /p:GenerateBuildInfoConfigFile=false /p:VisualStudioVersion=14.0 as arguments to MSBuild. Perhaps this might work for you, but so far this does not work for my TFS 2013 build agents. Update: I finally got this to work on TFS 2013. Here is what I had to do: Install VS 2015 or … Read more