如何让一个函数return既是数组的索引又是数组的值?
How to have a function return both index and value of an array?
此函数计算字母在给定字符串中出现的频率并将其放入数组中(索引是字母的 ascii 数,值是计算的出现次数)。现在我需要 return 字母(它已经这样做了)和值。只是通过在线阅读,我无法弄清楚如何使用 ref 和替代方法来做到这一点。
static char MostCommonLetter(string s)
{
int[] occurrances = new int[255];
for (int i = 0; i < s.Length; i++)
{
if (char.IsLetter(s[i]))
{
int ascii = (int)s[i];
occurrances[ascii]++;
}
}
char maxValue = (char)Array.IndexOf(occurrances, occurrances.Max());
return maxValue;
}
在 C# 7 及更高版本中,Value Tuples 是您的最佳选择。您可以按如下方式定义函数:
static (char letter, int occurrences) MostCommonLetter(string s)
{
int[] occurrences = new int[255];
for (int i = 0; i < s.Length; i++)
{
if (char.IsLetter(s[i]))
{
int ascii = (int)s[i];
occurrances[ascii]++;
}
}
char letter = (char)Array.IndexOf(occurrences, occurrences.Max());
return (index: letter, occurrences: occurrences);
}
然后您可以像这样引用输出:
var (index, occurrences) = MostCommonLetter(yourString);
您可以使用 "out" 参数来 return 函数中的其他参数。
static char MostCommonLetter(string s, out int maxOccurrance)
{
int[] occurrances = new int[255];
for (int i = 0; i < s.Length; i++)
{
if (char.IsLetter(s[i]))
{
int ascii = (int)s[i];
occurrances[ascii]++;
}
}
maxOccurrance = occurrances.Max();
char maxValue = (char)Array.IndexOf(occurrances, maxOccurrance);
return maxValue;
}
//...
// In C# 7 and above you can call it like that
var c = MostCommonLetter("abccd", out int maxOccurrance);
//// In older version of C# you should just declare out variable before use it
//int maxOccurrance;
//var c = MostCommonLetter("abccd", out maxOccurrance);
另一种解决方案是使用 LINQ:
string str = "Hello World!";
var result = str.GroupBy(c => c)
.Select(group => new { Letter = group.Key, Count = group.Count() })
.OrderByDescending(x => x.Count)
.First();
char letter = result.Letter;
int count = result.Count;
字母='l'
计数 = 3
在 C# 中执行所需操作的最佳且灵活的方法是使用 struct.
以这种方式定义一个结构,并用它来同时return多个结果(一个结构甚至可以包含函数……你可以看到这些结构更轻类):
namespace YourApp.AnyNamespace {
// Other things
public struct SampleName
{
public char mostCommon;
public int occurancies;
}
}
此函数计算字母在给定字符串中出现的频率并将其放入数组中(索引是字母的 ascii 数,值是计算的出现次数)。现在我需要 return 字母(它已经这样做了)和值。只是通过在线阅读,我无法弄清楚如何使用 ref 和替代方法来做到这一点。
static char MostCommonLetter(string s)
{
int[] occurrances = new int[255];
for (int i = 0; i < s.Length; i++)
{
if (char.IsLetter(s[i]))
{
int ascii = (int)s[i];
occurrances[ascii]++;
}
}
char maxValue = (char)Array.IndexOf(occurrances, occurrances.Max());
return maxValue;
}
在 C# 7 及更高版本中,Value Tuples 是您的最佳选择。您可以按如下方式定义函数:
static (char letter, int occurrences) MostCommonLetter(string s)
{
int[] occurrences = new int[255];
for (int i = 0; i < s.Length; i++)
{
if (char.IsLetter(s[i]))
{
int ascii = (int)s[i];
occurrances[ascii]++;
}
}
char letter = (char)Array.IndexOf(occurrences, occurrences.Max());
return (index: letter, occurrences: occurrences);
}
然后您可以像这样引用输出:
var (index, occurrences) = MostCommonLetter(yourString);
您可以使用 "out" 参数来 return 函数中的其他参数。
static char MostCommonLetter(string s, out int maxOccurrance)
{
int[] occurrances = new int[255];
for (int i = 0; i < s.Length; i++)
{
if (char.IsLetter(s[i]))
{
int ascii = (int)s[i];
occurrances[ascii]++;
}
}
maxOccurrance = occurrances.Max();
char maxValue = (char)Array.IndexOf(occurrances, maxOccurrance);
return maxValue;
}
//...
// In C# 7 and above you can call it like that
var c = MostCommonLetter("abccd", out int maxOccurrance);
//// In older version of C# you should just declare out variable before use it
//int maxOccurrance;
//var c = MostCommonLetter("abccd", out maxOccurrance);
另一种解决方案是使用 LINQ:
string str = "Hello World!";
var result = str.GroupBy(c => c)
.Select(group => new { Letter = group.Key, Count = group.Count() })
.OrderByDescending(x => x.Count)
.First();
char letter = result.Letter;
int count = result.Count;
字母='l'
计数 = 3
在 C# 中执行所需操作的最佳且灵活的方法是使用 struct.
以这种方式定义一个结构,并用它来同时return多个结果(一个结构甚至可以包含函数……你可以看到这些结构更轻类):
namespace YourApp.AnyNamespace {
// Other things
public struct SampleName
{
public char mostCommon;
public int occurancies;
}
}