Vigs
Vigs

Reputation: 1331

How to use generic Queue where the type also has a generic paramater?

I have a situation where I would like to have a member property on my class that utilizes the generic Queue<T> provided by .Net, however, my type is also generic.

For example:

I want to declare my class having a member property being a Queue instance;

public class SomeServiceThing: Singleton<SomeServiceThing>
{
    private Queue<SomeType<T>> = new Queue<SomeType<T>>();

    ....

    private void EnqueueEvent<T>(SomeType<T> _event)
    {
        pendingEvents.Enqueue(_event);
        lastEventQueued = DateTime.Now;
    }

    private void Update()
    {
        if (someConditionIsMet)
        {
            // flush the queue and perform an operation with each pending event
        }
    }
}

The compiler error I received is "Cannot resolve symbol 'T'", which makes sense as T cannot be derived when the class is instantiated, but then I would ask how would you go about using Queue in this way when T itself has a generic parameter?

Upvotes: 0

Views: 615

Answers (4)

Leo Bartkus
Leo Bartkus

Reputation: 1946

If you want one SomeServiceThing instead of one SomeServiceThing per type T, make your generic SomeType<T> implement an interface, and use that interface as the type T for the queue.

public class SomeServiceThing: Singleton<SomeServiceThing>
{
    private Queue<ISomeType> = new Queue<ISomeType>();

    ....

    private void EnqueueEvent<T>(ISomeType _event)
    {

And then define SomeType like:

public class SomeType<T>: ISomeType
{

Upvotes: 0

Farhad Zamani
Farhad Zamani

Reputation: 5861

you should change the SomeServiceThing class to SomeServiceThing<T>

public class SomeServiceThing<T> : Singleton<SomeServiceThing<T>>
{
    private Queue<SomeType<T>> pendingEvents = new Queue<SomeType<T>>();

    private void EnqueueEvent(SomeType<T> _event)
    {
        pendingEvents.Enqueue(_event);
        lastEventQueued = DateTime.Now;
    }

    private void Update()
    {
        if (someConditionIsMet)
        {
            // flush the queue and perform an operation with each pending event
        }
    }
}

Upvotes: 2

player2135
player2135

Reputation: 111

public class SomeServiceThing<T>: Singleton<SomeServiceThing>
{
    private Queue<SomeType<T>> = new Queue<SomeType<T>>();

....

    private void EnqueueEvent(SomeType<T> _event)
    {
        pendingEvents.Enqueue(_event);
        lastEventQueued = DateTime.Now;
    }

    private void Update()
    {
        if (someConditionIsMet)
        {
        // flush the queue and perform an operation with each pending event
        }
    }
}

Upvotes: 0

Michael Gehling
Michael Gehling

Reputation: 647

If you use T in your queue, you have to define it already on class level and not on method level:

public class SomeServiceThing<T>: Singleton<SomeServiceThing<T>>
{
    private Queue<SomeType<T>> = new Queue<SomeType<T>>();

    ....

    private void EnqueueEvent(SomeType<T> _event)
    {
        pendingEvents.Enqueue(_event);
        lastEventQueued = DateTime.Now;
    }

    private void Update()
    {
        if (someConditionIsMet)
        {
            // flush the queue and perform an operation with each pending event
        }
    }
}

Upvotes: 0

Related Questions