c#-7.0

In C#7, how can I “roll my own” Task-like type to use with async?

瘦欲@ 提交于 2019-12-17 23:37:12
问题 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 in certain paths. Task is a reference type, so using it means allocating an object. In cases where a method declared with the async modifier returns a cached result, or completes synchronously, the extra allocations can become a significant time cost in performance critical sections of code. It

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

送分小仙女□ 提交于 2019-12-17 17:59:30
问题 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) { (int sum, int count) a = ReturnTwoValues(); } static (int sum, int count) ReturnTwoValues() => (1, 1); } Compiler is generating an error: Error CS8137 Cannot define a class or member that utilizes tuples because the compiler required type 'System.Runtime

TryParse with out var param

允我心安 提交于 2019-12-17 15:55:09
问题 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 .NET framework 4.6 are set. 回答1: A new feature in C# 6.0 allows to declare variable inside TryParse method. Declaration expressions was cut from C# 6.0 and wasn't shipped in the final release. You currently can't do that. There is a proposal for it on

C# 7 Expression Bodied Constructors

本秂侑毒 提交于 2019-12-17 10:58:47
问题 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; } 回答1: A way to do this is to use a tuple and a deconstruction to allow multiple assignments in one expression: public class Person { public string Name { get; } public int Age { get; } public Person(string name, int age) => (Name, Age) = (name, age); } As of C# 7.1 (introduced with Visual Studio 2017 Update 3), the compiler code will now

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

大憨熊 提交于 2019-12-17 02:28:29
问题 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 get the error: Predefined type 'System.ValueTuple´2´ is not defined or imported According to the blog post, this features should be "on" by default. What did I do wrong? 回答1: For .NET 4.6.2 or lower, .NET Core 1.x, and .NET Standard 1.x you need to install the

How to use C# 7 with Visual Studio 2015?

女生的网名这么多〃 提交于 2019-12-16 20:13:38
问题 Visual Studio 2017 (15.x) supports C# 7, but what about Visual Studio 2015 (14.x)? How can I use C# 7 with it? 回答1: You can replace the compiler shipped with Visual Studio for a C# 7-enabled version by installing the Nuget package Microsoft.Net.Compilers: Referencing this package will cause the project to be built using the specific version of the C# and Visual Basic compilers contained in the package, as opposed to any system installed version. There is no indication that I can see on the

Is a takewhile() checked every iteration using something like yeild, or does it just grab a set of elements all at once?

一个人想着一个人 提交于 2019-12-13 22:22:00
问题 for instance, let's say I want to do something like this: bool foo(List<strings> stringList, int counter)//assume this list has, like, 10 elements, and counter=3, idk { bool found= false; for(int i=0; i<stringlist.Count && !found; i++) { if(stringlist[i].length < 2 || counter >=6) found=true; counter++; } return found } Now, Is that equivelent to this: bool foo(List<strings> stringList, int counter)//assume this list has, like, 10 elements, and counter=3, idk { bool found= false; foreach

Is it possible to deconstruct out ValueTuple parameters?

白昼怎懂夜的黑 提交于 2019-12-12 10:31:21
问题 Is it possible to deconstruct a tuple which isn't returned from a method, but is an out parameter? I'm not sure I'm expressing myself correctly or even using the right terms, so here's some examples: void OutMethod(out (int aNumber, string someText) output) => output = (15, "yo"); void Usage() { { // Works, of course. OutMethod(out var tuple); // But *slightly* aesthetically unappealing to use. var usage = $"{tuple.someText}: {tuple.aNumber}"; } { // Would be awesome, but doesn't work.

Passing List<T> by reference to a method accepting ref ICollection<T> [duplicate]

给你一囗甜甜゛ 提交于 2019-12-11 16:58:27
问题 This question already has answers here : error when passing a reference to a derived object in a method (3 answers) Closed last year . I've noticed that a List<T> cannot be passed to methods such as: Foo<T>(ref ICollection<T> bar) Foo<T>(out ICollection<T> bar) Taking into account that, on the other side, Foo<T>(ICollection<T> bar) does accept a List<T> as argument, could anyone please explain the described behavior to me? Complete example: public class AuxClass { } public class Test {

C# Interactive not recognizing ValueTuple reference

最后都变了- 提交于 2019-12-11 07:58:38
问题 I cannot use tuples in the C# Interactive window; I get the following error: (1,15): error CS8137: Cannot define a class or member that utilizes tuples because the compiler required type 'System.Runtime.CompilerServices.TupleElementNamesAttribute' cannot be found. Are you missing a reference? (1,15): error CS8179: Predefined type 'System.ValueTuple`2' is not defined or imported (2,9): error CS0103: The name 'IsLiteralTypeName' does not exist in the current context (2,47): error CS8179: