Several Extension Methods for Char

Extension methods -- this new feature of C# 3.0 is good. Just write several simple methods for convenient operations on 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);
            }

        }

    }


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));
        }

    }


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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值