Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

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.

The System::Threading Namespace

Threading has been a difficult part of Windows programming from the very beginning. It's quite a bit simpler in .NET. The vital classes for threading are in the System::Threading namespace. These include classes such as Mutex, Thread, and ThreadPool, which developers with experience in threaded applications will recognize instantly.

A thread is a path of execution through a program. In a multithreaded program, each thread has its own stack and operates independently of other threads running within the same program.

How Many Threads?

Any application always has at least one thread, which is the program's primary or main thread. You can start and stop as many additional threads as you need, but the main thread keeps running as long as the application is active.

A thread is the smallest unit of execution, much smaller than a process. Generally each running application on your system is a process. If you start the same application twice (for example, Notepad), there are two processes: one for each instance. It is possible for several instances of an application to share a single process: For example, if you choose File, New Window in Internet Explorer, two applications appear on your taskbar, and they share a process. The unfortunate consequence is that if one instance crashes, they all do.

Writing a multithreaded application is simple with classes from the System::Thread namespace. First, you need to think of some work for a new thread to do: some slow calculation that you want to run without slowing the responsiveness of your application. This work will go into a function, and the function will be executed on a new thread.

The Thread Proc

For a long time, Windows C++ programmers have called the function the thread proc (short for procedure).

Your thread proc must be a member function of a garbage-collected class. For example

public __gc class Counter
  void Countdown()
   String* threadName = Thread::CurrentThread->Name;
   Console::Write(S"This is the current thread: ");
   for (int counter = maxCount; counter >= 1; counter--)
     Console::WriteLine(S"{0} is currently on {1}.",threadName,
   Console::WriteLine(S"{0} has finished counting down from {1}.",

The heart of this function is the for loop that counts down from maxCount to zero. It uses the Name property of the thread that is executing the function, because the sample that uses this class calls the function on two different threads. Normally, the function that does the asynchronous work would not communicate with the user; it would be quietly working in the background, doing some long slow work. It might, however, update a progress bar, or an icon that indicates a background task is in progress.

This code calls the thread proc on a new thread and also on the application's main thread:

Console::Write("Enter a number to count down from: ");
maxCount = Int16::Parse(Console::ReadLine());
Thread::CurrentThread->Name = "Main Thread";

Counter* counter = new Counter();
ThreadStart* SecondStart = new ThreadStart(counter,Counter::Countdown);
Thread* secondThread = new Thread(SecondStart);
secondThread->Name = "Secondary Thread";


This code keeps the number entered by the user in a global variable called maxCount. Because ReadLine returns a pointer to a System::String instance, the static Parse() method of Int16 is used to convert a string to an integer so that it can be stored in maxCount.

To execute a particular function on a new thread, you create a Thread object and call its Start() method. To create a Thread object, you need a ThreadStart object, and the constructor for ThreadStart needs a reference to an instance of a garbage-collected class (counter in this example), and a function pointer (just the name of the function without the parentheses) to a member function of that garbage-collected class.

Because this code calls Countdown on a new thread and also on the main thread, the output shows the two threads taking turns, as in Figure 3.1.

Figure 3.1Figure 3.1 A multithreaded application demonstrates how threads take turns doing their work.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020