1

I've declared an interface with Add method to add new item, 2 classes TopicModel and CommentModel to store data.

I've re-write the code in console mode, like this:

interface IAction
{
    void Add<T>(params T[] t) where T : class;
}

class TopicModel
{
    public string Id { get; set; }
    public string Author { get; set; }
}

class CommentModel
{
    public string Id { get; set; }
    public string Content { get; set; }
}

class Topic : IDisposable, IAction
{
    public void Add<T>(params T[] t) where T : class
    {
        var topic = t[0] as TopicModel;
        var comment = t[1] as CommentModel;

        // do stuff...
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

class MainClass
{
    static void Main()
    {
        var t = new TopicModel { Id = "T101", Author = "Harry" };
        var c = new CommentModel { Id = "C101", Content = "Comment 01" };

        using (var topic = new Topic())
        {
            //topic.Add(t, c);
        }
    }
}

The line topic.Add(t, c) gave me error message:

The type arguments for method 'Topic.Add(params T[])' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Then, I've tried again with:

topic.Add(c, c) // Good :))
topic.Add(t, t, t); // That's okay =))

That's my problem. I want the method accepts 2 different types (TopicModel and CommentModel).

And, I don't want to declare:

interface IAction
{
    void Add(TopicModel t, CommentModel c);
}

because another classes can re-use Add method within different argument types.

So, my question is: how to change params T[] t to accept multiple argument types?

5
  • 7
    The code is simply not generic. You hope that the caller passes arguments of the correct type but there is no way for the compiler to actually verify and enforce that. You might as well use object. Commented Mar 6, 2016 at 17:20
  • What are the types that the Add method is expected to work with? For example, can it work for the int type? In the body of the Add method, you are explicitly casting the first parameter to TopicModel and the second to CommentModel, this means that you are expecting these specific types. You need to add more context to the question about what the Add method is supposed to do. Commented Mar 6, 2016 at 17:21
  • @YacoubMassad It gave me no error if I changed topic.Add(t, c) to topic.Add(t, t) or topic.Add(c, c) Commented Mar 6, 2016 at 17:25
  • @HansPassant You mean object array, don't you? Commented Mar 6, 2016 at 17:27
  • Sure. Or dynamic[], saves you from having to use as and fret about what to do when it generates null. Commented Mar 6, 2016 at 17:29

1 Answer 1

3

TopicModel et CommentModel must inherit same class Or implement same interface. Try This :

interface IAction
{
    void Add<T>(params T[] t) where T : IModel;
}

class IModel
{
}

class TopicModel : IModel
{
    public string Id { get; set; }
    public string Author { get; set; }
}

class CommentModel : IModel
{
    public string Id { get; set; }
    public string Content { get; set; }
}

class Topic : IDisposable, IAction
{
    public void Add<T>(params T[] t) where T : IModel
    {
        var topic = t[0] as TopicModel;
        var comment = t[1] as CommentModel;

        Console.WriteLine("Topic witch ID={0} added",topic.Id);
        Console.WriteLine("Commment witch ID={0} added", comment.Id);
    }

    public void Dispose()
    {

    }
}

class Program
{
    static void Main()
    {
        TopicModel t = new TopicModel { Id = "T101", Author = "Harry" };
        CommentModel c = new CommentModel { Id = "C101", Content = "Comment 01" };

        using (var topic = new Topic())
        {
            topic.Add<IModel>(t, c);
        }

        Console.ReadLine();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.