
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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
- Related Articles
- Select random number from a specific list in MySQL?
- How to select a random record from a MySQL database?
- How to pop the first element from a C# List?
- How to delete last element from a List in C++ STL
- How to generate random value from a given list in Excel?
- How to select random elements from an R vector?
- Python Program to Select the nth Smallest Element from a List in Expected Linear Time
- Python Program to Select the nth Largest Element from a List in Expected Linear Time
- How to remove an element from a Java List?
- Select a fixed number of random records from a MySQL table?
- Swift Program to Get a Random Element from the Set
- Select first element of a commaseparated list in MySQL?
- How to remove an element from a list in Python?
- Write a Python code to select any one random row from a given DataFrame
- How to remove an element from Array List in C#?

Advertisements