सबसे पहले, 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