Quantcast
Channel: whileloop » code
Viewing all articles
Browse latest Browse all 10

simple random alpha char string variable in C#

$
0
0

Just a quick little code sample which creates a variable which generates a random alpha character string (upper and lower case). This can be very useful when you need to randomise a name, or create one or more temporary directories or files on the fly.

private const int _length = 15;
private static string RandomAlphaString {
	get {
		var ret = "";
		var random = new Random();
		while (ret.Length < _length)
			if (random.Next(1, 9) % 2 == 0)
				ret = ret + Convert.ToChar(random.Next(65, 90));
			else
				ret = ret + Convert.ToChar(random.Next(97, 122));
		return ret;
	}
}

Usage is to declare a var within your code, such as var randomName = RandomAlphaString; and this will be createda a string of length _length randomly using upper and lower case alpha characters (letters).



Viewing all articles
Browse latest Browse all 10

Trending Articles