The .NET 2.0 upgrade included many minor improvements that are easily overlooked. One such improvement is Padding, a handy structure in the System.Windows.Forms namespace that you may find useful for representing offsets, margins or padding in the user interface.
Delegates can be tricky to understand. Think of a delegate as a reference to a method (pointer to a function).
You can count the number of constants defined in a C# enumeration with the static Enum.GetNames method. This returns an Array containing the names of the constants defined in the enumeration. You can then check the Array’s Length property.
C# includes a number of pre-defined “built-in” data types. Each built-in data type is represented by a class in the System namespace that inherits from the base System.Object class. For example, an integer is represented by the System.Int32 class, and a string is represented by the System.String class.
C# defines an alias keyword for each built-in type. The alias keyword and its corresponding C# type are interchangeable. For example, you can define a string with the “string” keyword or “System.String” type:
The .NET string class is quite comprehensive, yet some common string functions are missing or not entirely obvious. This article provides quick tips on using .NET strings.
Did you know? You can type ASCII characters into any application. Press and hold the Left-Alt key, then using the numeric keypad, type the four-digit decimal number for the ASCII character you want, then release the Left-Alt key. For example, to type è, press and hold Left-Alt, then type 0232 on the numeric keypad. When you release the Left-Alt key, è will be typed.
Here is the world famous ASCII table:
The Microsoft .NET Framework is quite comprehensive, but occasionally an obvious function slips through the cracks and you have to use InteropServices to access the Windows API.
One such obvious miss is the ability to truncate a file path. If you are drawing text and know the font and desired output size, you can use the WinForms TextRenderer class. But to truncate a file path to a specific number of characters, you need the “Shell Lightweight Utility Library” function PathCompactPathEx:
The version of .NET against which you compile an application or assembly may not be the same version of .NET on which the application is currently running. A .NET application should always be able to run on the same or newer version of .NET against which it was compiled.
This is because .NET is backward compatible. This means that an application compiled on .NET v1.1 should run OK on .NET v2.0 and v3.0. But an application compiled on .NET v2.0 will not run on .NET v1.1.
You can use the following JavaScript code in a web page to determine which versions of .NET are installed on a client PC:
Changing a font style is a bit easier than changing its size, as there is a Font constructor that accepts a font and style as arguments. For example, to bold a label’s font: