You can generate a random string of specified length in C# using the following code:
main.cs259 chars7 lines
Here, we are first defining a string chars
that includes all the possible characters that we want to include in our random string. Then, we are initializing a new instance of the Random
class to generate random numbers.
Finally, we are using the Enumerable.Repeat()
method to repeat the chars
string length
number of times, and then using the Select()
method to randomly select a character from each of the repeated chars
strings. We then convert this array of characters into a string using the String
constructor.
To use this method, you can call it with the desired length of the random string:
main.cs48 chars2 lines
This will generate a random string of length 10.
gistlibby LogSnag