.Net Dojo: Methods with a Generic Return Type
In 2.0, Generics were added. If you haven’t played with generics before, they are a way of declaring a type for an object for the compiler, such as a list, without having to use the System.Object class. In this way, you can have strongly typed lists instead of a list of objects that you must then typecast when you pull them back out.
If you think of generics outside the programming world, you might think of generic medicine. It is medicine that has the same effects as the name brand stuff, but isn’t attached to any brand. Generics in programming are like this. They aren’t bound to a type, but share the same methods and classes.
Here are a few examples of the List object from System.Collections.Generics. The list is exactly as it says. You add items to the list that are the same type as you delcare, and then access them using a zero-based index. It’s similar to the ArrayList from System.Collections, but the return type of the items is not System.Object that must be typecast.
List<int> integerList = new List<int>();
List<string> stringList = new List<string>();
integerList.Add(123);
stringList.Add("Hello World");
As you can see from the example, using one class you can make lists for any object. You can even use your own custom defined classes. Lets say you want to use generics to make a method that has mutiple return types. A prime example of this might be returning specified types from a querystring.
public static T GetQueryStringValue<T>(string key) where T : IConvertible
{
HttpContext context = HttpContext.Current;
string value = Request.QueryString[key];
return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
}
First, we have the statement where T : IConvertible. This is because, in order to use ChangeType, the type must implement IConvertible. Also, you’ll notice ChangeType still requires a typecast after telling it the type. With this code, you can create methods that have a variable return type. Usage would look something like this.
int customerId = GetQueryStringValue<int>("cid");
string customerName = GetQueryStringValue<string>("cn");
Generics have a lot of power when used correctly. It’s all about unlocking the secrets to make them do what you need.
Addendum: June, 21, 2008
You can adjust the types of your arguments using generics as well. I believe, however, that they must be the same type as declared within the brackets.
public static T MyMethod<T>(T arg1)
{
//...do stuff
}
public static void MyMethodNoReturn<T>(T arg1)
{
//...do stuff
}
public static MyCustomObject MyMethodSpecifiedType<T>(T arg1)
{
//...do stuff
}
Generics can be applied to both the method return type and the argument types.
November 27, 2007 - Posted by Broken Bokken | .Net | ASP.NET, C#, .Net, Programming, .Net 2.0, System.Collections.Generic, Generic Return, Return Type, IConvertible, TypeCast, typeof, ChangeType, Convert, Generics, .Net Dojo, Technology | 2 Comments
2 Comments »
Leave a comment
Categories
- Anime (3)
- Japanese (9)
- Life (4)
- Martial Arts (4)
- Personal (14)
- Recipes (2)
- Shinkendo (2)
- Storm Chasing (1)
- Tech-Knowledge (14)
- .Net (13)
Archived Articles
Favorites
Friends
Resources
-
Archives
- June 2008 (3)
- May 2008 (1)
- March 2008 (1)
- February 2008 (6)
- January 2008 (6)
- November 2007 (18)
-
Categories
-
RSS
Entries RSS
Comments RSS
Nice! What if one wants a different return tipe from the argument of the function?
Sort of int customerid=getwhathever(”cid”>.
Is it possible?
Thanks.
.d,
If you want a different return type, simply change the type in the < and > from int to whatever datatype you wish to return. However, only types that implement IConvertible can be returned using this method.