The collection of extension methods in C#

No.of Views1812
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  15 Aug 2010 09:08:18
Tag : CSharp , Miscellaneous
In this codesnippet, you can see examples for extension methods in C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this code snippet i have given no of useful examples extension methods with C#. The extension methods help to us rewrite code again and again. And also it does adapt with existing class and object.

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{class ExtensionSamples
    {public static bool IsWholeNumber(this string strNumber)
        {if (strNumber == "")return false;
            Regex objNotWholePattern = new Regex("[^0-9]");return !objNotWholePattern.IsMatch(strNumber);
        }public static bool IsDouble(this string strNumber)
        {if (strNumber == "")return false;try{
                Convert.ToDouble(strNumber);
            }catch (Exception)
            {return false;
            }return true;
        }public static bool IsAlphaNumeric(this string strToCheck)
        {bool valid = true;if (strToCheck == "")return false;
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
            valid = !objAlphaNumericPattern.IsMatch(strToCheck);return valid;
        }public static bool IsValidAlphaNumericWithSpace(this string strToCheck)
        {bool valid = true;if (strToCheck == "")return false;
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9\\s]");
            valid = !objAlphaNumericPattern.IsMatch(strToCheck);return valid;

        }public static bool IsValidAlphabetWithSpace(this string strToCheck)
        {bool valid = true;if (strToCheck == "")return false;
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z\\s]");
            valid = !objAlphaNumericPattern.IsMatch(strToCheck);return valid;
        }public static bool IsValidAlphabetWithHyphen(this string strToCheck)
        {bool valid = true;if (strToCheck == "")return false;
            Regex objAlphaNumericPattern = new Regex("[^a-zA-Z\\-]");
            valid = !objAlphaNumericPattern.IsMatch(strToCheck);return valid;
        }public static bool IsAlpha(this string strToCheck)
        {bool valid = true;if (strToCheck == "")return false;

            Regex objAlphaPattern = new Regex("[^a-zA-Z]");
            valid = !objAlphaPattern.IsMatch(strToCheck);return valid;
        }public static bool IsNumber(this string strNumber)
        {try{
                Convert.ToDouble(strNumber);return true;
            }catch{return false;

            }

        }public static bool IsInteger(this string strInteger)
        {try{
                Convert.ToInt32(strInteger);return true;
            }catch{return false;
            }

        }public static bool IsDateTime(this string strDateTime)
        {try{

                Convert.ToDateTime(strDateTime);return true;
            }catch{return false;
            }
        }public static bool isEmail(this string inputEmail)
        {if (inputEmail != null && inputEmail != "")
            {string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

                Regex re = new Regex(strRegex);if (re.IsMatch(inputEmail))return (true);elsereturn (false);

            }elsereturn (false);

        }

    }
}

 I hope this is help to you all.thank you for reading

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top