c#-8.0

Short cut for a check for null and assigning a value to it if it is?

爱⌒轻易说出口 提交于 2020-02-25 13:15:15
问题 With the new C# 8 capabilities is there a short cut now for this code structure: if (App.selectedPhrases == null) App.selectedPhrases = App.DB.GetSelectedPhrases(); 回答1: Yes, it is called Null-coalescing assignment: App.selectedPhrases ??= App.DB.GetSelectedPhrases(); C# 8.0 introduces the null-coalescing assignment operator ??=. You can use the ??= operator to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. 回答2: App

Why can I declare a child variable with the same name as a variable in the parent scope?

纵然是瞬间 提交于 2020-02-23 09:57:49
问题 I wrote some code recently where I unintentionally reused a variable name as a parameter of an action declared within a function that already has a variable of the same name. For example: var x = 1; Action<int> myAction = (x) => { Console.WriteLine(x); }; When I spotted the duplication, I was surprised to see that the code compiled and ran perfectly, which is not behavior I would expect based on what I know about scope in C#. Some quick Googling turned up SO questions that complain that

Why can I declare a child variable with the same name as a variable in the parent scope?

北战南征 提交于 2020-02-15 06:54:01
问题 I wrote some code recently where I unintentionally reused a variable name as a parameter of an action declared within a function that already has a variable of the same name. For example: var x = 1; Action<int> myAction = (x) => { Console.WriteLine(x); }; When I spotted the duplication, I was surprised to see that the code compiled and ran perfectly, which is not behavior I would expect based on what I know about scope in C#. Some quick Googling turned up SO questions that complain that

Why can I declare a child variable with the same name as a variable in the parent scope?

筅森魡賤 提交于 2020-02-15 06:52:30
问题 I wrote some code recently where I unintentionally reused a variable name as a parameter of an action declared within a function that already has a variable of the same name. For example: var x = 1; Action<int> myAction = (x) => { Console.WriteLine(x); }; When I spotted the duplication, I was surprised to see that the code compiled and ran perfectly, which is not behavior I would expect based on what I know about scope in C#. Some quick Googling turned up SO questions that complain that

Using Linq's Where/Select to filter out null and convert the type to non-nullable cannot be made into an extension method

瘦欲@ 提交于 2020-02-05 03:54:43
问题 Suppose I have List<MyObject?> list = ...; I want to turn it into List<MyObject> , but I have not been able to drop the nullable reference. Below is an MCVE. In my project I have nullable reference warnings turned to errors, so the commented out line below will not compile. If I do .Where(e => e != null).Select(e => e!) then it will be fine in the latest .NET Core 3.1.100, however I cannot extract this into an extension method. I tried adding this extension method public static IEnumerable<T>

When to null-check arguments with nullable reference types enabled

北战南征 提交于 2020-02-01 00:34:44
问题 Given a function in a program using C# 8.0's nullable reference types feature, should I still be performing null checks on the arguments? void Foo(string s, object o) { if (s == null) throw new ArgumentNullException(nameof(s)); // Do I need these? if (o == null) throw new ArgumentNullException(nameof(o)); ... } None of the code is part of a public API so I suspect that these checks may be redundant. The two parameters are not marked as nullable, so the compiler should warn if any calling code

Receiving error about nullable type parameter even when parameter has notnull constraint

安稳与你 提交于 2020-01-24 16:38:48
问题 I have a generic interface IDataAdapter<T> ; implementors of the interface should be able to read POCOs with Guid IDs from a data source. IDataAdapter<T> has a method Read(Guid id) which I want to return a T? , where null indicates that no matches were found in the data source. However, even with a constraint T : notnull on IDataAdapter<T> , attempting to define this method gives the error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type.

Receiving error about nullable type parameter even when parameter has notnull constraint

泄露秘密 提交于 2020-01-24 16:38:07
问题 I have a generic interface IDataAdapter<T> ; implementors of the interface should be able to read POCOs with Guid IDs from a data source. IDataAdapter<T> has a method Read(Guid id) which I want to return a T? , where null indicates that no matches were found in the data source. However, even with a constraint T : notnull on IDataAdapter<T> , attempting to define this method gives the error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type.

Receiving error about nullable type parameter even when parameter has notnull constraint

送分小仙女□ 提交于 2020-01-24 16:38:06
问题 I have a generic interface IDataAdapter<T> ; implementors of the interface should be able to read POCOs with Guid IDs from a data source. IDataAdapter<T> has a method Read(Guid id) which I want to return a T? , where null indicates that no matches were found in the data source. However, even with a constraint T : notnull on IDataAdapter<T> , attempting to define this method gives the error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type.

How to implement an efficient WhenEach that streams an IAsyncEnumerable of task results?

百般思念 提交于 2020-01-24 09:35:06
问题 I am trying to update my toolset with the new tools offered by C# 8, and one method that seems particularly useful is a version of Task.WhenAll that returns an IAsyncEnumerable. This method should stream the task results as soon as they become available, so naming it WhenAll doesn't make much sense. WhenEach sounds more appropriate. The signature of the method is: public static IAsyncEnumerable<TResult> WhenEach<TResult>(Task<TResult>[] tasks); This method could be used like this: var tasks =