1

I'm creating a class that implements an interface found in aws-cdk library but my class is not being forced to implement the properties defined on the interface. I'm very confused about how this is even possible but would really like to find a solution to force my class to implement the interface. Any ideas?

namespace MyProject
{
    /// <summary>
    /// MyClass
    /// </summary>
    public class MyClass : Amazon.CDK.AWS.Lambda.IFunctionOptions
    {
    }
}

enter image description here

cal5barton
  • 1,606
  • 1
  • 14
  • 29
  • Either that won't compile *or* the `IFunctionOptions` type being referenced in screenshot 1 is not the same one you are looking at there in your 2nd screenshot. – Igor Jul 02 '20 at 20:50
  • Please review [MCVE] guidance on posting code and [edit] the question to include code as text that demonstrates the problem. – Alexei Levenkov Jul 02 '20 at 20:55
  • @Igor it does compile and it is in fact the same interface. That's why I'm so confused on why it's compiling. – cal5barton Jul 02 '20 at 21:15
  • @AlexeiLevenkov this is the minimum amount of code to reproduce it. – cal5barton Jul 02 '20 at 21:18
  • Right-click and choose "Go To Definition" and see if it goes where you expect. – John Wu Jul 02 '20 at 21:23
  • @JohnWu it takes me to the posted screenshot of the interface – cal5barton Jul 02 '20 at 21:25
  • 2
    I bet it provides "default interface implementations" so you don't have to define anything (likely as they use nullable reference types too based on screenshot) – Alexei Levenkov Jul 02 '20 at 21:27
  • Put your cursor onto `Amazon.CDK.AWS.Lambda.IFunctionOptions`, press `Control + .` and hit `Enter` – efkah Jul 02 '20 at 21:30
  • @AlexeiLevenkov I was thinking maybe it had something to do with that. However, when trying to use my class I don't have access to any of those properties. Is there a way to get around this and force `MyClass` to implement each property? I could easily just add those properties manually but I want to make sure that if their interface changes then I can easily fix or add any additional properties. – cal5barton Jul 02 '20 at 21:32
  • @efkah I've already tried that and there is no option to implement the interface. – cal5barton Jul 02 '20 at 21:34

1 Answers1

1

This looks like C# 8 code, which includes nullable reference types and a new way to provide default implementations for interfaces.

This can make interface properties/methods optional:

  interface IInterface
  {
    public void MyMethod()
    {
      // do default thing
    }
  }

  class MyClass : IInterface
  {
  }

You can still just add your own implementations of these methods.

Keith
  • 150,284
  • 78
  • 298
  • 434
  • If it had a default implementation wouldn't I still have access to that property on the class level? So far it does not give me access to any of the properties the interface has defined. – cal5barton Jul 02 '20 at 21:42
  • @cal5barton absolutely not. https://stackoverflow.com/questions/62334232/can-a-c-sharp-class-call-an-interfaces-default-interface-method-from-its-own-im?noredirect=1&lq=1 and – Alexei Levenkov Jul 02 '20 at 22:05
  • @AlexeiLevenkov thanks for the link. I was referring to class usage outside of itself. If I have `MyClass` that implements an interface with a default property of Name, shouldn't I be able to call MyClass.Name or do I have to cast MyClass as the interface? – cal5barton Jul 02 '20 at 22:43
  • @cal5barton I'm pretty sure you need to cast to interface. I have not used that feature for real yet (lucky me :) ) so not sure. It should be trivial to experiment with code shown in this answer to confirm so. – Alexei Levenkov Jul 02 '20 at 22:55
  • @cal5barton it's not a base class, you're not overriding the implementation, you're replacing it. You can't reference any of them with `this.` because it's not in your class, but if you cast it that should work. – Keith Jul 03 '20 at 06:58