03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

156 x CHAPTER 5 PROGRAMMING YOUR <strong>ASP</strong>.<strong>NET</strong> WEB PAGES<br />

An Introduction to Generics<br />

S<strong>in</strong>ce their <strong>in</strong>troduction with .<strong>NET</strong> 2.0, generics pop up <strong>in</strong> many different locations <strong>in</strong> the .<strong>NET</strong><br />

Framework. Although they are used often <strong>in</strong> situations where collections are used, the use of generics<br />

is not limited to collections; you can also use them for s<strong>in</strong>gular types of objects.<br />

Generics are to code what Microsoft Word templates are to word process<strong>in</strong>g. They enable you to<br />

write a code template that can be used <strong>in</strong> different scenarios with different types. With generics,<br />

you can def<strong>in</strong>e a generic code template that doesn’t explicitly specify a type. Only when that code is<br />

used do you def<strong>in</strong>e the type. The ma<strong>in</strong> benefit of this is that you can reuse the same template over<br />

<strong>and</strong> over aga<strong>in</strong> for multiple data types, without retyp<strong>in</strong>g <strong>and</strong> ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>g multiple versions of the<br />

code. In addition to us<strong>in</strong>g generics <strong>in</strong> your own code def<strong>in</strong>itions, you f<strong>in</strong>d a host of generics-enabled<br />

objects <strong>and</strong> collections <strong>in</strong> the .<strong>NET</strong> Framework, ready to be used by your code.<br />

To underst<strong>and</strong> how you can take advantage of generics, take a look at the follow<strong>in</strong>g example. It’s<br />

essentially the same code you saw earlier where the ArrayList was used, but this time the type of<br />

the list is constra<strong>in</strong>ed so it accepts only str<strong>in</strong>gs:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Dim roles As New List(Of Str<strong>in</strong>g)<br />

roles.Add("Adm<strong>in</strong>istrators")<br />

roles.Add("ContentManagers")<br />

roles.Add("Members")<br />

C#<br />

List roles = new List();<br />

roles.Add("Adm<strong>in</strong>istrators");<br />

roles.Add("ContentManagers");<br />

roles.Add("Members");<br />

Not much code has changed to make the roles list type safe. However, with the def<strong>in</strong>ition of List<br />

(Of Str<strong>in</strong>g) <strong>in</strong> <strong>VB</strong>.<strong>NET</strong> <strong>and</strong> List <strong>in</strong> C# the new list is now set up to allow only str<strong>in</strong>gs<br />

to be added through its Add method. This compiles f<strong>in</strong>e:<br />

roles.Add("Adm<strong>in</strong>istrators");<br />

The follow<strong>in</strong>g will not compile because 33 is not a Str<strong>in</strong>g:<br />

roles.Add(33);<br />

Similar to a generics list of str<strong>in</strong>gs, you can also create lists to hold other types. For example:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Dim <strong>in</strong>tList As New List(Of Integer)<br />

Dim boolList As New List(Of Boolean)<br />

Dim buttonList As New List (Of Button)<br />

C#<br />

List <strong>in</strong>tList = new List();<br />

List boolList = new List();<br />

List buttonList = new List();<br />

' Can hold Integers only<br />

' Can hold Booleans only<br />

' Can hold Button controls only<br />

// Can hold <strong>in</strong>ts only<br />

// Can hold bools only<br />

// Can hold Button controls only

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!