Questions tagged [c#-7.1]

Use this tag for questions specific to C# 7.1, released on 16 August 2017. In most cases you should also specify the C# tag.

C# 7.1 adds the ability to configure the compiler to match a specified version of the language. That enables you to separate the decision to upgrade tools from the decision to upgrade language versions.

C# 7.1 adds the language version selection configuration element, three new language features and new compiler behavior.

The new language features in this release are:

Finally, the compiler has two options /refout and /refonly that control reference assembly generation.


Full release notes are available here.

23 questions
47
votes
2 answers

IWebHost: Calling Run() vs RunAsync()

When a new ASP.NET Core 2.0 project is created, the boilerplate Main method in the Program class looks something like this: public static void Main(string[] args) { BuildWebHost(args).Run(); // BuildWebHost returns an IWebHost } But since C#…
Ali Zahid
  • 1,329
  • 2
  • 17
  • 23
46
votes
5 answers

C# 7.1 can't be published

I have ASP.NET Core C# web application. I made some changes that now use C# 7.1 features. I changed project version, so it compiles and runs fine. However, when I try to publish the project, I am getting an error: Feature 'default literal' is not…
Felix
  • 9,248
  • 10
  • 57
  • 89
31
votes
6 answers

Using C# 7.1 with MSBuild

To use the new C# 7.1 language features with Visual Studio 2017, you add the setting latest to your project file(s). However, building such projects from MSBuild (version 15.3.409.57025, located at C:\Program Files…
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
19
votes
5 answers

Switch based on generic argument type

In C# 7.1 the below is valid code: object o = new object(); switch (o) { case CustomerRequestBase c: //do something break; } However, I want to use the pattern switch statement in the following scenario: public T…
11
votes
1 answer

Using C# 7.1 default literal in nullable optional argument causes unexpected behavior

C# 7.1 introduces a new feature called "Default Literals" that allows new default expressions. // instead of writing Foo x = default(Foo); // we can just write Foo x = default; For Nullable types, the default value is null, and with the usual…
Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107
8
votes
3 answers

Using await Task.Factory.StartNew on a method will return immediately

I'm running the following code (C#7.1 Console App), and I can't really understand why the difference in behavior. If I await a regular async method call, or a Task.Run - it works as expected (i.e. the app doesn't return immediately). But if I use…
Maverick Meerkat
  • 5,737
  • 3
  • 47
  • 66
7
votes
1 answer

C# 7.1 and 7.2 Span and ReadOnlySpan

I am using Visual Studio 15.5.6 version. When I create a simple console application project I can't use ReadOnlySpan or Span or Memory. I set the project to .Net Framework 4.7.1, because in Visual Studio that is the latest version. Is there anybody…
Murat Can OĞUZHAN
  • 735
  • 11
  • 19
6
votes
1 answer

Why do ternary operator and if statement return different results?

In the following example, I'm returning a DateTimeOffset? using the default value var a = ConvertToDateTimeOffsetA(null); // 1/1/0001 12:00:00 AM +00:00 var b = ConvertToDateTimeOffsetB(null); // null private static DateTimeOffset?…
KyleMit
  • 30,350
  • 66
  • 462
  • 664
6
votes
1 answer

async Task Main() in WinForms application having async initialization

We're having a winforms application that uses an async initialization process. Simplified you can say that the application will run the following steps: Init - this runs async Show MainForm Application.Run() The currently existing and working code…
Sebastian Schumann
  • 3,204
  • 19
  • 37
3
votes
1 answer

C#: default literal and type inference on nullable structs

Since C# 7.1, it is possible to get default values by using default without specifying the type. I tried it out today and found the results for nullable structs and nullable value types somewhat counterintuitive. [TestFixture] public class Test { …
Jan
  • 241
  • 1
  • 6
3
votes
1 answer

Why is this method group conversion ambiguous in C# 7.2 and lower?

Given the following class: public class Foo { public Foo(int i, double d) { Integer = i; Double = d; } public int Integer {get;} public double Double {get;} private static Random rand = new Random(); public…
Jonathon Chase
  • 9,396
  • 21
  • 39
3
votes
1 answer

When to create and distribute "reference assemblies"?

C# 7.1 introduced a few new command line parameters to help create "reference assemblies". By documentation it outputs an assembly which: have their method bodies replaced with a single throw null body, but include all members except anonymous…
Imre Pühvel
  • 4,468
  • 1
  • 34
  • 49
2
votes
3 answers

What is generic pattern matching for in c# 7.1?

There is new feature introduced in C# 7.1 called generic pattern matching. One of the examples I found looks like this: void Attack(IWeapon weapon, IEnemy enemy) { switch (weapon) { case Sword sword: // process sword…
KorsaR
  • 536
  • 1
  • 10
  • 26
2
votes
5 answers

What is the difference between 'main' and 'async main'?

Here I have two functions static int Main() and static async Task Main(). Can anyone tell me what is the difference between them? static int Main() { return DoAsyncWork().GetAwaiter().GetResult(); } static async Task Main() { …
Minhaj Patel
  • 579
  • 1
  • 6
  • 21
1
vote
1 answer

C# Linq return list of records with different flags value

I have set of targetEngagementIds in engDetails object, and want to retrive targetEngagementId from object with two different filter, First, to get all the TargetEngagementId without any where clause. Second, to get only Ready Status…
1
2