c#-7.0

How to await for one task on two different threads?

久未见 提交于 2019-12-11 03:24:57
问题 Can I await on a Task that was created on a different thread? For example: ... CurrentIteration = Execute(); // returns Task await CurrentIteration; ... And then, on another thread: ... await CurrentIteration; ... Will the second thread wait for method Execute to finish executing? If it will, will I be able to re-use CurrentIteration for the same purpose in the second thread, given that I re-run CurrentIteration = Execute(); // returns Task await CurrentIteration; On the first thread? I tried

How to enable C# 7 features in existing projects

孤者浪人 提交于 2019-12-11 01:27:53
问题 I am not sure how to use new C# 7 features in existing solution. I tried using pattern matching in a switch statement but I keep getting Value of integral type expected error. Is there a trick to enable it? I though I can just use new features if I open the solution in VS 2017. My projects are targeting .net 4.6.2. Here is the sample code private void CS7Test(object o) { switch (o) { case null: Console.WriteLine("it's a constant pattern"); break; case int i: Console.WriteLine("it's an int");

Enabling c# 7 compilation in Project Rider

你。 提交于 2019-12-10 19:55:08
问题 If I set the LangVersion to 7, I get the following error: Error:CS1617: Invalid option '7' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6. I can't install Visual Studio 2017. 回答1: Unfortunately for now it is not possible on mac/linux, but it is planned - around Q1 2017. Source: https://rider-support.jetbrains.com/hc/en-us/community/posts/205987804-Rider-and-C-7- 来源: https://stackoverflow.com/questions/42830694/enabling-c-sharp-7-compilation-in-project-rider

Named Parameters in ValueTuple.Create

余生颓废 提交于 2019-12-10 16:49:34
问题 I was playing arround with the Value Tuple in C#. First some demo data: #region Data public class Product { public string Name { get; set; } public int CategoryID { get; set; } } public class Category { public string Name { get; set; } public int ID { get; set; } } public class Data { public List<Category> Categories { get; } = new List<Category>() { new Category(){Name="Beverages", ID=001}, new Category(){ Name="Condiments", ID=002}, }; public List<Product> Products { get; } = new List

Pattern Matching on a string

走远了吗. 提交于 2019-12-10 14:36:44
问题 I was wondering if there is a way to do something like this in c# 7 var test = "aaeag"; switch (test) { case test.StartsWith("a"): break; default: break; } Sadly it does not look like it possible. Is this correct or am I doing something wrong? 回答1: This is possible with C# 7, using a when guard: var test = "aaeag"; switch (test) { case var s when s.StartsWith("a"): break; default: break; } What your version of the code is doing is often referred to as active patterns . By eg defining the the

Generic functions and ref returns in C# 7.0

China☆狼群 提交于 2019-12-10 13:09:53
问题 Is it possible to use the ref returns feature in C# 7.0 define a generic function that can do both comparison and update of a field in two instances of an Object? I am imagining something like this: void UpdateIfChanged<TClass, TField>(TClass c1, TClass c2, Func<TClass, TField> getter) { if (!getter(c1).Equals(getter(c2)) { getter(c1) = getter(c2); } } Example intended usage: Thing thing1 = new Thing(field1: 0, field2: "foo"); Thing thing2 = new Thing(field1: -5, field2: "foo");

C# ValueTuple properties naming

房东的猫 提交于 2019-12-10 04:08:21
问题 I'm trying ValueTuple Class in C#, and i have a doubt about properties naming, let's see: If instantiate a ValueTuple declaring the object like this: var tuple1 = (Name: "Name1", Age: 25); We can name the properties, but, like this: ValueTuple<string,int> tuple2 = (Name: "Name1", Age: 25); we get a warning that says the names are ignored,so We should type: ValueTuple<string,int> tuple2 = ("Name1",25); Then the properties will be tuple2.Item1 and tuple2.Item2 Can someone explain this newbie

Pattern matching equal null vs is null

旧城冷巷雨未停 提交于 2019-12-09 11:25:51
问题 From Microsoft new-features-in-c-7-0: public void PrintStars(object o) { if (o is null) return; // constant pattern "null" if (!(o is int i)) return; // type pattern "int i" WriteLine(new string('*', i)); } Whats the diferrence of o == null and o is null ? 回答1: The o is null is translated to object.Equals(null, o) (you can see it here). The object.Equals code is written as: public static bool Equals(Object objA, Object objB) { if (objA == objB) { return true; } if (objA == null || objB ==

C# 7 switch case with null checks

淺唱寂寞╮ 提交于 2019-12-08 21:46:01
问题 C#7 introduces a new feature called patterns , which you can use with Is-Expression or Switch cases like this: string str = null; switch(str){ case string x: Console.WriteLine("string " + x); break; default: Console.WriteLine("default"); break; } and you would expect that it will goes inside case #1, as it is the same type, but it didn't. 回答1: Despite what you might think, string x = null actually isn't a string at all. It is 'nothing', assigned to a variable of type string. The check in your

Getting “Tuple element name is inferred. Please use language version 7.1 or greater to access an element by its inferred name.”

六眼飞鱼酱① 提交于 2019-12-08 14:25:59
问题 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, IList<(string, bool)> items)>(); var items = new List<(string, bool)> { ("a", true), ("b", false), ("c", false) }; var group = (Guid.NewGuid(), items); groups.Add(group); } There is no error message but this in the output window Tuple element name 'items' is inferred. Please use language version 7.1 or