BETA
 
A Collection Of Useful .NET Functions C#
Category: Computers
Views: 6,141  

This is a collection of useful .NET functions (C#) that every developer must have. It will save you a lot of repetitive tasks. For beginners, it's also a great learning reference. Please post comments if you need help using them. Happy programming!

Recommend New Item
 DescriptionAverage RatingComments
1 .
Pad Left

/// <summary>
        /// pads a string to the left with the specified total
        /// </summary>
        /// <param name="inString"></param>
        /// <param name="totalWidth"></param>
        /// <param name="paddingString"></param>
        /// <returns></returns>
        public static string PadLeft( string inString, int repeat, string paddingString )
        {
            string temp = "";

            for ( int i=0; i < repeat; i ++ )
            {
                temp += paddingString;
            }
            inString = temp + inString;

            return inString;
        }
     
View Comments (2)
Add Rating
2 .
Merges a class with a template text

        /// Merges a class with a template text, data place holders are wrapped around{}
    
        public static string DataMergeTemplate(string template, object ClassObject)
        {            
            Type t = ClassObject.GetType();

            string newTemplate = template;
            System.Reflection.FieldInfo[] finfos = t.GetFields();
            foreach (System.Reflection.FieldInfo fifo in finfos)
            {
                string v = fifo.GetValue(ClassObject).ToString();
                newTemplate = newTemplate.Replace("{" + fifo.Name.ToLower() + "}", v);
            }

            foreach ( System.Reflection.PropertyInfo pifo in t.GetProperties() )
            {
                string v = pifo.GetValue(ClassObject, null).ToString();
                if (pifo.Name != null)
                {
                    newTemplate = newTemplate.Replace("{" + pifo.Name.ToLower() + "}", v);
                }
            }

            return newTemplate;
        }
     
View Comments (0)
Add Rating
3 .
HexToInt

public static int HexToInt(String hexstr)
            // This method converts a hexvalues string as 80FF into a integer.
            //    Note that you may not put a '#' at the beginning of string! There
            // is not much error checking in this method. If the string does not
            // represent a valid hexadecimal value it returns 0.
        {
            int counter,hexint;
            char[] hexarr;
            hexint=0;
            hexstr=hexstr.ToUpper();
            hexarr=hexstr.ToCharArray();
            for (counter=hexarr.Length-1;counter>=0;counter--)
            {
                if ((hexarr[counter]>='0') && (hexarr[counter]<='9'))
                {
                    hexint+=(hexarr[counter]-48)*((int)(Math.Pow(16,hexarr.Length-1-counter)));
                }
                else
                {
                    if ((hexarr[counter]>='A') && (hexarr[counter]<='F'))
                    {
                        hexint+=(hexarr[counter]-55)*((int)(Math.Pow(16,hexarr.Length-1-counter)));
                    }
                    else
                    {
                        hexint=0;
                        break;
                    }
                }
            }
            return hexint;
        }

     
View Comments (1)
Add Rating
4 .
IntToHex

public static String IntToHex(int hexint)
            // This method converts a integer into a hexadecimal string representing the
            // int value. The returned string will look like this: 55FF. Note that there is
            // no leading '#' in the returned string!
        {
            int counter,reminder;
            String hexstr;

            counter=1;
            hexstr="";
            while ( hexint + 15 > Math.Pow( 16, counter - 1 ) )
            {
                reminder = (int)( hexint % Math.Pow( 16, counter ) );
                reminder = (int)( reminder / Math.Pow( 16, counter - 1 ) );
                
                if (reminder<=9)
                {
                    hexstr = hexstr + (char)( reminder + 48 );
                }
                else
                {
                    hexstr = hexstr + (char) ( reminder + 55 );
                }

                hexint -= reminder;
                counter ++;
            }
            return ReverseString( hexstr );
        }

        public static String IntToHex(int hexint,int length)
            // This version of the IntToHex method returns a hexadecimal string representing the
            // int value in the given minimum length. If the hexadecimal string is shorter then the
            // length parameter the missing characters will be filled up with leading zeroes.
            // Note that the returend string though is not truncated if the value exeeds the length!
        {
            String hexstr,ret;
            int counter;
            hexstr=IntToHex(hexint);
            ret="";
            if (hexstr.Length<length)
            {
                for (counter=0;counter<(length-hexstr.Length);counter++)
                {
                    ret=ret+"0";
                }
            }
            return ret+hexstr;
        }

     
View Comments (0)
Add Rating
5 .
ToHexString

/// <summary>
        /// Encodes non-US-ASCII characters in a string, good for encoding file names for download
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToHexString(string s)
        {
            char[] chars = s.ToCharArray();
            StringBuilder builder = new StringBuilder();
            for(int index=0; index<chars.Length; index++)
            {
                bool needToEncode = NeedToEncode(chars[index]);
                if(needToEncode)
                {
                    string encodedString = ToHexString(chars[index]);
                    builder.Append(encodedString);
                }
                else
                {
                    builder.Append(chars[index]);                    
                }
            }
            
            return builder.ToString();
        }
        
        /// <summary>
        /// Determines if the character needs to be encoded.
        /// </summary>
        /// <param name="chr"></param>
        /// <returns></returns>
        private static bool NeedToEncode(char chr)
        {
            string reservedChars = "$-_.+!*'(),@=&";    

            if(chr>127)
                return true;
            if(char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr)>=0)
                return false;

            return true;
        }
        
        /// <summary>
        /// Encodes a non-US-ASCII character.
        /// </summary>
        /// <param name="chr"></param>
        /// <returns></returns>
        private static string ToHexString(char chr)
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            byte[] encodedBytes = utf8.GetBytes(chr.ToString());
            StringBuilder builder = new StringBuilder();
            for(int index=0; index<encodedBytes.Length; index++)
            {
                builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
            }
            
            return builder.ToString();
        }
     
View Comments (0)
Add Rating
Page: << Previous  Next >>  1  2  3  4  5  6  


Author:

manit
Posts 86

Top Lists:more...
1 . Useful functions within C... (0)
2 . A Collection Of Useful .N... (8)
3 . Best Restaurants in Addis... (21)
4 . Hot girls on MySpace (20)
5 . Best US cities to visit..... (12)
6 . Laugh-O-Potamus (12)
7 . It's the End of the World (5)
8 . Some of the craziest webs... (4)
9 . Ways to get pirated softw... (7)
10 . Things to know when trave... (7)

Top Users:more...
1 . manit - (86)
2 . manit - (86)
3 . LaughOPotamus - (29)
4 . LaughOPotamus - (29)
5 . Dawn - (7)
6 . Dawn - (7)
7 . eepah - (13)
8 . eepah - (13)
9 . ivan - (6)
10 . ivan - (6)