Questions tagged [c#-6.0]

C# 6.0 is shipped along with Roslyn (.NET Compiler Platform) and Visual Studio 2015.

C# 6.0 is shipped along with Roslyn (.NET Compiler Platform) and Visual Studio 2015. The compiler for this version, code-named 'Roslyn', has been released as open source on codeplex during the Build 2014 conference.

The new features that are available in this version include:

  • Expression-bodied Members
  • Auto-property Initializers
  • Getter-only Auto-properties
  • Null-conditional Operator
  • NameOf Operator
  • String Interpolation
  • Await in Catch and Finally Blocks
  • Exception filters
  • Index Initializers
  • Extension Add in Collection Initializes
  • Improved Overload Resolution
  • Using Static Statement

For more information see:

732 questions
592
votes
1 answer

How to use the ternary operator inside an interpolated string?

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}";
Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
537
votes
3 answers

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

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…
Arkadiusz Kałkus
  • 17,101
  • 19
  • 69
  • 108
365
votes
17 answers

What is the purpose of nameof?

Version 6.0 got a new feature of nameof, but I can't understand the purpose of it, as it just takes the variable name and changes it to a string on compilation. I thought it might have some purpose when using but when I try to nameof(T) it just…
atikot
  • 4,761
  • 4
  • 26
  • 34
309
votes
7 answers

What does the => operator mean in a property or method?

I came across some code that said public int MaxHealth => Memory[Address].IsValid ? Memory[Address].Read(Offs.Life.MaxHp) : 0; Now I am somewhat familiar with Lambda expressions. I just have not seen it used it…
Mike
  • 5,918
  • 9
  • 57
  • 94
275
votes
5 answers

Does C# 6.0 work for .NET 4.0?

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;…
MajesticRa
  • 13,770
  • 12
  • 63
  • 77
200
votes
6 answers

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

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…
Hassaan
  • 3,931
  • 11
  • 34
  • 67
183
votes
1 answer

How to use verbatim strings with interpolation?

In C# 6 there is a new feature: interpolated strings. These let you put expressions directly into code. Rather than relying on indexes: string s = string.Format("Adding \"{0}\" and {1} to foobar.", x, this.Y()); the above becomes: string s =…
Keith
  • 150,284
  • 78
  • 298
  • 434
168
votes
2 answers

How do I use the C#6 "Using static" feature?

I'm having a look at a couple of the new features in C# 6, specifically, "using static". using static is a new kind of using clause that lets you import static members of types directly into scope. (Bottom of the blog post) The idea is as…
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
160
votes
5 answers

Long string interpolation lines in C#6

I've found that while string interpolation is really nice when applied to my existing code base's string Format calls, given the generally preferred column limit, the string rapidly becomes too long for a single line. Especially when the…
Jeremiah Gowdy
  • 5,476
  • 3
  • 21
  • 33
145
votes
8 answers

How to use C# 6 with Web Site project type?

Updated an existing Web Site project type Visual Studio 2015, I changed the Framework to 4.6. I then expected to have all those new features available in my code behind files. Unfortunately I'm getting errors like: Error CS8026: Feature…
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
139
votes
7 answers

C# 6.0 Features Not Working with Visual Studio 2015

I am testing Visual Studio 2015 with C# 6.0 but the language features are not working. In an MVC web application, the following code does compile: if (!string.IsNullOrWhiteSpace(Model.Profile?.TypeName)) { // More logic here... } However, when…
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
137
votes
6 answers

How do I disable C# 6 Support in Visual Studio 2015?

Background We have a project that we're developing in VS 2015 with C#6 enabled that occasionally needs to be opened by developers using VS 2013 without C#6. We have no intention to use C# 6 within this particular solution (as much as I'd like…
SeanKilleen
  • 8,809
  • 17
  • 80
  • 133
136
votes
6 answers

What is the meaning of the planned "private protected" C# access modifier?

As part of the Roslyn documentation on GitHub, there's a page called Language feature implementation status, with planned language features for C# and VB. One feature I couldn't wrap my head around was private protected access modifier: private…
Kobi
  • 135,331
  • 41
  • 252
  • 292
132
votes
2 answers

Is nameof() evaluated at compile-time?

In C# 6, you can use the nameof() operator to get a string containing the name of a variable or a type. Is this evaluated at compile-time, or at runtime via some Roslyn API?
Gigi
  • 28,163
  • 29
  • 106
  • 188
118
votes
1 answer

Why can't I use the null propagation operator in lambda expressions?

I often use null propagating operator in my code because it gives me more readable code, specially in long queries I don't have to null-check every single class that is used. The following code throws a compile error that we can't use null…
Mohsen Sarkar
  • 5,910
  • 7
  • 47
  • 86
1
2 3
48 49