我需要一个无限参数的C#字符串



所以我需要创建一个无限(或至少1000(的参数字符串。我只需要能够硬编码1000个参数。

我已经尝试了var I = new List<string>("bob", "frank",等1000个参数(;我在C#中找不到方法,所以如果我需要一个NuGet包,那没关系。

一般来说,要求某人在不尝试的情况下提供完整的代码是不受欢迎的,但由于您在今天的前一个问题中提到您当时11岁,因此请抓住这个机会学习一些不同的概念。

您需要将其放在您的.cs文件的顶部:

using System;
using System.Collections.Generic;
using System.Linq;

试试这样的东西:

public string GenerateOutput()
{
// create a List of 1000 words
var words = new List<string>(1000)
{
"bob",
"frank",
"windows",
"test",
"sample",
"xylophone"
// keep populating your list of words this way
};

int numberOfLettersToGet = 10;
// keep getting #'numberOfLettersToGet' random letters until a word is found
char[] randomLetters = new char[numberOfLettersToGet];
dynamic match = null;
while (match == null)
{
// select 10 random letters
randomLetters = GetRandomLetters(numberOfLettersToGet);

// get the word that has the most number of matching letters
match = words.Select(w => new { Word = w, NumberOfMatchingLetters = w.ToCharArray().Intersect(randomLetters).Count() })
.OrderByDescending(o => o.NumberOfMatchingLetters)
.FirstOrDefault(o => o.NumberOfMatchingLetters > 0);
}

// return the results
return $"{string.Join("", randomLetters)}: found "{match.Word}", {match.NumberOfMatchingLetters} matching letters";
}
public char[] GetRandomLetters(int numberOfLetters)
{
// create an array of letters
var letters = "abcdefghijklmnopqrstuvwxyz".ToCharArray();

// select random letters
var rnd = new Random();
var randomLetters = new char[numberOfLetters];
for (var i = 0; i < numberOfLetters; i++)
{
randomLetters[i] = letters[rnd.Next(0, numberOfLetters)];
}

return randomLetters;
}

这是一把你可以摆弄的小提琴:https://dotnetfiddle.net/GNE96w

最新更新