c#-8.0

C# 8 Using Declaration Scope Confusion

坚强是说给别人听的谎言 提交于 2020-04-13 06:36:47
问题 With the new C# 8 Using Declaration Syntax, what is containing scope of a second consecutive using statement? TL;DR Previous to C# 8, having a consecutive using statement like: using(var disposable = new MemoryStream()) { using(var secondDisposable = new StreamWriter(disposable)) {} } would expand to something like the following (My Source): MemoryStream disposable = new MemoryStream(); try { { StreamWriter secondDisposable = new StreamWriter(disposable); try{ {} } finally { if

C# 8 Using Declaration Scope Confusion

给你一囗甜甜゛ 提交于 2020-04-13 06:36:00
问题 With the new C# 8 Using Declaration Syntax, what is containing scope of a second consecutive using statement? TL;DR Previous to C# 8, having a consecutive using statement like: using(var disposable = new MemoryStream()) { using(var secondDisposable = new StreamWriter(disposable)) {} } would expand to something like the following (My Source): MemoryStream disposable = new MemoryStream(); try { { StreamWriter secondDisposable = new StreamWriter(disposable); try{ {} } finally { if

Can a non-nullable reference type in C# 8 be null in runtime?

喜欢而已 提交于 2020-04-10 07:27:07
问题 It seems to me there is really no guarantee that a non-nullable variable won't ever have null. Imagine I have a class that has one property that is not nullable: public class Foo { public Foo(string test) { Test = test; } public string Test {get;set;} } Now that might seem like it's now cannot be null. But if we reference this class with another library that does not use nullable context, nothing stops it from sending null in there. Is that correct or there are some runtime checks as well

What does exclamation mark mean before invoking a method in C# 8.0? [duplicate]

冷暖自知 提交于 2020-04-05 12:40:57
问题 This question already has answers here : Postfix ! (exclamation) operator in C# [duplicate] (1 answer) What does null! statement mean? (2 answers) Closed 4 months ago . I have found a code written in C# seemingly version 8.0. In the code, there is an exclamation mark before invoking a method. What does this part of the code mean, and above all, what are its uses? var foo = Entity!.DoSomething(); 回答1: This would be the null forgiving operator. It tells the compiler "this isn't null, trust me",

What happens with returning IEnumerable if used with async/await (streaming data from SQL Server with Dapper)?

五迷三道 提交于 2020-03-21 11:47:13
问题 I am using Dapper to stream data from a very large set in SQL Server. It works fine with returning IEnumerable and calling Query() , but when I switch to QueryAsync() , it seems that the program tries to read all of the data from SQL Server instead of streaming. According to this question, it should work fine with buffered: false , which I am doing, but the question says nothing about async/await . Now according to this question, it's not straightforward to do what I want with QueryAsync() .

.net core 3 yields different floating point results from version 2.2

旧街凉风 提交于 2020-03-16 07:28:46
问题 Here is a sample piece of code with outputs from .net core 2.2 and 3.1. It shows different computational results for a basic floating point expression a^b. In this example we calculate 1.9 to the power of 3. Previous .NET frameworks yielded the correct result, but .net core 3.0 and 3.1 yields a different result. Is this an intended change and how can we migrate financial calculation code to the new version with a guarantee that numerical calculations will still yield the same results? (It

How can I call the default method instead of the concrete implementation

痞子三分冷 提交于 2020-03-16 01:57:50
问题 Why is the behavior of Default Interface Methods changed in C# 8? In the past the following code (When the Default interface methods was demo not released): interface IDefaultInterfaceMethod { // By default, this method will be virtual, and the virtual keyword can be here used! virtual void DefaultMethod() { Console.WriteLine("I am a default method in the interface!"); } } interface IOverrideDefaultInterfaceMethod : IDefaultInterfaceMethod { void IDefaultInterfaceMethod.DefaultMethod() {

Possibility of external functions as nullable guards?

孤街浪徒 提交于 2020-03-09 19:50:30
问题 C# 8 introduced nullable reference types which is a very cool feature. Now if you expect to get nullable value you have to write so-called guards: object? value = null; if (value is null) { throw new ArgumentNullException(); } ... These can be a bit repetetive. What I am wondering is it possible to avoid writing this type of code for every variable but have a guard-type static void function that throws exception if value is null or just returns if value is not null. Or is this too hard for

Using IAsyncEnumerable with Dapper

好久不见. 提交于 2020-03-05 02:51:06
问题 We have recently migrated our ASP.NET Core API which uses Dapper to .NET Core 3.1. After the migration, we felt there was an opportunity to use the latest IAsyncEnumerable feature from C# 8 for one of our endpoints. Here is the pseudocode before the changes: public async Task<IEnumerable<Item>> GetItems(int id) { var reader = await _connection.QueryMultipleAsync(getItemsSql, param: new { Id = id }); var idFromDb = (await reader.ReadAsync<int?>().ConfigureAwait(false)).SingleOrDefault(); if

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

≯℡__Kan透↙ 提交于 2020-02-25 13:15:53
问题 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