In this post, we are going to learn how to generate alphanumeric 4 Ways to generate an alphanumeric string in C#.
Random.Next() in C#
The C# Random class next() method is used to generate the random number non-negative random integer that is less than the passed max value.
Syntax
public virtual int Next ();
public virtual int Next (int minval,int maxVal);
Parameter
- minval :This is absolute lower bound of random number.
- Maxvalue :This is absolute upper bound of random number that need to generate,it should be greater than or equal to 0.
Returns
It returns 32 signed integer that is greater than equal to zero or less than maxval.
1. Random Class to generate an alphanumeric string in C#
In this example, we are using the C# Built-in Random Class next() method that is used to generate a random string that contains characters and numbers.
- We are converting constant string src_string that is containing all alphanumeric characters .
- We are generating alphanumeric string of same size as constant input string by using Random next() method.It returning a randon character each time.
- Using stringbuillder class Append() method to append each random character to generate a string.
C# Program
using System;
using System.Linq;
namespace ProgramExample
{
class Program
{
static void Main(string[] args)
{
var src_string = "devenumcomsoftwaredev01236789";
var random = new Random();
var str_builder = new StringBuilder();
for (int i = 0; i < src_string.Length; i++)
{
var alphnumeric_string = src_string[random.Next( src_string.Length)];
str_builder.Append(alphnumeric_string);
}
Console.WriteLine("generated alphnumeric string \n:{0}", str_builder);
}
}
}
Output
generated alphnumeric string :
s1a9muvmea1ov3ddvm83
2. LINQ to Generate alphanumeric string in C#
The C# Enumerable.Range() method generates a sequence of integral numbers within the specified range.
Syntax
IEnumerable<int> Range (int start, int count);
Parameters
- start : This is value of first integer in sequence.
- stop : The count of sequence integer to generate.
C# Program To generate alphnumeric number using Linq
In this example, we are generating a random string that contains characters and numbers by using LINQ along with the Random class.
The Enumerable method generates a sequence of integral numbers within the specified range,0 for start index and string. length is the count of sequence integer to generate.
using System;
using System.Linq;
namespace ProgramExample
{
class Program
{
static void Main(string[] args)
{
var src_string = "devenumcomsoftwaredev01236789";
var random = new Random();
var alphanumeric_string = new string(Enumerable.Range(0,src_string.Length).Select(x => src_string[random.Next(0, src_string.Length)]).ToArray());
Console.WriteLine("generated alphnumeric string \n:{0}", alphanumeric_string);
Console.ReadLine();
}
}
}
Output
generated alphnumeric string
:eedddfu9tnmmvfmwf9eod16t66d9u
3. RNGCryptoServiceProvider to generate alphanumeric string in C#
The RNGCryptoServiceProvider class GetBytes() method fills an array with a cryptographically strong sequence of a random value. These are recommended ways to generate an alphanumeric string in C#.
Syntax
public void override void GetBytes(byte[] array)
Parameters
- array :This is an array that will filled with with cryptographically strong sequence of random values.
Steps to generate strong secure alphanumeric string in C#
- First add System.Security.Cryptography namespace in the code.
- Create a object(crypto) of RNGCryptoServiceProvider class inside using statement.
- Call crypto.GetBytes() by passing the byte array as a argument to it.
- Convert result byte array to base64 string by using Convert.ToBase64String(bytes_array) method.
using System;
using System.Security.Cryptography;
namespace ProgramExample
{
class Program
{
static void Main(string[] args)
{
var random = new Random();
var bytes_array = new byte[32];
using (var crypto = new RNGCryptoServiceProvider()) crypto.GetBytes(bytes_array);
var alphanumeric_string = Convert.ToBase64String(bytes_array);
Console.WriteLine("generated alphnumeric string \n:{0}", alphanumeric_string);
Console.ReadLine();
}
}
}
Output
generated alphnumeric string
:Rap7yTrrSDQLBJHYz1OudGrJkYFdu/N5l6jH3hMmbaY=
4. Membership class to alphanumeric string in C#
In the web application sometimes this method can be used to generate the alphanumeric string in C#.
We are using the Membership.GeneratePassword() method to generate an alphanumeric string in this below example.
C# Program to generate alphanumeric string
using System;
using System.Web.Security;
namespace ProgramExample
{
public class conversion
{
public static void Main(string[] args)
{
var alphanumeric_string = Membership.GeneratePassword(26, 1);
Console.WriteLine("generated alphnumeric string \n:{0}", alphanumeric_string);
}
}
}
Output
generated alphnumeric string :
lEQm.Xz(6xSDi%F0omPo{};:w$
Summary
In this post, we have learned different 4 Ways to generate an alphanumeric string in C#.
- Random Class to generate an alphanumeric string in C#.
- LINQ to Generate alphanumeric string in C#.
- RNGCryptoServiceProvider to generate alphanumeric string in C#.
- Membership class to alphanumeric string in C#.