Hi @Ronald Rex , Welcome to Microsoft Q&A,
An interface defines a contract that a class must adhere to. In this case, IActionMovie
is an interface, and any class that implements this interface must provide concrete implementations for the methods or properties defined in the interface.
- Define an interface
IActionMovie
:
public interface IActionMovie
{
void Play();
string Title { get; set; }
}
- Create a class that implements the IActionMovie interface:
public class ActionMovie : IActionMovie
{
public string Title { get; set; }
public void Play()
{
Console.WriteLine("Playing action movie: " + Title);
}
}
- Use the MyClass to work with an IActionMovie:
MyClass myObject = new MyClass();
// Set the Movie property with an instance of ActionMovie
myObject.Movie = new ActionMovie { Title = "Die Hard" };
// Call the Play method on the ActionMovie
myObject.Movie.Play();
By setting the Movie property with an instance of a class that implements IActionMovie, you can call the methods and access the properties defined in the interface through the Movie property.
This allows you to work with various classes that implement the IActionMovie interface within your MyClass without knowing their specific implementations. It provides flexibility and enables polymorphism in your code, making it easier to swap different action movie implementations as long as they adhere to the IActionMovie contract.
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.