Passing generic parameters without typing the containing class?

I am wondering if there is a way to implement a Generic interface and pass the generic item to a method without typing the class containing the method?
 
The following does not compile, stating, "The type or namespace ‘T’ could not be found (are you missing a using directive or an assembly reference?)".
 

class SomeClass {

    IList<T> encapsulatedList;

    public SomeClass(IList<T> list) {

        this.encapsulatedList = list;

    }

}

 

When you change SomeClass to SomeClass<T>, it then compiles.

The problem is that the relationship between SomeClass and its encapsulated list is a "has a" rather than an "is a" relationship.

 

Anyone with any guidance, please post a comment.

Advertisements

2 thoughts on “Passing generic parameters without typing the containing class?

  1. Just in case the link is dead, here is the snipped from msdn:
    A generic method is a method that is declared with type parameters, as follows:

    static void Swap(ref T lhs, ref T rhs)
    {
    T temp;
    temp = lhs;
    lhs = rhs;
    rhs = temp;
    }

    The following code example shows one way to call the method, using int for the type argument:

    public static void TestSwap()
    {
    int a = 1;
    int b = 2;

    Swap(ref a, ref b);
    System.Console.WriteLine(a + ” ” + b);
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s