Solved

Can someone explain?

Posted on 2007-11-14
2
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

690 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