SHRI
SHRI

Reputation: 2466

Usage of generics as parameter in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns
{
    public class Composite
    {
        public Composite()
        {
            Console.WriteLine("I am Composite class constructor");
        }

        public void add()
        {
            Console.WriteLine("Added to Composite class");
        }
    }

    public class Component
    {
        public Component()
        {
            Console.WriteLine("I am Component class constructor");
        }

        public void add()
        {
            Console.WriteLine("Added to Component class");
        }
    }
}

I want to call add functions in above code. I do not want to use interfaces.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns
{
    class Program
    {
        static void Main(string[] args)
        {
            new Examp().Add(new Component());
            new Examp().Add(new Composite());
            Console.Read();
        }
    }

    public class Examp
    {
        public void Add<T>(T classinstance)
        {
            Console.WriteLine("Added " + classinstance + " to the Examp class");
            ((Composite)classinstance).add();
        }
    }
}

This is a compiler error. If this is not possible in this way, how to use classinstance here? (I tried to use generics similar to interface). If it is not possible, then what is the use of generics as parameter?

Upvotes: 0

Views: 84

Answers (3)

artm
artm

Reputation: 8584

Make Composite and Component implement an interface

public interface IMyI
{
    void add();
}

Your two classes implement IMyI and change your Examp.Add to

public void Add<T>(T classinstance) where T : IMyI
{
    Console.WriteLine("Added " + classinstance + " to the Examp class");
    classinstance.add();
}

Upvotes: 2

poke
poke

Reputation: 387677

If you don’t want to use interfaces, and your Add method actually just does something like that, you could also just provide an overload for both types:

public void Add (Composite classinstance)
{
    Console.WriteLine("Added " + classinstance + " to the Examp class");
    classinstance.add();
}

public void Add (Component classinstance)
{
    Console.WriteLine("Added " + classinstance + " to the Examp class");
    classinstance.add();
}

But if you plan to add these objects to something, for example a collection, you should definitely use a common base type, e.g. an interface as the others suggested, so that you retain their type instead of having to add them as information-less objects.

Then you could just use the interface to access things:

private List<IAddable> list = new List<IAddable>();

public void Add (IAddable obj)
{
    Console.WriteLine("Added {0} to the Examp class", obj);
    list.Add(obj); // actually add the object to some collection
    obj.add();
}

Upvotes: 0

tdragon
tdragon

Reputation: 3329

I would also strongly advice you using interfaces and similar solution provided in @artm. But if you really do not want to use interface, you can use dynamic parameter and change your Add method in Examp class:

public void Add(dynamic classinstance)
{
    Console.WriteLine("Added " + classinstance + " to the Examp class");
    classinstance.add();
}

Upvotes: 2

Related Questions