February 25, 2021
Hot Topics:

Sample Chapter: The .NET Base Class Libraries

  • By Kate Gregory
  • Send Email »
  • More Articles »

Other Useful Namespaces

There are plenty of other useful classes contained in sub-namespaces of the System namespace. Some are covered elsewhere in this book. Four are covered here because almost every .NET developer is likely to use them: System::IO, System::Text, System::Collections, and System::Threading.

The System::IO Namespace

Getting information from users and providing it to them is the sort of task that can be incredibly simple (like reading a string and echoing it back to the users) or far more complex. The most basic operations are in the Console class in the System namespace. More complicated tasks are in the System::IO namespace. This namespace includes 27 classes, as well as some structures and other related utilities. They handle tasks such as

  • Reading and writing to a file

  • Binary reads and writes (bytes or blocks of bytes)

  • Creating, deleting, renaming, or moving files

  • Working with directories

This snippet uses the FileInfo class to determine whether a file exists, and then deletes it if it does:

System::IO::FileInfo* fi = new System::IO::FileInfo("c:\\test.txt");
if (fi->Exists)

This snippet writes a string to a file:

System::IO::StreamWriter* streamW = new System::IO::StreamWriter("c:\\test.txt");
streamW->Write("Hi there" );

Be sure to close all files, readers, and writers when you have finished with them. The garbage collector might not finalize the streamW instance for a long time, and the file stays open until you explicitly close it or until the instance that opened it is finalized.

When Typing Strings with Backslashes - The backslash character (\) in the filename must be "escaped" by placing another backslash before it. Otherwise the combination \t will be read as a tab character. This is standard C++ behavior when typing strings with backslashes.

Check the documentation to learn more about IO classes that you can use in console applications, Windows applications, and class libraries. Keep in mind also that many classes can persist themselves to and from a file, or to and from a stream of XML.

The System::Text Namespace

Just as Console offers simple input and output abilities, the simplest string work can be tackled with just the String class from the System namespace. More complicated work involves the System::Text namespace. You've already seen System::Text::StringBuilder. Other classes in this namespace handle conversions between different types of text, such as Unicode and ASCII.

The System::Text::RegularExpressions namespace lets you use regular expressions in string manipulations and elsewhere. Here is a function that determines whether a string passed to it is a valid US ZIP code:

using namespace System;
using namespace System::Text::RegularExpressions;
// . . .
String* Check(String* code)
  String* error = S"OK";
  Match* m;
  switch (code->get_Length())
  case 5:
    Regex* fivenums;
    fivenums = new Regex("\\d\\d\\d\\d\\d");
    m = fivenums->Match(code);
    if (!m->Success) 
      error = S"Non numeric characters in 5 digit code";
  case 10:
    Regex* fivedashfour;
    fivedashfour = new Regex("\\d\\d\\d\\d\\d-\\d\\d\\d\\d");
    m = fivedashfour->Match(code);
    if (!m->Success) 
      error =  S"Not a valid zip+4 code";
    error = S"invalid length";
  return error;

The Regex class represents a pattern, such as "five numbers" or "three letters." The Match class represents a possible match between a particular string and a particular pattern. This code checks the string against two patterns representing the two sets of rules for ZIP codes.

The syntax for regular expressions in the .NET class libraries will be familiar to developers who have used regular expression as MFC programmers, or even as UNIX users. In addition to using regular expressions with classes from the System::Text namespace, you can use them in the Find and Replace dialog boxes of the Visual Studio editor, and with ASP.NET validation controls. It's worth learning how they work.

Regular Expression Syntax

A regular expression is some text combined with special characters that represent things that can't be typed, such as "the end of a string" or "any number" or "three capital letters."

When regular expressions are being used, some characters give up their usual meaning and instead stand in for one or more other characters. Regular expressions in Visual C++ are built from ordinary characters mixed in with these special entries, shown in Table 3.2.

Here are some examples of regular expressions:

  • ^test$ matches only test alone in a string.

  • doc[1234] matches doc1, doc2, doc3, or doc4 but not doc5.

  • doc[1-4] matches the same strings as doc[1234] but requires less typing.

  • doc[^56] matches doca, doc1, and anything else that starts with doc, except doc5 and doc6.n -H\~ello matches Hillo and Hxllo (and lots more) but not Hello. H[^e]llo has the same effect.

  • [xy]z matches xz and yz.

  • New *York matches New York, NewYork, and New York (with several spaces between the words).

  • New +York matches New York and New York, but not NewYork.

  • New.*k matches Newk, Newark, and New York, plus lots more.

  • World$ matches World at the end of a string, but World\$ matches only World$ anywhere in a string.

Table 3.2 Regular Expression Entries




Start of the string.


End of the string.


Any single character.


Any one of the characters within the brackets (use for a range, ^ for "except").


Anything except the character that follows.


Zero or more of the next character.


One or more of the next character.


A single letter or number, or an underscore. (These are called word characters and are the only characters that can be used in a variable name.)


Whitespace (tabs or spaces).


A single numerical digit.


Removes the special meaning from the character that follows.

The System::Collections Namespace

Another incredibly common programming task is holding on to a collection of objects. If you have just a few, you can use an array to read three integers in one line of input. In fact, arrays in .NET are actually objects, instances of the System::Array class, which have some useful member functions of their own, such as Copy(). There are times when you want specific types of collections, though, and the System::Collections namespace has plenty of them. The provided collections include

  • Stack. A collection that stores objects in order. The object stored most recently is the first taken out.

  • Queue. A collection that stores objects in order. The first stored is the first taken out.

  • Hashtable. A collection that can be searched far more quickly than other types of collections, but takes up more space.

  • ArrayList. An array that grows as elements are added to it.

  • SortedList. A collection of two-part (key and value) items that can be accessed by key or in numerical order.

  • BitArray. A compact way to store an array of true/false flags.

One rather striking omission here is a linked list. You have to code your own if you need a linked or double-linked list.

Page 4 of 5

This article was originally published on January 27, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date