Solved

Can someone explain?

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ASP.NET e-commerce website 4 30
SQl help with selection 14 44
Set form below another form 3 26
Spacing between controls 4 17
      Install BugTracker on Windows 2008 server Step 1:  Install windows 2008 server 32 bit OS and configure IIS. Step 2:  Install SQL server ( SQL server 2005 or SQL server 2005 Express edition. The installer for 2008  version isn’t very f…
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…
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.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now