Tag Archives: ToString

Display locale-aware sample dates and currency values in C#

This program uses the following simple CultureData class to store information about a locale. // A class to store cultre data. public class CultureData { // The CultureInfo. public CultureInfo Info; // Initializing constructor. public CultureData(CultureInfo info) { Info = … Continue reading

Posted in internationalization, localization | Tagged , , , , , , , , , , , , , , , | Leave a comment

Make locale-aware methods use a new culture at runtime in C#

The example Change program locale at runtime in C# explains how to change a program’s locale and reload its controls for the new locale. However, that doesn’t change the thread’s current culture, which determines the formats used by locale-aware methods … Continue reading

Posted in internationalization, localization | Tagged , , , , , , , , , , , , , , , | Leave a comment

Add useful extensions to the BitArray class in C#

The example Use the BitArray class in C# explains how to use the BitArray class. This example adds some useful features to the BitArray class as extension methods. The following code shows the NumTrue and NumFalse extension methods, which respectively … Continue reading

Posted in classes, extension methods | Tagged , , , , , , , , | Leave a comment

Add a ToString extension method to the TimeSpan structure in C#

Until .NET Framework version 4.0, you could not pass the TimeSpan structure’s ToString method a format string, so the result was always in the form hh:mm:ss.fffffff where hh is hours, mm is minutes, ss is seconds, and fffffff is 7 … Continue reading

Posted in extension methods, parsing | Tagged , , , , , , , , , , | 2 Comments

Use custom numeric formats in C#

You can use formatting characters to display custom numeric formats for numeric values. These formats work with a variable’s ToString method as well as with string.Format. For example, the code (1.234).ToString(“0.00″) returns the value “1.23.” This example builds a ListView … Continue reading

Posted in formatting, strings | Tagged , , , , , , , , , , , , | Leave a comment

Use custom date and time formats in C#

Note that custom date and time formats may not produce the result you want when you program runs in different locales. For example, the date January 23, 2010 should be displayed as 1/23/2010 in the United States but in as … Continue reading

Posted in formatting, strings | Tagged , , , , , , , , , , , , | Leave a comment

Use standard numeric formats in C#

You can use numeric formatting characters to display numbers in particular formats. These formats work with a variable’s ToString method as well as with String.Format. For example, the code value.ToString(“C”) returns the variable value formatted as currency. These formats are … Continue reading

Posted in formatting, strings | Tagged , , , , , , , , , , , , | Leave a comment

Use standard date and time formats in C#

You can use formatting characters to display date and time values in particular formats. These formats work with a variable’s ToString method as well as with String.Format. For example, the code DateTime.Now.ToString(“f”) returns the current date and time in full … Continue reading

Posted in formatting, strings | Tagged , , , , , , , , , , , , , , | 1 Comment

Use the DateTime class’s standard formatting methods in C#

The ToString and string.Format methods let you use formatting characters to format a DateTime value, but it’s not easy to remember all of the characters you might want to use. To make using standard formats easier, the DateTime class provides … Continue reading

Posted in formatting, strings | Tagged , , , , , , , , , , , , , , , | Leave a comment

Override ToString to allow controls such as ListBox to display objects in C#

All classes inherit the ToString method from the Object ancestor class. Controls such as ComboBox and ListBox use the ToString methods of the objects they contain to decide what to display. By default, ToString returns a class’s name, which is … Continue reading

Posted in classes, OOP, strings, syntax | Tagged , , , , , , , , , , , , , | 4 Comments