c#-6.0

How to use the ternary operator inside an interpolated string?

半腔热情 提交于 2019-11-26 04:38:45
问题 I\'m confused as to why this code won\'t compile: var result = $\"{fieldName}{isDescending ? \" desc\" : string.Empty}\"; If I split it up, it works fine: var desc = isDescending ? \" desc\" : string.Empty; var result = $\"{fieldName}{desc}\"; 回答1: According to the documentation: The structure of an interpolated string is as follows: { <interpolationExpression>[,<alignment>][:<formatString>] } The problem is that the colon is used to denote formatting, like: Console.WriteLine($"The current

TFS 2013 building .NET 4.6 / C# 6.0

夙愿已清 提交于 2019-11-26 03:41:35
问题 We use TFS 2013 to as our build server. I\'ve started a C# 6.0 project and I am trying to get it to build. I am using the new null-conditional operators, and my build chokes. I\'ve tried installing several things on the TFS server, including the targeting pack and VS 2015. I\'ve tried providing /tv:14.0 to the MSBuild arguments. Configuration\\EntityEntityConfig.cs (270): Invalid expression term \'.\' Configuration\\EntityEntityConfig.cs (283): Invalid expression term \'.\' Configuration\

Does C# 6.0 work for .NET 4.0?

亡梦爱人 提交于 2019-11-26 00:46:27
问题 I created a sample project, with C#6.0 goodies - null propagation and properties initialization as an example, set target version .NET 4.0 and it... works. public class Cat { public int TailLength { get; set; } = 4; public Cat Friend { get; set; } public string Mew() { return \"Mew!\"; } } class Program { static void Main(string[] args) { var cat = new Cat {Friend = new Cat()}; Console.WriteLine(cat?.Friend.Mew()); Console.WriteLine(cat?.Friend?.Friend?.Mew() ?? \"Null\"); Console.WriteLine

What does question mark and dot operator ?. mean in C# 6.0?

寵の児 提交于 2019-11-26 00:28:01
问题 With C# 6.0 in the VS2015 preview we have a new operator, ?. , which can be used like this: public class A { string PropertyOfA { get; set; } } ... var a = new A(); var foo = \"bar\"; if(a?.PropertyOfA != foo) { //somecode } What exactly does it do? 回答1: It's the null conditional operator. It basically means: "Evaluate the first operand; if that's null, stop, with a result of null. Otherwise, evaluate the second operand (as a member access of the first operand)." In your example, the point is

How to enable C# 6.0 feature in Visual Studio 2013?

人盡茶涼 提交于 2019-11-25 23:48:46
问题 I was going through the latest features introduced in C# 6.0 and just followed an example of auto property initializer, class NewSample { public Guid Id { get; } = Guid.NewGuid(); } but my IDE did not recognize the syntax. I am wondering how I could enable C# 6.0 in Visual Studio 2013. The Target framework I am using is 4.5.1. 回答1: Information for obsoleted prerelease software: According to this it's just a install and go for Visual Studio 2013: In fact, installing the C# 6.0 compiler from