Posts

Showing posts from August, 2011

C# Parallel.For

Image
With the advent of C# 4.0 you can now write code to more efficiently use multiple core machines. I will show a trivial example on how to do this. This code reads through a directory and lists all the files. Now while the example itself isn't that complicated I will show some timings and benefits on my Intel duo core 2 machine. Running on even more cores would show an even bigger result. This kind of computing really shines when you're dealing with large amounts of data or very intensive computations. This test sums up 10 million random integers. using System.Threading.Tasks; static void Main( string[] args ) { int[] values = new int[10000000]; Random r = new Random( DateTime.Now.Millisecond ); double sum = 0; for( int i = 0; i < values.Length; i++ ) { values[i] = r.Next( 0, 10 ); } DateTime start = DateTime.Now; DateTime end; // run the test in parallel Parallel.For( 0,

C# create MD5 hash from a string

A common task when dealing with sensitive data is to create a hash of it. MD5 is used quite often especially when dealing with password, etc. I will leave the discussion of collisions, etc to others and just show a function that will return an array of bytes that is the result of hashing the input string. This function calls for an Encoding object to be passed in since you never know what encoding you will be using. But you could easily hard code the encoding if you know that will never change. using System.Security.Cryptography; /// <summary> /// Hashes the string. /// </summary> /// <param name="valueToHash">The value to hash.</param> /// <param name="textEncoding">The text encoding.</param> /// <returns></returns> private byte[] HashString( String valueToHash, Encoding textEncoding ) { byte[] result; using( MD5 md5 = MD5.Create() ) { byte[] inputBytes = textEncoding.GetBytes( val

C# get the number of days in a month

Image
Ok this isn't the longest blog post, but it contains a handy little code snippet. Sometimes you find yourself needing to know the number of days in any given month. The code below will get you the last day of the month. I'm using the current month but you could use any month. You just need to know the month and year you're looking for. The magic lies in the DateTime.DaysInMonth method. You pass in the year and month and it returns the number of days in that month. The code below is pretty self explanatory. static void Main( string[] args ) { DateTime now = DateTime.Now; int daysInMonth = DateTime.DaysInMonth( now.Year, now.Month ); String dateFormat = String.Format( "Last day of the month is - {0}/{1}/{2}", now.Month, daysInMonth, now.Year); Console.WriteLine( dateFormat); Console.ReadKey(); } And a brief picture showing the output, obviously I ran this is October 2011.

C# Get the ip address of a host name

Often times you want to find the ip address of a host name. This function will do this for you. It returns the first address. using System.Net; public string GetIPAddress(string sHostName) { IPHostEntry ipEntry = Dns.GetHostByName(sHostName); IPAddress [] addr = ipEntry.AddressList; string sIPAddress = addr[0].ToString(); return sIPAddress; }