Working with Generics

Must Read

Resource Management and Performance

Like all mobile devices, Windows Phone 7 devices have limited processing, memory, and storage facilities compared to a desktop...

Nikon D7000, Classic Black-and-White Portraits

There is something timeless about a black-and-white portrait. It eliminates the distraction of color and puts all the emphasis...

5 Points to Consider When Refinancing Your Mortgage

Refinancing your mortgage can be important at various points in time to make sure you are saving the most...

How To Find A Student Loan Consolidation Program That’s Right For You

Many people are now looking for ways on how to find a student loan consolidation program which would suit...

Nikon D7000, Using the Ae Lock (Auto Exposure Lock) Feature

There will often be times when your subject is not in the center of the frame but you still...

Peeking Inside a Crime Lab

You have heard of crime labs where scientific testing takes place on the TV show CSI. How is...

Generic classes are exceptionally easy to work with in IronPython, which is a good feature to have because the .NET Framework includes a lot of generic classes. Listing 7-5 starts with a simple List, but the technique shown in this listing works with every other generic class that .NET provides.

Listin g 7-5: Using .NET generics


# Add the .NET Framework 2.0 to the path.
import sys
# Make clr accessible.
import clr
# Import the required assemblies.
import System
import System.Collections.Generic
# Create an integer list.
MyList = System.Collections.Generic.List[int]()
# Add items to the list.
# Enumerate the list.
print ‘Initial List’
for I in MyList:
print I,
# Add a range of numbers.
# Enumerate the updated list.
print ‘nnList With Range Added’
for I in MyList:
print I,
# Remove a value that’s no longer needed.
# Display the results.
print ‘nnList With 4 Removed’
for I in MyList:
print I,
# Remove a range of items.
MyList.RemoveRange(1, 2)
# Display the results.
print ‘nnRemoved Items 1 and 2’
for I in MyList:
print I,
# Pause after the debug session.
raw_input(‘nPress any key to continue…’)


The code begins with the usual imports. Notice that you must import System.Collections.Generic to make this example work. If you were going to work quite a bit with this namespace, you’d want to import it into the global namespace because typing the longer namespace would prove time consuming and error prone. Fortunately, you use it only once in this example, to create MyList, which is an int List. Notice that the data type of List appears within square brackets and that the example uses a Python data type. You could just as easily use a .NET data type.

At this point, the code makes some additions to MyList using the Add() method. Most .NET developers have used this technique since they first started working with their .NET language of choice, so there aren’t any surprises here. The initial output appears in Figure 7-15.

Figure 7-15: You can use generics to interact with a list.
Figure 7-15: You can use generics to interact with a list.

The next addition might surprise you a little. The code uses the AddRange() method to add a range of numbers supplied by the range() function. This is another example where you can easily mix .NET and Python code without any problems. Figure 7-15 shows that MyList now contains both the initial additions and the range of numbers.

You can also remove values from MyList. The first example uses Remove() to remove a specific value. The RemoveRange() method removes a range of the entries by position. Both removals appear in Figure 7-15 so you can see their effect.

Like many of the techniques described in this chapter, List has a lot more to offer. Make sure you perform a dir(MyList) to see other tasks you can perform with the List generic class. For example, you might want to see the results of using Reverse() or Sort() on MyList. Have some fun with this example because discovering what generics can add to IronPython is important.

Latest News

Digital Marketing for Beginners

Digital marketing for starter, Let to basic learning about connecting with your audience in the right place at the...

More Articles Like This