Questions tagged [c#-7.0]

The version of C#, released in 2017 that added value tuples, local functions, basic pattern matching, ref locals and returns, async main, and various other new features. In most cases you should also specify the c# tag.

Features added to C# 7.0 (Visual Studio 2017)

Features added to C# 7.1 (Visual Studio 2017 Update 3)

Features added to C# 7.2 (Visual Studio 2017 Version 15.5)

497 questions
562
votes
3 answers

What is the difference between "x is null" and "x == null"?

In C# 7, we can use if (x is null) return; instead of if (x == null) return; Are there any advantages to using the new way (former example) over the old way? Are the semantics any different? Is it just a matter of taste? If not, when should I use…
Maniero
  • 10,311
  • 6
  • 40
  • 85
429
votes
10 answers

Predefined type 'System.ValueTuple´2´ is not defined or imported

I've installed Visual Studio 15 Preview 3 and tried to use the new tuple feature static void Main(string[] args) { var x = DoSomething(); Console.WriteLine(x.x); } static (int x, int y) DoSomething() { return (1, 2); } When I compile I…
gsharp
  • 27,557
  • 22
  • 88
  • 134
228
votes
5 answers

Local function vs Lambda C# 7.0

I am looking at the new implementations in C# 7.0 and I find it interesting that they have implemented local functions but I cannot imagine a scenario where a local function would be preferred over a lambda expression, and what is the difference…
Sid
  • 14,176
  • 7
  • 40
  • 48
179
votes
6 answers

What's the difference between System.ValueTuple and System.Tuple?

I decompiled some C# 7 libraries and saw ValueTuple generics being used. What are ValueTuples and why not Tuple instead? https://learn.microsoft.com/en-gb/dotnet/api/system.tuple https://learn.microsoft.com/en-gb/dotnet/api/system.valuetuple
Steve Fan
  • 3,019
  • 3
  • 19
  • 29
154
votes
2 answers

How to use C# 7 with Visual Studio 2015?

Visual Studio 2017 (15.x) supports C# 7, but what about Visual Studio 2015 (14.x)? How can I use C# 7 with it?
Luis Teijon
  • 4,769
  • 7
  • 36
  • 57
118
votes
1 answer

In C# can you define an alias to a value tuple with names?

I know it's possible to define aliases in C# with the using keyword. e.g. using ResponseKey = System.ValueTuple; However, is it possible to define one using the new syntax for value tuples? using ResponseKey = (Guid…
Nick Randell
  • 17,805
  • 18
  • 59
  • 74
113
votes
4 answers

Unable to return Tuple from a method using Visual Studio 2017 and C# 7.0

I've installed Visual Studio 2017 Community that was released a week ago, and I started exploring the new features of C# 7. So I created a simple method that returns two values: public class Program { public static void Main(string[] args) …
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
107
votes
2 answers

Odd return syntax statement

I know this may sound strange but I don't know even how to search this syntax in internet and also I am not sure what exactly means. So I've watched over some MoreLINQ code and then I noticed this method public static IEnumerable
kuskmen
  • 3,648
  • 4
  • 27
  • 54
106
votes
3 answers

TryParse with out var param

A new feature in C# 6.0 allows to declare variable inside TryParse method. I have some code: string s = "Hello"; if (int.TryParse(s, out var result)) { } But I receive compile errors: What I am doing wrong? P.S.: in project settings C# 6.0 and…
Anton23
  • 2,079
  • 5
  • 15
  • 28
103
votes
22 answers

Could not load file or assembly 'System.ValueTuple'

I've got a VS2017 project that compiles to a DLL which is then called by an EXE written by someone else. Both projects target .Net Framework 4.6.2. I rewrote one of my DLL methods to return a tuple and also imported the associated NuGet package.…
Mike Lowery
  • 2,630
  • 4
  • 34
  • 44
98
votes
11 answers

When to use: Tuple vs Class in C# 7.0

Before Tuples, I used to create a class and its variables, then create object from this class and make that object the return type for some functions. Now, with tuples, I can do the same thing, and in C# 7.0 we can assign understandable names for…
Madonna Remon
  • 1,149
  • 1
  • 7
  • 12
95
votes
10 answers

Does C# 7 have array/enumerable destructuring?

In JavaScript ES6, you are able to destructure arrays like this: const [a,b,...rest] = someArray; where a is the first element in the array, b is the second, and rest is an array with the remaining elements. I know in C#7 that you can destructure…
kmc059000
  • 2,937
  • 1
  • 23
  • 27
93
votes
4 answers

C# 7 tuples and lambdas

With new c# 7 tuple syntax, is it possible to specify a lambda with a tuple as parameter and use unpacked values inside the lambda? Example: var list = new List<(int,int)>(); normal way to use a tuple in lambda: list.Select(value => value.Item1*2 +…
Rast
  • 2,341
  • 4
  • 20
  • 29
93
votes
6 answers

C#7: Underscore ( _ ) & Star ( * ) in Out variable

I was reading about new out variable features in C#7 here. I have two questions: It says We allow "discards" as out parameters as well, in the form of a _, to let you ignore out parameters you don’t care about: p.GetCoordinates(out var x, out _);…
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
92
votes
3 answers

switch with var/null strange behavior

Given the following code: string someString = null; switch (someString) { case string s: Console.WriteLine("string s"); break; case var o: Console.WriteLine("var o"); break; default: …
budi
  • 6,351
  • 10
  • 55
  • 80
1
2 3
33 34