Data types in dot net flatform part two

When working with objects, it is frequently necessary to organize groups of objects into some kind of structure. Groups of similar or dissimilar objects might need to be accessed in sequence and require syntax to keep track of their organization.

Arrays allow you to organize groups of similar objects and refer to them by an index rather than a name. Collections are similar to arrays but implement advanced functionality and can be used to organize dissimilar groups of objects as well. Constants allow you to define user friendly names to represent frequently used values. Enumerations (enums) allow you to organize sets of related integral constants and use them to help make your application easy to read and debug.

You might find that certain values are frequently used in your application. Constants allow you to assign user friendly names to frequently used values and refer to them in code. Enumerations are user-defined sets of integral constants that correspond to a set of friendly names. Using constants and enumerations will make your code easier to read, easier to debug, and less prone to errors caused by typographical errors.

Constants can be of any intrinsic or enumerated type, but not of a user-defined type or an array. As the name implies, constants are of a fixed value that cannot be changed or redefined once set. Once a constant is defined, you can use its name in code in place of the value it represents.

Like variables, constants can be of any access level. If you want your constant to be available to all users of your application or component, you can declare it with the Public (public) keyword, as shown in the preceding examples. To create a constant for internal use only, use the Private (private) keyword. The Friend (internal) keyword specifies assembly level access, and the Protected (protected) keyword allows access by inheriting types.

Enumerations allow you to work with sets of related constants and to associate easy to remember names with those constants. For example, you could declare a set of enumeration constants that were associated with the days of the week, and then refer to them by name in your code.

The default data type for enums is Integer (int), but can be any of the integral numeric data types (in Visual Basic .NET: Byte, Short, Integer, Long or in Visual C#: byte, short, int, long). To define an enum as a different data type, you must specify it in the declaration line.

It is not necessary to supply values for the members of your enum. If you choose to not designate specific values, they will be assigned default values numbered sequentially starting from zero.

Once you have defined the members of your enumeration, you can use them in code. In Visual Basic .NET, enumeration members are automatically converted to the value that they represent and can be used as constants in code.

Because enumerations are user-defined types, you can create methods that require enumeration members as values instead of numeric values. By requiring enum members as parameters instead of numbers, you create code that is less prone to errors by inaccurate typing.


You can manage groups of similarly typed values or objects by using arrays. Arrays allow you to group a series of variables and refer to them by an index. You can loop through all or part of the variables and examine or affect each in turn. You can also create arrays with multiple dimensions. Arrays in the .NET Framework have built-in functionality to facilitate many of the common tasks associated with arrays.

Declaring and Initializing Arrays

Arrays can be declared and initialized in the same statement. When declaring an array in this manner, you must specify the type of the array elements, and you must specify the number of elements of the array.

All arrays in Visual Basic .NET and Visual C# are zero based—meaning the index of the first element is zero—and they are numbered sequentially. In Visual Basic .NET, you must specify the number of array elements by indicating the upper bound of the array.

The upper bound is the number that specifies the index of the last element of the array. When declaring an array in Visual C#, however, you must indicate the number of array elements by specifying the number of elements in the array. Thus, the upper bound of an array in Visual C# will always be one less than the number used in the declaration statement.

Although you can dynamically initialize arrays in Visual Basic .NET as well, the syntax is somewhat less flexible. If you declare an array without specifying a number of elements on one line, you must provide values for each element when initializing the array. Initial values can be provided for an array by enclosing them in curly braces {} and separating them with commas.

When creating an array of reference types, declaring and initializing an array does not create an array filled with members of that type. Rather, it creates an array of null references that can point to that type. To fill the array with members, you must assign each variable in the array to an object, which can be either a new object or an existing object.

Multidimensional Arrays

The arrays that you have worked with thus far have been linear arrays, that is, ones with only one dimension. The .NET Framework supports two kinds of multidimensional arrays: rectangular arrays and jagged arrays.

Rectangular Arrays

Rectangular arrays are arrays in which each member of each dimension is extended in each other dimension by the same length. For example, a two dimensional array can be thought of as a table, consisting of rows and columns. In a rectangular array, all of the rows have the same number of columns.

You declare a rectangular array by specifying additional dimensions at declaration.

Jagged Arrays

The other type of multidimensional array is the jagged array. A two dimensional jagged array can be thought of as a table where each row can have a different number of columns. Take for example, a table where families are the rows and family members are the columns. Unless each family has the same number of members, each row will have a variable number of columns. You can use a jagged array to represent such a table.

A jagged array is really an array of arrays. To create a jagged array, you declare the array of arrays with multiple sets of parentheses or brackets and indicate the size of the jagged array in the first set of brackets (parentheses).


Collections provide advanced functionality for managing groups of objects. A collection is a specialized class that organizes and exposes a group of objects. Like arrays, members of collections can be accessed by an index. Unlike arrays, however, collections can be resized dynamically, and members can be added and removed at run time.

Collections are useful for managing groups of items that are dynamically created at run time. For example, you might create an application that analyzes a group of Customer objects, each representing a set of data about a particular customer.

By creating a collection of Customer objects, you would be able to access the objects, iterate through them, and add new objects as they became available, or remove objects as they became irrelevant.

The ArrayList Class

The System.Collections.ArrayList class provides general collection functionality that is suitable for most purposes. This class allows you to dynamically add and remove items from a simple list. Items in the list are retrieved by accessing the index of the item.

The list of objects managed by the ArrayList is a zero-based collection. This means that the first object added to the list is assigned the index zero, and every subsequently added object is assigned to the next available index.

The Item Property Indexer

You can retrieve the item at a particular index by using the Item property. In Visual Basic .NET, the Item property is the default property for collections. Thus, you can access it without using the Item property name, allowing syntax similar to that of an array.

In Visual C#, default properties are called indexers, and you must use the array-like syntax rather than specifically using the Item property name. Default properties and indexers are discussed in detail in the next lesson. The following example demonstrates how to retrieve a reference to an object in a collection.


No comments:

Post a Comment