Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Generics in ASP.NET


Generics in ASP.NET


Generics are a Common Language Runtime feature that language compilers and the .NET Framework fully support. What about ASP.NET? What is, if any, the intersection between ASP.NET and generics? Like in many other .NET programming contexts, generics can be used in ASP.NET to write neater and more strongly typed code. However, the vast majority of sample code for ASP.NET 2.0 doesn’t use generics. Why is it so? I don’t have the definitive answer, but I can make some guesses. First and foremost, ASP.NET 2.0 was not designed with generics in mind. Just because they are such a powerful feature, to take real advantage of generics, you need to design the code in a much more parametric way. This is true for developers authoring pages and controls, but it was even more so for the Microsoft developers who actually wrote the ASP.NET framework. I’d even say that ASP.NET supports generics, but it doesn’t seem to be conscious of this new and exciting possibility—not in Version 2.0, at least.

However, there are few things that you can do in ASP.NET today that involve generics. Let’s briefly tour through them. They are: event handlers, collections and data binding, and viewstate.

In ASP.NET 1.x, to define an event on a custom control, you had to define a delegate and an event data structure. For example, to fire the event ViewCollapsing and pass some control-specific data, you need a custom delegate like:

delegate void ViewCollapsingEventHandler(<br>     object sender, ViewCollapsingEventArgs e);

In addition to defining the custom event-args class, you need an explicit declaration of the delegate. Next, you define your event:

public event ViewCollapsingEventHandler ViewCollapsing;

The point is, all event delegates share a similar structure. They are void and accept two arguments, the first of which is the sender (an object). The second argument varies and corresponds to the event-args class. If you look at it with a generics-open mind, you can’t help but note the similarity. What is the event-args class if not a parametric type?

In light of this, in ASP.NET 2.0 the EventHandler class—the default class for event handlers—also sports a generic version: EventHandler<T>. If you use this version, you can declare events on pages and controls without explicitly defining the event handler delegate. Here’s how:

public event EventHandler<ViewCollapsingEventArgs> ViewCollapsing;

You define the event-args class and go. No other code is required.

Another scenario in which generics are transparently used is the viewstate storage. You can work with generic collections and still save and restore them from the viewstate.

public List<int> MyIntList<br>{<br>	get <br>	{<br>		object o = ViewState["MyIntList"];<br>		if (o == null)<br>			return new List<int>();<br>		return (List<int>) o; <br>	}<br>	set { ViewState["MyIntList"] = value; }<br>}

The same holds true for data binding. All data-bound controls don’t distinguish classic collections from generic collections.

Finally, ASP.NET accepts custom controls with a generic declaration; for example, a custom control where one of the properties owes its runtime type to the generics layer.

public class GenControl<T> : WebControl<br>{<br>	public T Data<br>	{<br>		get { return (T) ViewState["Data"]; }<br>		set { ViewState["Data"] = value; }<br>	}<br>      :<br>}

If you create instances of this control dynamically, all works just fine.

GenControl<int> x = new GenControl<int>();<br>x.Data = 3;<br>Controls.Add(x);

Unfortunately, you can’t work with these controls from within Visual Studio 2005.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.