Tuesday, December 02, 2008

Some current C# test snippets...

While coding, I normally have a spare devenv (VS2008) open with a simple winforms app where I add buttons in order to test little code snippets.

Nothing very exciting, but here are some of my current snippets:

Question: What does Int32.Parse do on string.Empty?

string test = string.Empty;
int i = Int32.Parse(test);

Answer:
FormatException


Question: Debug a regex for friendly urls?

Match m = Regex.Match("http://www.stuff.and.nonsense.com/Tag/the-tag", "Tag/(?.*)");
Debug.WriteLine(m);
Match m2 = Regex.Match("http://www.stuff.and.nonsense.com/Tag/", "Tag/(?.*)");
Debug.WriteLine(m2);
Match m3 = Regex.Match("http://www.stuff.and.nonsense.com/Tag/tagwith/slashes/and.dots", "Tag/(?.*)");
Debug.WriteLine(m3);
Match m4 = Regex.Match("http://www.stuff.and.nonsense.com/Tag/tagwith/Tag/and.Tag/in/it", "Tag/(?.*)");
Debug.WriteLine(m4);

Answer: Looks good


Question: Generate me a machinekey and a decryptionkey for web.config

Answer: (sorry - but I've lost the link I borrowed this from and Google returns a lot of hits for this code - so who knows where it came from originally?!)

int len = 32; //32 for decryptionkey, 64 for machinekey
byte[] buff = new byte[len / 2];
RNGCryptoServiceProvider rng = new
RNGCryptoServiceProvider();
rng.GetBytes(buff);
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < buff.Length; i++)
sb.Append(string.Format("{0:X2}", buff[i]));
Debug.WriteLine(sb);


Question: How do I send mail by smtp?

Answer:
SmtpClient client = new SmtpClient("mail.mydomain.com", 587);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("donotreply@mydomain.com", "mypassword");
MailMessage msg = new MailMessage("donotreply@mydomain.com", "me@testdomain.com");
msg.Body = "Hello";
msg.Subject = "Test body";
client.Send(msg);

Aside:
There's also an HTML version of this.... uses
msg.Subject = "Test body";
msg.IsBodyHtml = true;


Question:Can I be lazy when using string.Substring?

Answer: No - if you pass in too long a string length, then it will exception (we're not in Python now)

string a = "aaaa";
string b = a.Substring(0, 10); // yup - this causes ArgumentOutOfRangeException


Question: Can I get the name of the current method?

Answer:
StackTrace stack = new StackTrace(true);
Debug.WriteLine(stack.GetFrame(0).GetMethod().Name);

Can also get the class name using:
Debug.WriteLine(stack.GetFrame(0).GetMethod().ReflectedType.FullName);



Question: A less than string for use in one particular LINQ query

(This probably needs a bit of explanation.... it's basically a StartsWith test - I have a whole load of strings like: AdamA, AdamB, AdamC, etc which I'd like to match - the first string I don't want to match is Adan. Also, it's worth noting that I'm working in a specific domain here where I don't care about outofbounds testing - i.e. I know Adam won't be ending with a char 0xFFFF)

Answer:
string lessThanString = compareString.Substring(0, compareString.Length - 1) + (char)(compareString[compareString.Length - 1] + 1);


This is then used in my Azure storage LINQ query as:

IEnumerable query = from c in svc.CreateQuery(_tableName)
where c.RowKey.CompareTo(compareString) >=0 && c.RowKey.CompareTo(lessThanClause) < 0
select c;


That's all for now

No comments:

Post a Comment