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.
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.
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:
An inspection of the Font class will reveal that every public property is read-only. This means to change a font’s size, you need to create a new Font object with all the same properties of your current font but with the new size. Here is a handy method to do just that:
When you show a .NET Form, by default the form will appear in the Windows Start bar and in the list of open windows shown when the user presses Alt+Tab.
This multi-part article answers common questions about assemblies, the basic building blocks of .NET applications. This Part 4 covers shared assemblies and the Global Assembly Cache.
The .NET Framework v2.0 did a nice job filling many holes in the System.IO namespace, especially when it comes to managing the file system. One such addition is the DriveInfo class, which enables you to determine what drives are available, their type, capacity and available free space.