Format Data Based on Culture Information

Don’t let yourself fooled by the title, we’ll discuss the topics of Globalization here. You’ll need the System.Globalization and System.Threading namespaces and possibly Sysglobl.dll to build custom cultures.

A little terminology: today, culture will mean the language, currency, date-format, calendar, writing system and different names of a given group of people. There are two types of cultures: neutral, which only provides information of the language spoken, and specific culture, which provides information about the region/country. For example “es” means the Spanish neutral culture, and “es-MX” means the Mexican culture with the appropriate region information (such as the unit of currency, date format, etc.). There’s also a special culture, called Invariant culture. This one is associated with the English language, but not with any region/country. You’ll use this one when you need consistency across different cultures in your application.
Continue reading “Format Data Based on Culture Information”

A Quick Checklist

Here are the good news:

  • System
  • System.Collections
  • System.Collections.Generics
  • System.Collections.Specialized
  • System.Configuration
  • System.Configuration.Install
  • System.Diagnostics
  • System.Drawing
  • System.IO
  • System.IO.Compression
  • System.IO.IsolatedStorage
  • System.Globalization
  • System.Net.Mail
  • System.Reflection.Emit
  • System.Runtime.InteropServices
  • System.Runtime.Serialization
  • System.Runtime.Serialization.Formatters
  • System.Security
  • System.Security.AccessControl
  • System.Security.Authentication
  • System.Security.Crypthography
  • System.Security.Permission
  • System.Security.Policy
  • System.Security.Principal
  • System.Services.Process
  • System.Text
  • System.Threading
  • System.Xml.Serialization

You should know only these!

Debug and Trace a .NET Application

Debugging and tracing are both enables you to log information about your application’s health, current status, etc. The main difference is that debugging is only allowed (it runs only) in debug mode. You can define a debug mode by adding the #define DEBUG command at the top of your code (or compiling your code with the /d:DEBUG flag.

We’ll cover the following classes and attributes in this post:

  • Debug
  • Debugger
  • Debugger Attributes
    • DebuggerBrowsable
    • DebuggerDisplay
    • DebuggerHidden
    • DebuggerNonUserCode
    • DebuggerStepThrough
    • DebuggerTypeProxy
  • Trace
  • TraceListener
    • EventLogTraceListener
    • XmlWriterTraceListener
    • DelimitedListTraceListener

Continue reading “Debug and Trace a .NET Application”

Mailing and SMTP

As no doubt you’ll love it, .NET comes with the support of sending e-mail messages by SMTP. Unfortunately, it can’t act as a mail receiver, but we should overcome the pain and examine how to send emails with it.

The key namespace is System.Net.Mail, and there are two important classes: MailMessage, which represents an e-mail message, and SmtpClient, which provides methods to send it.

First let’s build our message by using the MailMessage class!

MailMessage represents an email message eager to be sent. It has the following constructors:

  • MailAddress from, MailAddress to
  • String from, String to
  • String from, String to, String subject, String body

Don’t worry about MailAddress, it just takes a string and verifies that it is a valid e-mail address. It also defines some useful properties such as DisplayName or User. Use it instead of just passing a string.
Continue reading “Mailing and SMTP”

Using Delegates and Events

Delegates are object-oriented function pointers that safely encapsulate a method. A delegate must be of type delegate, define a return type and optionally a parameter list, like this:

public delegate void ImDelegate(int x, int y);

This delegate then can point to any methods which returns void and takes two integer values as parameters. To try our new delegate add the following code:

public static void Add(int x, int x)
ImDelegate handler = Add;
Handler(12, 23);

Continue reading “Using Delegates and Events”

Access Files and Folders by Using the File System Classes

The .NET Framework provides a set of classes to work with the file system, thus creating, modifying and opening files, directories, monitoring changes, etc. These classes live in the System.IO namespace. Here’s a brief list of them, and what they are capable of:

Classes of System.IO
DriveInfo Class Provides information about installed drives on the system.
Directory Class Static class with static methods to work with the directory system.
DirectoryInfo Class A class to work with the directory system through instances of it.
File Class Static class with static methods to work with the file system.
FileInfo Class A class to work with the file system thorough instances of it.
FileSystemInfo Class The base class of DirectoryInfo and FileInfo. Use it when you work with both, or extend it.
FileSystemWatcher Class Look for changes in a given location, and forwards them to your code.

Continue reading “Access Files and Folders by Using the File System Classes”

Manage Data by Using Reader and Writer Classes

There are a bunch of reader/writer classes shipped with the .NET Framework. They inherit most of their functionality from the abstract TextReader/TextWriter class (except BinaryReader/Writer).

TextReader defines the following virtual methods:

  • Peek: allows you to view the next character in a stream without advancing the cursor of the given reader class.
  • Read: reads the next character from the stream and advances the cursor, too.
  • Read(char[] buffer, int index, int count): reads the characters between the specified index position and count-1.
  • ReadBlock: does exactly the same what the previous overload of Read.
  • ReadLine: reads a line from the current stream.
  • ReadToEnd: reads the current stream to the end.

TextWriter defines the following virtual methods:

  • Flush: forces the content of the buffer to be written to the underlying device, thus clears it.
  • Write: with 17 overloads, each one for writing a specific type.
  • WriteLine: 18 overloads, each one for writing a specific type, and ending the line with the defined NewLine string.

Continue reading “Manage Data by Using Reader and Writer Classes”