views:

21

answers:

1

Hi I'm trying to reach this goal but as far as I am, I've got nothing. I wish to add some static method to string type, which would return new changed string. I've got: using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
namespace TestProject.Models
{
    public static class Extension
    {
        public static string md5(this string input)
        {

            MD5 HashAlgorithm = new MD5CryptoServiceProvider();
            Byte[] InputsBytes = Encoding.UTF8.GetBytes(input3);
            Byte[] HashedInput = HashAlgorithm.ComputeHash(InputsBytes);
            return BitConverter.ToString(HashedInput);
        }
    }
}

honestly I just don't know where should it be. I put it in my models catalog but I'm pretty sure it's wrong. Where should it be? And what then? I'd like to use it in this way:

string hashedString = String.md5(input); 
A: 

try this : string hashedString = input.md5();

Extension methods are compiler trick the actual call is just a normal static method call.

The compiler just turns the code into:

string hashedString = TestProject.Models.Extentions.md5(input)

Preet Sangha
it helps! ;) but why is that if I daclare it as a static method? is it feature of extensions?
trn
The method is static because you don't need an object of the Extension class to use it.
Paul Creasey
right, now it's obvious. thx for help and explanation ;)
trn