Solved

Can someone explain?

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

733 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