Labels

test (1)

16 February 2011

Direct Access Collections C# .NET


   The most common example of a direct access collection is the array.We define an array as a collection of elements with the same data type that are directly accessed via an integer index, as illustrated in Figure 1.1.



   Arrays can be static so that the number of elements specified when the array is declared is fixed for the length of the program, or they can be dynamic, where the number of elements can be increased via the ReDim or ReDim Preserve statements.

   In C#, arrays are not only a built-in data type, they are also a class. 
We can use an array to store a linear collection. Adding new elements to an array is easy since we simply place the new element in the first free position at the rear of the array. Inserting an element into an array is not as easy (or efficient), since we will have to move elements of the array down in order to make room for the inserted element.

   Deleting an element from the end of an array is also efficient, since we can simply remove the value from the last element. Deleting an element in any other position is less efficient because, just as with inserting, we will probably have to adjust many array elements up one position to keep the elements in the array contiguous.
The .NET Framework provides a specialized array class, ArrayList, for making linear collection programming easier. Another type of direct access collection is the string. A string is a collection of characters that can be accessed based on their index, in the same manner we access the elements of an array. Strings are also implemented as class objects in C#. The class includes a large set of methods for performing standard operations on strings, such as concatenation, returning substrings, inserting characters, removing characters, and so forth.

   C# strings are immutable, meaning once a string is initialized it cannot be changed. When you modify a string, a copy of the string is created instead of changing the original string. This behavior can lead to performance degradation in some cases, so the .NET Framework provides a StringBuilder class that enables you to work with mutable strings.

   The final direct access collection type is the struct (also called structures and records in other languages). A struct is a composite data type that holds data that may consist of many different data types.
 For example, an employee record consists of employee’ name (a string), salary (an integer), identification number (a string, or an integer), as well as other attributes. Since storing each of these data values in separate variables could become confusing very easily, the language provides the struct for storing data of this type.

   A powerful addition to the C# struct is the ability to define methods for performing operations stored on the data in a struct. This makes a struct somewhat like a class, though you can’t inherit or derive a new type from a structure.

The following code demonstrates a simple use of a structure in C#:

using System;
public struct Name
{
    private string fname, mname, lname;
    public Name(string first, string middle, string last)
    {
        fname = first;
        mname = middle;
        lname = last;
    }
    public string firstName
    {
        get
        {
            return fname;
        }
        set
        {
            fname = firstName;
        }
    }
    public string middleName
    {
        get
        {
            return mname;
        }
        set
        {
            mname = middleName;
        }
    }
    public string lastName
    {
        get
        {
            return lname;
        }
        set
        {
            lname = lastName;
        }
    }
    public override string ToString()
    {
        return (String.Format("{0} {1} {2}", fname, mname,
        lname));
    }
    public string Initials()
    {
        return (String.Format("{0}{1}{2}", fname.Substring(0, 1),
        mname.Substring(0, 1), lname.Substring(0, 1)));
    }
}
public class NameTest
{
    static void Main()
    {
        Name myName = new Name("Michael", "Mason", "McMillan");
        string fullName, inits;
        fullName = myName.ToString();
        inits = myName.Initials();
        Console.WriteLine("My name is {0}.", fullName);
        Console.WriteLine("My initials are {0}.", inits);
    }
}

   Although many of the elements in the .NET environment are implemented as classes (such as arrays and strings), several primary elements of the language are implemented as structures, such as the numeric data types. The Integer data type, for example, is implemented as the Int32 structure. One of the methods you can use with Int32 is the Parse method for converting the string representation of a number into an integer. Here’s an example:
using System;
public class IntStruct
{
    static void Main()
    {
        int num;
        string snum;
        Console.Write("Enter a number: ");
        snum = Console.ReadLine();
        num = Int32.Parse(snum);
        Console.WriteLine(num);
    }
}

No comments:

Post a Comment