Posts Tagged ‘c#’

What are extension methods?

What are extension methods?

This method allows us to add a new static method to the existing classes.


class Program
        static void Main(string[] args)
            int x = 3;
    public static class MyMathExtension
        public static int factorial(this int x)
            if (x <= 1) return 1;
            if (x == 2) return 2;
                return x * factorial(x – 1);
Use Of This

  1.     Extension methods allow existing classes to be extended without relying on inheritance or having to change the class’s source code.
  2.     If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced i.e. extension methods.
  3.     This feature is important for all developers especially if you would like to use the dynamism of the C# enhancements to be taken place in your classes design.

Important points while using extension methods:

  1.     An extension method must be defined in a top-level static class.
  2.     An extension method with the same name and signature as an instance method will not be called.
  3.     Extension methods cannot be used to override existing methods.
  4.     The concept of extension methods cannot be applied to fields, properties or events.
  5.     Overuse of extension methods is not good style of programming.

Read Full Post »

encrypt decrypt password asp.net?

encrypt decrypt password asp.net with c#.


using System.Text;

private string Encryptdata(string password)
        string strmsg = string.Empty;
        byte[] encode = new byte[password.Length];
        encode = Encoding.UTF8.GetBytes(password);
        strmsg = Convert.ToBase64String(encode);
        return strmsg;

private string Decryptdata(string encryptpwd)
        string decryptpwd = string.Empty;
        UTF8Encoding encodepwd = new UTF8Encoding();
        Decoder Decode = encodepwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
        int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        decryptpwd = new String(decoded_char);
        return decryptpwd;

string strpassword = Encryptdata(TextBox1.Text); //Enc

string strpassword = Decryptdata(TextBox1.Text); //Dec

Read Full Post »