default-interface-member

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

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

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

If default interface methods are implemented in C# 8.0 why would I ever need abstract classes? [closed]

别说谁变了你拦得住时间么 提交于 2019-12-19 02:47:11
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I recently ran into a list of features that are being considered for addition in the next version of C#. One of them is called "default interface methods": https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md In short, it will allow you to define

Calling C# interface default method from implementing struct without boxing

不想你离开。 提交于 2019-12-12 14:57:19
问题 The only thing I can think of is as follows, which is far from ideal: interface IBar { void Foo() => Console.WriteLine("Hello from interface!"); } struct Baz : IBar { // compiler error void Test1() => this.Foo(); // IIRC this will box void Test2() => ((IBar)this).Foo(); // this shouldn't box but is pretty complicated just to call a method void Test3() { impl(ref this); void impl<T>(ref T self) where T : IBar => self.Foo(); } } Is there a more straightforward way to do this? (Related and how I

Calling C# interface default method from implementing class

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 04:24:56
问题 C# 8 supports default method implementations in interfaces. My idea was to inject a logging method into classes like this: public interface ILoggable { void Log(string message) => DoSomethingWith(message); } public class MyClass : ILoggable { void MyMethod() { Log("Using injected logging"); // COMPILER ERROR } } I get a compiler error: "The name does not exist in the current context" Is it impossible to use default method implementations in this way? EDIT: For the correct response regarding C

Default Interface Implementations. What is deep meaningful difference now, between abstract class and interface?

跟風遠走 提交于 2019-11-30 09:17:35
I know that an abstract class is a special kind of class that cannot be instantiated. An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards. Also I know that An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature

Default Interface Methods. What is deep meaningful difference now, between abstract class and interface?

烂漫一生 提交于 2019-11-27 02:45:56
问题 I know that an abstract class is a special kind of class that cannot be instantiated. An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards. Also I know that An interface is not a class. It is an

How would you implement a “trait” design-pattern in C#?

橙三吉。 提交于 2019-11-26 13:00:34
问题 I know the feature doesn\'t exist in C#, but PHP recently added a feature called Traits which I thought was a bit silly at first until I started thinking about it. Say I have a base class called Client . Client has a single property called Name . Now I\'m developing a re-usable application that will be used by many different customers. All customers agree that a client should have a name, hence it being in the base-class. Now Customer A comes along and says he also need to track the client\'s