Skip to content

Basic Class Library Items

A quick introduction to the C# string class

C# treats the string class in C# as a basic type, although it is a reference type. It includes a keyword, string, which maps to System.String. Because of this, you do things like string.IsNullOrEmpty(null), or refer to any other class method. (You can actually do this with any primitive type in C#, as the keywords map to classes that represent the actually data types. C# uses a form of autoboxing/unboxing similar to Java)

Strings are not mutable in C#, just like in Java. This means you are subject to the same performance issues in C# as in Java. Consider using the StringBuilder class as you would use the class of the same name in Java.

An introduction to Windows forms.

To create windows, you must utilize the classes in the System.Windows.Forms namespace. Visual Studio includes a form editor that you can used to create windows for your programs. When you create a program, simply select the Windows Forms project type. The best way to become familiar with Windows Forms is to experiment and see what you can do.

It is very useful to know how to display debugging information with a message dialog box. When you are using System.Windows.Forms, simply call 

MessageBox.Show("This is your message");

This can be very handy to get debugging information when you do not have a console to which you can print debugging information.

Using the .NET Collections

In my opinion, the .NET collections are not yet as developed as Java collections. They do have several classes, though, that are very handy. I recommend using the Generic versions of the collections found in the System.Collections.Generic namespace. Here is a quick listing of some of the collections I have found to be the most useful.

Table #3 – Common .NET Collections

Class Name

Description

List<T>

A linked list data structure

Queue<T>

A simple FIFO queue

Stack<T>

A simple LIFO stack

ArrayList<T>

An array list data structure

BitArray

An array for quick manipulation of arrays of single bits

SortedList<Key, Value>

Provides a list that is always sorted

Dictionary<Key, Value>

Provides a key/value mapping

 

Keep in mind, that like Java, these data structures are not thread safe. Refer to the .NET threading libraries section on how to synchronize access to these data structures.

Using .NET I/O

.NET provides a layered I/O system, very similar to Java. In this section, I will discuss the StreamReader and StreamWriter classes, which are wrappers around other streams for reading and writing text to files. An example using these classes is as follows:

using (StreamWriter sw = new StreamWriter("test.txt"))

    sw.Write("test");
}

using (StreamReader sr = new StreamReader("test.txt"))

    Console.WriteLine(sr.ReadLine());
}

This should seem somewhat straight forward, except for the using construct.

Using the using construct

The reason why we use the using construct is the same reason why you have to close streams in Java. You do not know exactly when the garbage collector will destroy the stream, and the stream thereby closed.

Because we do not know when the garbage collector will destroy objects in C#, we cannot use object destructors as we would in C++. To solve this issue, classes such as streams implement the IDisposable interface. This interface provides a single method, called Dispose. This method, in the case of IO streams, closes the stream.

The using construct takes as its only argument the initialization of an object that implements the IDisposable interface. At the end of the block, .NET will call the Dispose method on that object, and it will fall out of scope. This ensures that you properly close your streams.

It is important to note that it will not generate an error if you do not use the using block. If you do not use it, you must explicitly call the Close method for each stream. I strongly recommend you get in the habit of always using the using block. If it is a habit to use it with IO, you will not run into bugs with open streams.

How to consult the MSDN documentation

The MSDN documentation contains a huge amount of information about the .NET class library and the C# programming language. I often find it useful to read the documentation for individual classes. To do that, go the MSDN homepage at http://www.msdn.com/. Click on library, and then navigate the following path to the .NET class library directory in the tree to the left: MSDN Library à .NET Development à .NET Framework 3.5 à .NET Framework Class Library.

Searching with Google for things on the MSDN site is almost easier than searching with the MSDN search tool. If you try searching for something and do not have any luck, try Google.

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.