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.
c#-7.0
510
questions
590
votes
3
answers
170k
views
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 ...
431
votes
10
answers
144k
views
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) ...
233
votes
5
answers
39k
views
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 ...
188
votes
6
answers
66k
views
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....
155
votes
2
answers
81k
views
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?
123
votes
2
answers
25k
views
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<System.Guid, string, string>;
However, is it possible to define one using the ...
113
votes
4
answers
35k
views
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
...
107
votes
2
answers
5k
views
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 ...
106
votes
3
answers
49k
views
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 ...
105
votes
22
answers
138k
views
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 ...
101
votes
11
answers
47k
views
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 ...
98
votes
10
answers
46k
views
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 ...
97
votes
4
answers
29k
views
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 ...
95
votes
6
answers
49k
views
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’...
92
votes
3
answers
4k
views
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");
...
90
votes
3
answers
36k
views
Deconstruction in foreach over Dictionary
Is it possible in C#7 to use deconstruction in a foreach-loop over a Dictionary? Something like this:
var dic = new Dictionary<string, int>{ ["Bob"] = 32, ["Alice"] = 17 };
foreach (var (name, ...
80
votes
3
answers
121k
views
How to return multiple values in C# 7? [closed]
Is it is possible to return multiple values from a method natively?
80
votes
10
answers
38k
views
How to null check c# 7 tuple in LINQ query?
Given:
class Program
{
private static readonly List<(int a, int b, int c)> Map = new List<(int a, int b, int c)>()
{
(1, 1, 2),
(1, 2, 3),
(2, 2, 4)
};
...
69
votes
2
answers
45k
views
Does C# 7.0 work for .NET 4.5?
I created a project in Visual Studio 2017 RC to check whether I can use new C# 7.0 language features in a .NET Framework 4.5 project. It seems to me that after referencing System.ValueTuple NuGet, new ...
66
votes
7
answers
37k
views
Discard feature significance in C# 7.0?
While going through new C# 7.0 features, I stuck up with discard feature. It says:
Discards are local variables which you can assign but cannot read
from. i.e. they are “write-only” local variables.
...
60
votes
6
answers
32k
views
Enabling c# 7 in a asp.net application
I just started working on my old solution in Visual Studio 2017. Just opening the solution in the old IDE worked seamlessly.
The c# application projects now default to the c# 7.0 compiler. The ...
58
votes
6
answers
33k
views
In C# 7 is it possible to deconstruct tuples as method arguments
For example I have
private void test(Action<ValueTuple<string, int>> fn)
{
fn(("hello", 10));
}
test(t =>
{
var (s, i) = t;
Console.WriteLine(s);
Console.WriteLine(i)...
58
votes
3
answers
31k
views
How can I enable all features of C# 7 in Visual Studio 2017 project?
After Visual Studio 2017 was released I wanted to try to create simple console project with new C# 7 features. I expected that I simply download new Visual Studio 2017, then create new console project ...
54
votes
3
answers
23k
views
Inline variable declaration not compiling
I've been getting a message in Visual Studio 2017, specifically, IDE0018 Variable declaration can be inlined.
So I try using an inline variable declaration the way it's mentioned in the visual studio ...
51
votes
3
answers
11k
views
Getting "Tuple element name is inferred. Please use language version 7.1 or greater to access an element by its inferred name."
We have the following code that has been working fine in our UWP app until today after we updated Visual Studio 2017 to the latest 15.3.
private void Test()
{
var groups = new List<(Guid key, ...
50
votes
2
answers
17k
views
C# 7 .NET / CLR / Visual Studio version requirements
What are the minimum .NET framework and CLR version requirements for running C# 7? Also, do I need VS 2017 to compile C# 7?
49
votes
1
answer
24k
views
C#7 tuple & async
Old format:
private async Task<Tuple<SomeArray[], AnotherArray[], decimal>>
GetInvoiceDetailAsync(InvoiceHead invoiceHead) { ... }
How can you do that in C#7 with new tuples ...
43
votes
4
answers
47k
views
How to create a List of ValueTuple?
Is it possible to create a list of ValueTuple in C# 7?
like this:
List<(int example, string descrpt)> Method()
{
return Something;
}
41
votes
2
answers
17k
views
C# 7.0 Value Tuple compile error?
When I am trying to compile the following code:
var post = iPostService.GetAll().Select(x => (x.Title, x.Author));
I get the compiler error: 'An expression tree may not contain a tuple literal.'
...
41
votes
2
answers
6k
views
Local Functions in C# - to capture or not to capture when passing parameters down?
When using Local Functions in C# 7 you have two options when you want to pass parameters (or other local variables) from the main method down to the local function: You can either explicitly declare ...
40
votes
3
answers
8k
views
ValueTuple naming conventions
When naming a ValueTuple element, should they be capitalized or not?
(string Name, int Index) rec;
or
(string name, int index) rec;
EDIT:
It seems the convention is PascalCase
See: https://github....
39
votes
5
answers
34k
views
Convert anonymous type to new C# 7 tuple type
The new version of C# is there, with the useful new feature Tuple Types:
public IQueryable<T> Query<T>();
public (int id, string name) GetSomeInfo() {
var obj = Query<SomeType>(...
39
votes
3
answers
19k
views
Expression of type T cannot be handled by a pattern of type X
I have upgraded my project to target C# 7 and used Visual Studio 2017 RC to implement pattern matching across my solution. After doing this some errors were introduced relating to pattern matching ...
38
votes
1
answer
5k
views
C# 7 local function not working as expected and no errors being displayed
I have an Asp.Net MVC App running with framework version .NET 4.5 and I'm using VS2017 pro version. Users can upload attachments including but not limited to:
Excel
Word
PowerPoint
pdf
jpeg
png
So ...
37
votes
3
answers
5k
views
What is the idiomatic naming convention for local functions in C# 7 [closed]
Normal class methods, whether instance or static, have an idiomatic naming convention with regards to their casing. It's not clear that there is a convention for local functions, introduced in C# 7.
...
36
votes
1
answer
6k
views
In C#7, how can I "roll my own" Task-like type to use with async?
One of the less-talked-about features of C#7 is "generalized async return types", which is described by Microsoft as:
Returning a Task object from async methods can introduce performance bottlenecks ...
35
votes
1
answer
13k
views
C# 7 Expression Bodied Constructors
In C# 7, how do I write an Expression Bodied Constructor like this using 2 parameters.
public Person(string name, int age)
{
Name = name;
Age = age;
}
35
votes
2
answers
46k
views
Return multiple values from a C# asynchronous method
I have worked with asynchronous methods and the methods which return multiple values, separately. In this specific situation, following "GetTaskTypeAndId()" method should be asynchronous and should ...
35
votes
4
answers
8k
views
Why does pattern matching on a nullable result in syntax errors?
I like to use pattern-matching on a nullable int i.e. int?:
int t = 42;
object tobj = t;
if (tobj is int? i)
{
System.Console.WriteLine($"It is a nullable int of value {i}");
}
However, ...
34
votes
4
answers
10k
views
C# 7 ValueTuple compile error
I'm using VS2017 RC and my application targets net framework 4.6.1.
I have two assemblies referencing System.ValueTuple 4.3
MyProject.Services
MyProject.WebApi
In MyProject.Services I have a class ...
33
votes
2
answers
20k
views
Using a C# 7 tuple in an ASP.NET Core Web API Controller
Do you know why this works:
public struct UserNameAndPassword
{
public string username;
public string password;
}
[HttpPost]
public IActionResult Create([FromBody]UserNameAndPassword ...
33
votes
3
answers
8k
views
Does C# 7 allow to deconstruct tuples in linq expressions
I'm trying to deconstruct a tuple inside a Linq expression
// somewhere inside another method
var result = from word in words
let (original, translation) = Convert(word)
...
33
votes
5
answers
21k
views
Name ValueTuple properties when creating with new
I know I can name parameters when I create a tuple implicitly like:
var me = (age: 21, favoriteFood: "Custard");
Is it possible to name parameters when a tuple is created explicitly? i.e.
var me = ...
33
votes
2
answers
6k
views
C# 7.0 ValueTuples vs Anonymous Types
Looking at the new C# 7.0 ValueTuples, I am wondering if they will completely replace Anonymous Types. I understand that ValueTuples are structs and therefore behave a bit differently than Anonymous ...
32
votes
2
answers
17k
views
Using named tuples in select statements
Is there a nicer way to select a named tuple in C# 7 using a var target variable? I must be doing something wrong in example 1, or misunderstanding something completely. I seem to have to explicitly ...
31
votes
6
answers
26k
views
How to declare a C# Record Type?
I read on a blog that C# 7 will feature record types
class studentInfo(string StudentFName, string StudentMName, string StudentLName);
However when I tried it, I get these errors
CS0116 A ...
31
votes
4
answers
3k
views
What's the benefit of var patterns in C#7?
I don't understand the use case of var patterns in C#7. MSDN:
A pattern match with the var pattern always succeeds. Its syntax is
expr is var varname
where the value of expr is always assigned ...
29
votes
2
answers
3k
views
C# 7 Compiler Error - Pattern Matching
For some reason, M1() causes a compiler error, while M2(), which does the same thing, causes no error. Any idea why?
Using false == should be the same as using the not operator, !.
Use of ...
27
votes
2
answers
7k
views
Private methods vs local functions
To my understanding, both local functions and private methods serve merely as implementation details - helpers for public methods. Why would I want to choose one over the other?
When using a private ...
25
votes
7
answers
30k
views
Invalid option '7.3' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6
I'm using Visual Studio 17 (version 15.8.5), my project targets .NET Framework 4.8 and I've tried setting the C# version to use (via Build tab in the Properties window) C# 7.3 (that's the maximum ...