Solved

Can someone explain?

Posted on 2007-11-14
2
196 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
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…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
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.

829 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