c#-8.0

Can you use IAsyncEnumerable in Razor pages to progressively display markup?

六月ゝ 毕业季﹏ 提交于 2020-06-10 16:54:50
问题 I've been playing around with Blazor and the IAsyncEnumerable feature in C# 8.0. Is it possible to use IAsyncEnumerable and await within Razor Pages to progressively display markup with data? Example service: private static readonly string[] games = new[] { "Call of Duty", "Legend of Zelda", "Super Mario 64" }; public async IAsyncEnumerable<string> GetGames() { foreach (var game in games) { await Task.Delay(1000); yield return game; } } Example in razor page: @await foreach(var game in

Can you use IAsyncEnumerable in Razor pages to progressively display markup?

社会主义新天地 提交于 2020-06-10 16:49:28
问题 I've been playing around with Blazor and the IAsyncEnumerable feature in C# 8.0. Is it possible to use IAsyncEnumerable and await within Razor Pages to progressively display markup with data? Example service: private static readonly string[] games = new[] { "Call of Duty", "Legend of Zelda", "Super Mario 64" }; public async IAsyncEnumerable<string> GetGames() { foreach (var game in games) { await Task.Delay(1000); yield return game; } } Example in razor page: @await foreach(var game in

C# 8 base interface's default method invocation workaround

泪湿孤枕 提交于 2020-06-10 16:06:09
问题 According to https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods It is possible to explicitly invoke an interface base implementation with the following syntax. base(IInterfaceType).Method(); But this doesn't seem to be implemented yet. Is there a workaround (e.g reflection) to achieve this? Example code to illustrate the problem interface IA { void M() { Console.WriteLine("IA.M"); } } interface IB : IA { void IA.M() { Console

C# 8 base interface's default method invocation workaround

旧城冷巷雨未停 提交于 2020-06-10 16:03:29
问题 According to https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods It is possible to explicitly invoke an interface base implementation with the following syntax. base(IInterfaceType).Method(); But this doesn't seem to be implemented yet. Is there a workaround (e.g reflection) to achieve this? Example code to illustrate the problem interface IA { void M() { Console.WriteLine("IA.M"); } } interface IB : IA { void IA.M() { Console

Nullable Reference Types and the Options Pattern

邮差的信 提交于 2020-06-10 02:32:48
问题 How can we use non-nullable reference types in combination with the Options pattern? Let's say we have an options model named MyOptions . The services requiring those options are getting IOptions<MyOptions> options injected into the constructor. Configuring the options happens on the IServiceCollection like this: services .AddOptions<MyOptions>() .Configure(options => { options.Name = "ABC"; }); Now, the problem is in the definition of MyOptions : public sealed class MyOptions { public string

Nullable Reference Types and the Options Pattern

不羁岁月 提交于 2020-06-10 02:31:22
问题 How can we use non-nullable reference types in combination with the Options pattern? Let's say we have an options model named MyOptions . The services requiring those options are getting IOptions<MyOptions> options injected into the constructor. Configuring the options happens on the IServiceCollection like this: services .AddOptions<MyOptions>() .Configure(options => { options.Name = "ABC"; }); Now, the problem is in the definition of MyOptions : public sealed class MyOptions { public string

Can't get C# default interface method to compile

自古美人都是妖i 提交于 2020-05-24 02:05:29
问题 C# 8.0 has a new feature that lets you add a default implementation to a method on an interface. Either I'm doing something wrong or this feature doesn't work as advertised. (I'm guessing it's the former.) I created a new .NET Core 3.1 console app with the following code: using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var xxx = new MyClass { MyInt = 5 }; Console.WriteLine(xxx.GetItNow()); } } public interface ITest { int MyInt { get; set; } int

Return IAsyncEnumerable from an async method

橙三吉。 提交于 2020-05-15 09:23:05
问题 Take the following the methods: public async IAsyncEnumerable<int> Foo() { await SomeAsyncMethod(); return Bar(); // Throws since you can not return values from iterators } public async IAsyncEnumerable<int> Bar() { for(int i = 0; i < 10; i++) { await Task.Delay(100); yield return i; } } I wonder what the best practice would be, to do, what the code above tries to. Basically returning an IAsyncEnumerable from an async method. For myself I can imagine two ways: Iterating over the

Nullable reference type information not exposed from FirstOrDefault

让人想犯罪 __ 提交于 2020-04-27 05:59:42
问题 I wanted to test out the new nullable reference types feature in C# 8.0. I started a new project targeting .NET Core 3.0, enabled nullable reference types in the .csproj file, and started coding. I created a simple list that takes a string[] and returns the string in that array that equals abc . Now, because I am not certain that abc actually exists in the array, I use FirstOrDefault() , which should default to null if a match is not found. using System; using System.Linq; public string

C# 8 switch expression: Handle multiple cases at once?

℡╲_俬逩灬. 提交于 2020-04-16 19:27:10
问题 C# 8 introduced pattern matching, and I already found good places to use it, like this one: private static GameType UpdateGameType(GameType gameType) { switch (gameType) { case GameType.RoyalBattleLegacy: case GameType.RoyalBattleNew: return GameType.RoyalBattle; case GameType.FfaLegacy: case GameType.FfaNew: return GameType.Ffa; default: return gameType; } } which then becomes private static GameType UpdateGameType(GameType gameType) => gameType switch { GameType.RoyalBattleLegacy =>