Extension methods -- this new feature of C# 3.0 is good. Just write several simple methods for convenient operations on Char.
A console application for test :
Output:
Using unchecked keyword to ignore the arithmetic overflow.
These codes were written several days ago, after that I wrote codes to check if these extension methods are thread-safe, and the result was as I wish, you can now build them into a class library and simplify your code dealing with char
/// <summary>
/// This helper class extends the
/// methods that Char offers
/// </summary>
internal static class CharHelper
{
/// <summary>
/// Increase the char value by an integer
/// </summary>
/// <param name="source">the source char value</param>
/// <param name="increament">the increament</param>
/// <returns>the increased char</returns>
public static Char Increase(this Char source, Int32 increament)
{
Int32 temp = (Int32)source;
// use unchecked keyword to deal with overflow
unchecked
{
return (Char)(temp + increament);
}
}
/// <summary>
/// Increase the char value by 1
/// </summary>
/// <param name="source">the source char value</param>
/// <returns>the increased char</returns>
public static Char Increase(this Char source)
{
Int32 temp = (Int32)source;
unchecked
{
return (Char)(++temp);
}
}
/// <summary>
/// Decrease the char value by an integer
/// </summary>
/// <param name="source">the source char value</param>
/// <param name="decreament">the decreament</param>
/// <returns>the decreased char</returns>
public static Char Decrease(this Char source, Int32 decreament)
{
Int32 temp = (Int32)source;
unchecked
{
return (Char)(temp - decreament);
}
}
/// <summary>
/// Decrease the char value by 1
/// </summary>
/// <param name="source">the source char value</param>
/// <returns>the decreased char</returns>
public static Char Decrease(this Char source)
{
Int32 temp = (Int32)source;
unchecked
{
return (Char)(--temp);
}
}
}
/// This helper class extends the
/// methods that Char offers
/// </summary>
internal static class CharHelper
{
/// <summary>
/// Increase the char value by an integer
/// </summary>
/// <param name="source">the source char value</param>
/// <param name="increament">the increament</param>
/// <returns>the increased char</returns>
public static Char Increase(this Char source, Int32 increament)
{
Int32 temp = (Int32)source;
// use unchecked keyword to deal with overflow
unchecked
{
return (Char)(temp + increament);
}
}
/// <summary>
/// Increase the char value by 1
/// </summary>
/// <param name="source">the source char value</param>
/// <returns>the increased char</returns>
public static Char Increase(this Char source)
{
Int32 temp = (Int32)source;
unchecked
{
return (Char)(++temp);
}
}
/// <summary>
/// Decrease the char value by an integer
/// </summary>
/// <param name="source">the source char value</param>
/// <param name="decreament">the decreament</param>
/// <returns>the decreased char</returns>
public static Char Decrease(this Char source, Int32 decreament)
{
Int32 temp = (Int32)source;
unchecked
{
return (Char)(temp - decreament);
}
}
/// <summary>
/// Decrease the char value by 1
/// </summary>
/// <param name="source">the source char value</param>
/// <returns>the decreased char</returns>
public static Char Decrease(this Char source)
{
Int32 temp = (Int32)source;
unchecked
{
return (Char)(--temp);
}
}
}
A console application for test :
class
Program
{
static void Main(string[] args)
{
Char c1 = 'c';
Console.WriteLine(c1.Increase());
Console.WriteLine(c1.Increase(65533));
Console.WriteLine(c1.Decrease());
Console.WriteLine(c1.Decrease(65536));
}
}
{
static void Main(string[] args)
{
Char c1 = 'c';
Console.WriteLine(c1.Increase());
Console.WriteLine(c1.Increase(65533));
Console.WriteLine(c1.Decrease());
Console.WriteLine(c1.Decrease(65536));
}
}
Output:
Using unchecked keyword to ignore the arithmetic overflow.
These codes were written several days ago, after that I wrote codes to check if these extension methods are thread-safe, and the result was as I wish, you can now build them into a class library and simplify your code dealing with char