먼저 C#에서 목록을 설정합니다.
var list = new List<string>{ "one","two","three","four"}; 이제 요소 수를 가져와 무작위로 표시합니다.
int index = random.Next(list.Count); Console.WriteLine(list[index]);
C#의 목록에서 임의의 요소를 선택하려면 다음 코드를 실행하십시오 -
예
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]);
}
}
} 출력
three