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.
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…
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…
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…
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
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…
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)
…
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…
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…
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.…
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…
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…
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 +…
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 _);…
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:
…