c#-8.0

Making member virtual prevents calling default interface implementation and causes StackOverflowException in C# 8

夙愿已清 提交于 2020-01-23 08:02:14
问题 Consider the code: class ChildClass : BaseClass { public void Method1() {} //some other method } abstract class BaseClass : IChildInterface { public virtual //<- If we add virtual so that this method can be overridden by ChildClass, we get StackOverflowException and DoWork() implementation in IChildInterface is never called. void DoWork() { //base class specific implmentation ((IChildInterface)this).DoWork(); //call into default implementation provided by IChildInterface } } interface

Making member virtual prevents calling default interface implementation and causes StackOverflowException in C# 8

丶灬走出姿态 提交于 2020-01-23 07:58:08
问题 Consider the code: class ChildClass : BaseClass { public void Method1() {} //some other method } abstract class BaseClass : IChildInterface { public virtual //<- If we add virtual so that this method can be overridden by ChildClass, we get StackOverflowException and DoWork() implementation in IChildInterface is never called. void DoWork() { //base class specific implmentation ((IChildInterface)this).DoWork(); //call into default implementation provided by IChildInterface } } interface

Create empty IAsyncEnumerable

你说的曾经没有我的故事 提交于 2020-01-23 04:18:06
问题 I have an interface which is written like this: public interface IItemRetriever { public IAsyncEnumerable<string> GetItemsAsync(); } I want to write an empty implementation that returns no item, like so: public class EmptyItemRetriever : IItemRetriever { public IAsyncEnumerable<string> GetItemsAsync() { // What do I put here if nothing is to be done? } } If it was a plain IEnumerable, I would return Enumerable.Empty<string>(); , but I didn't find any AsyncEnumerable.Empty<string>() .

What is Range and Index types in c# 8?

倾然丶 夕夏残阳落幕 提交于 2020-01-21 06:51:27
问题 In c# 8 add two new types to System namespace as System.Index and System.Range How work this classes and When can use them? 回答1: They're used for indexing and slicing. From Microsoft's blog: Indexing: Index i1 = 3; // number 3 from beginning Index i2 = ^4; // number 4 from end int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Console.WriteLine($"{a[i1]}, {a[i2]}"); // "3, 6" Range (slicing): We’re also introducing a Range type, which consists of two Indexes, one for the start and one for the end,

Nullable reference types: How to specify “T?” type without constraining to class or struct

夙愿已清 提交于 2020-01-19 09:27:38
问题 I want to create a generic class that has a member of type T . T may be a class, a nullable class, a struct, or a nullable struct. So basically anything. This is a simplified example that shows my problem: #nullable enable class Box<T> { public T Value { get; } public Box(T value) { Value = value; } public static Box<T> CreateDefault() => new Box<T>(default(T)); } Due to using the new #nullable enable feature I get the following warning: Program.cs(11,23): warning CS8653: A default expression

Nullable reference types: How to specify “T?” type without constraining to class or struct

十年热恋 提交于 2020-01-19 09:20:55
问题 I want to create a generic class that has a member of type T . T may be a class, a nullable class, a struct, or a nullable struct. So basically anything. This is a simplified example that shows my problem: #nullable enable class Box<T> { public T Value { get; } public Box(T value) { Value = value; } public static Box<T> CreateDefault() => new Box<T>(default(T)); } Due to using the new #nullable enable feature I get the following warning: Program.cs(11,23): warning CS8653: A default expression

Why C#8 Default implementations of interface members will report an error

前提是你 提交于 2020-01-15 12:36:07
问题 Why C#8 Default implementations of interface members will report an error? public interface Logger { void Info(string message); void Error(string message); // C#8 Default implementations of interface void Warn(string message) { // "interface method cannot declare a body" error message } } and .NET Core 3.0 is configured as shown in the screenshot. 回答1: This is a Resharper/Rider bug: https://youtrack.jetbrains.com/issue/RSRP-474628 回答2: The feature is sound and your setup is correct. Also the

public interface member in c#8

限于喜欢 提交于 2020-01-15 03:10:51
问题 Since the latest version of c#, it is possible to write the following interface: public interface IMyInterface { public void MyMethod(); } This seems like a code smell to me, as I feel like the intention was to write the previously available: public interface IMyInterface { void MyMethod(); } Are those two interfaces exactly the same ? Does the public keyword add/changes anything ? Is this something that should be corrected, or am I wrong and should public be consistently used now ? 回答1:

Default implementation in interface is not seen by the compiler?

天大地大妈咪最大 提交于 2020-01-10 05:18:05
问题 Here is a my code inside a c# project that targets .NET Core 3.0 (so I should be in C# 8.0) with Visual Studio 2019 (16.3.9) public interface IJsonAble { public string ToJson() => System.Text.Json.JsonSerializer.Serialize(this); } public class SumRequest : IJsonAble { public int X { get; set; } public int Y { get; set; } public void Tmp() { new SumRequest().ToJson(); //compile error } } The compile error is: CS1061 'SumRequest' does not contain a definition for 'ToJson' and no accessible

Is there a way for switch to return a string value using C# 8 switch expressions?

我与影子孤独终老i 提交于 2020-01-05 08:27:36
问题 I have this code where each part of the switch returns a value to ModeMessage2 . Is it possible using the new C# switch expressions (or any other code optimization) to optimize the way this switch works? switch (Settings.Mode) { case MO.Learn: ModeMessage2 = "Use this mode when you are first learning the phrases and their meanings."; if (Settings.Cc == CC.H) { Settings.Cc = CC.JLPT5; App.cardSetWithWordCount = null; App.DB.RemoveSelected(); } break; case MO.Practice: ModeMessage2 = "Use this