Please help me with the task: Everyone likes messages nowadays

Hello, I’m working on this task:

Remove implementation and all forbidden members from the interface IMessage. Move them to the class Message, so that class Message implements interface IMessage. Your program should compile.

Here is my code:


namespace Interfaces
{
   public interface IMessage
   {
       string Type { get; }
       string Payload { get; }
       void Read(string payload);
   }

   public class Message : IMessage
   {
       public string Type
       {
           get
           {
               return "Encrypted message";
           }
       }

       public string PayLoad
       {
           get
           {
               return PayLoad;
           }
       }
       public void Read(string payload)
       {
           payload = PayLoad;
       }
   }

   public class EveryoneLikesSendingMessagesNowadays
   {
       public static void Main()
       {
           var message = new Message();

           message.Read("I will never implement methods and properties in an interface.");

           Console.WriteLine(message.Payload);
       }
   }
}

Can’t get what I have to do in ‘public void Read(string payload)’ . Please, help me to understand the task and my mistakes.

  1. In the method read, I guess you rather meant:
public void Read(string payload)
 {
     PayLoad = payload;
 }

But! According to IMessage inteface this would not work, since PayLoad has only get method and thus can’t be assigned directly like in the code above.
Luckily in C# there are private fields, that you could use to solve this task, like:

private string _payload;

so that you could both assign and return it :slight_smile:

1 Like

Thank you! It works, also I found a mistake in one word:)