Solved

Can someone explain?

Posted on 2007-11-14
2
195 Views
Last Modified: 2013-11-26
I am disecting an application and need some help understanding the "where T : class"  and the "<T>"
portions of the following piece of code.  This is something new to me.  I appreciate all explanations.  If not to much of a problem could you refer me to some info too.  Thanks in advance.

Line below:
public static Type FindTypeByInterface<T>(Assembly assembly) where T : class
0
Comment
Question by:TroyCrowe
  • 2
2 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 250 total points
ID: 20284192
It's declares a function named FindTypeByInterface.  In this case, T is a 'generic' parameter, very similar to a template in C++.  The < > is like anotherparameter list for the function, just like ( ).  The difference is that this parameter list accepts types instead of values.  The T means it accepts one type, and in the function that type will be named T.  In the body of the function, T will be used in place of whatever type is passed to the function.  So if your were to call the function like this:
FindTypeByInterface<string>(objAssembly)
Then in the function T would be a string, and you could have code like this:
T instance = "instance is a string";

Now that leaves the 'where T : class' part.  This means that the T parameter passed to the function must be a class.  No structures or value types allowed.  Many generic functions will instead require T do something like implement IComparable, ICloneable, or something like that, but it looks like the point of this function is simply to tell you what T is.
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 20284228
Here's an easier to understand example of a generic function:

    public void Swap<T>(out T a, out T b)
    {
         T temp = a;
         a = b;
         b = temp;
    }

This method swaps two of anything that understands the = operator.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question