Search
Sunday, August 01, 2010 ..:: Home ::.. Register  Login
   Calendar  
     
  
   Search  
     
  
   Blogroll  
     
  
   Disclosure  
All blog entries are the opinions of the author and do not necessarily reflect the opinions of their employer. All the code presented is for explanation and demonstration purposes only. Any damages incurred to your site and/or data are not the responsibility of the author. Every effort is taken to ensure the code properly compiles, however sometimes there are some hiccups and you might be required to do your own debugging.
     
  
   TechTidBits (Blog)  
Dec 5

Written by: Peter Henry
Friday, December 05, 2008 9:26 PM

A good friend of mine challenged me a C# technical question this week (thanks SG :>) and we both thought it would be great a topic for a blog entry.  Unfortunately I don't have time for a drawn out example, but I wanted to make sure I put some information into a blog entry so I don't forget it!  Oh and hopefully you'll be able to use the links (and maybe the information :>) and put it to good use on your projects. :>

SG's question was, explain this code?

public Food<string> FeedMe<T>(String typeOfFood) where T : class

Generics are a GREAT way to ensure strong typing for methods you want to be able to accept/return anything!  But what about when you want to put limits on what you can/cannot accept?  hhhhmmmmm are you SOL?  No, not really, you're looking for Contraints.  The basic idea is if you want to have a method accept an a Dog class but not Cat or even Mamal or Animal, then you would put the Dog Constraint onto your generic. 

But what about if you want your generics to be able to accept any reference type but not a value type, or what about the other way around?  hhhmm  You could use Object for the first case, but wait, that won't work if you want to specify an Interface, or what about a delegate?  Are you out of luck?  Nope, C# has thought of this.  Read further for more details.

Back to the question above.  Take a look at the sample code below for a more complete explanation (specific line to look at is line 30).

Generics Code Sample

The basic idea is, in the Zoo class, we're declaring a public method named FeedMe which returns a type of Food object (I hope you got that much :>).  After that is where Generics come into play.  The method signature for FeedMe is taking a Generic T class, any T class will do, whoa, that isn't entirely correct, since there's a where clause, it's "constrainted" to what ever is after it, in this case, any reference type.  And lastly, the returned object is a Food object of type string (yup, another Generic).

 

Resources:

MSDN: An Introduction to C# Generics (http://msdn.microsoft.com/en-us/library/ms379564(vs.80).aspx)

MSDN: Definition of Constraints (http://msdn.microsoft.com/en-us/library/ms379564(vs.80).aspx#csharp_generics_topic4)

MSDN: Constraints on Type Parameters (C# Programming Guide) (http://msdn.microsoft.com/en-us/library/d5x73970(VS.80).aspx)

Copyright ©2008 Peter Henry

Tags:

Your name:
Title:
Comment:
Add Comment    Cancel  
     
  
Copyright 1999-2010 by PCHenry.com   Terms Of Use  Privacy Statement