在我的一个应用程序中,我需要一个大的常量(实际上是
该数组包含一千多个项目,当该类型首次使用时,我的程序会遇到严重的减速。我想知道是否有一种快速初始化大型数组的方法在C#中。
static readonly
)对象数组。该数组在类型的静态构造函数中进行初始化。该数组包含一千多个项目,当该类型首次使用时,我的程序会遇到严重的减速。我想知道是否有一种快速初始化大型数组的方法在C#中。
public static class XSampa {
public class XSampaPair : IComparable<XSampaPair> {
public XSampaPair GetReverse() {
return new XSampaPair(Key, Target);
}
public string Key { get; private set; }
public string Target { get; private set; }
internal XSampaPair(string key, string target) {
Key = key;
Target = target;
}
public int CompareTo(XSampaPair other) {
if (other == null)
throw new ArgumentNullException("other",
"Cannot compare with Null.");
if (Key == null)
throw new NullReferenceException("Key is null!");
if (other.Key == null)
throw new NullReferenceException("Key is null!");
if (Key.Length == other.Key.Length)
return string.Compare(Key, other.Key,
StringComparison.InvariantCulture);
return other.Key.Length - other.Key;
}
}
private static readonly XSampaPair[] pairs, reversedPairs;
public static string ParseXSampaToIpa(this string xsampa) {
// Parsing code here...
}
public static string ParseIpaToXSampa(this string ipa) {
// reverse code here...
}
static XSampa() {
pairs = new [] {
new XSampaPair("a", "\u0061"),
new XSampaPair("b", "\u0062"),
new XSampaPair("b_<", "\u0253"),
new XSampaPair("c", "\u0063"),
// And many more pairs initialized here...
};
var temp = pairs.Select(x => x.GetReversed());
reversedPairs = temp.ToArray();
Array.Sort(pairs);
Array.Sort(reversedPairs);
}
}
提示:我使用数组将X-SAMPA音标转换为相应的IPA字符的Unicode字符串。
IEnumerable<yourobj>
,以便在需要时惰性地 yield return 数组? - jb.