Moons
Moons

Reputation: 3854

Class having Generic List in C#

I have a class like:

 public class boolMessage
{
    public bool Message { get; set; }
    public ResponseType Resp { get; set; }

    public boolMessage(bool _Message, ResponseType _Resp)
    {
        Message = _Message;
        Resp = _Resp;
    }
}

What i want to do is to create a class something like:

 public class listMessage
{
    public List<T> Message { get; set; }
    public ResponseType Resp { get; set; }

    public listMessage(List<T> _Message, ResponseType _Resp)
    {
        Message = _Message;
        Resp = _Resp;
    }
}

I think i can do with something like Generic Classes. I have googled it open lot of pages but not able to find some easy solution.

Why i required is like i want to return a array of object that are of different types like Student[] or Teacher[].

These classes like Teacher are generated by Entity Framework.

I would like to return some additional information with them ...

Any help is appreciated

Upvotes: 0

Views: 76

Answers (1)

JohnB
JohnB

Reputation: 13743

  class ListMessage<T> {
     ...
  }

Further comments:

  1. The name of your constructor in ListMessage is wrong.
  2. You should not expose member variables to the outside.

With regard to 2., I would do the following:

class ListMessage<T> {
   private List<T> _messages;
   public IEnumerable<T> Messages { get { return _messages; } }

   public ListMessage (IEnumerable<T> messages) {
      this._messages = messages.ToList ();
   }
}

Upvotes: 2

Related Questions