
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to select a random element from a C# list?
Firstly, set a list in C#.
var list = new List<string>{ "one","two","three","four"};
Now get the count of the elements and display randomly.
int index = random.Next(list.Count); Console.WriteLine(list[index]);
To select a random element from a list in C#, try to run the following code −
Example
using System; using System.Collections.Generic; namespace Demo { class Program { static void Main(string[] args) { var random = new Random(); var list = new List<string>{ "one","two","three","four"}; int index = random.Next(list.Count); Console.WriteLine(list[index]); } } }
Output
three
Advertisements