本文整理汇总了C#中System.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# System.Sum方法的具体用法?C# System.Sum怎么用?C# System.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System
的用法示例。
在下文中一共展示了System.Sum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
IEnumerable <int> intArr =new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
IEnumerable<double> dblArr =new[] { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };
Console.WriteLine("Sum of: ");
Console.WriteLine("Integer array: {0}", intArr.Sum());
Console.WriteLine("Double array: {0}", dblArr.Sum());
Console.WriteLine();
Console.WriteLine("Product of: ");
Console.WriteLine("Integer array: {0}", intArr.Product());
Console.WriteLine("Double array: {0}", dblArr.Product());
Console.WriteLine();
Console.WriteLine("Average of: ");
Console.WriteLine("Integer array: {0}", intArr.Average());
Console.WriteLine("Double array: {0}", dblArr.Average());
Console.WriteLine();
Console.WriteLine("Min of: ");
Console.WriteLine("Integer array: {0}", intArr.Min());
Console.WriteLine("Double array: {0}", dblArr.Min());
Console.WriteLine();
Console.WriteLine("Max of: ");
Console.WriteLine("Integer array: {0}", intArr.Max());
Console.WriteLine("Double array: {0}", dblArr.Max());
}
示例2: List_Binds
public void List_Binds()
{
var values = new[] { 1, 2, 3, 4, };
var list = new List<int>(values);
var squares = list.Bind<int>(a => new List<int>(Helper(a))).Values.ToList();
Assert.AreEqual(values.Sum(), squares.Count);
}
示例3: ForEach
public void ForEach()
{
int[] numbers = new[] { 2, 3, 6, 7 };
int sum = 0;
numbers.ForEach(x => sum += x);
Assert.Equal(sum, numbers.Sum());
}
示例4: ForEachFunc
public void ForEachFunc()
{
var @enum = new[] {1, 2, 3, 4}.AsEnumerable();
var sum = 0;
@enum.ForEach(n => sum += n);
Assert.AreEqual(sum, @enum.Sum());
}
示例5: TestInvokeInstanceMethod
public void TestInvokeInstanceMethod()
{
RunWith( ( object person ) =>
{
var elements = new[] { 1d, 2d, 3d, 4d, 5d };
elements.ForEach( element => person.CallMethod( "Walk", element ) );
Assert.AreEqual( elements.Sum(), person.GetFieldValue( "metersTravelled" ) );
} );
}
示例6: BanParser
public HasVictim BanParser(bool wait = false)
{
var userHistory = Datastore.UserHistory(_message.SenderName) ?? new UserHistory { Nick = _message.SenderName }; // todo maKe this lazy
var fullWidthCharacters = new[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'q', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'Q', 'X', 'Y', 'Z' };
if (fullWidthCharacters.Sum(c => _originalText.Count(ot => ot == c)) > 5)
return MuteAndIncrementHardCoded(userHistory, MagicStrings.FullWidth, "fullwidth text", wait);
var spamCharacters = new[] { "▓", "▂", "♥", "ᴶ", "♠", "ᑫ", "ᴷ", "♦", "ᴬ", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "⁰", ":", "░", "═", "╔", "╗", "╚", "╝", "║", "─", "┐", "┌", "└", "╥", "┘", "╡", "▀", "▐", "▌", "█", "▄", "■", "▉", " ", "▒", " ", "̍", "̎", "̄", "̅", "̿", "̑", "̆", "̐", "͒", "͗", "͑", "̇", "̈", "̊", "͂", "̓", "̈́", "͊", "͋", "͌", "̃", "̂", "̌", "͐", "̀", "́", "̋", "̏", "̒", "̓", "̔", "̽", "̉", "ͣ", "ͤ", "ͥ", "ͦ", "ͧ", "ͨ", "ͩ", "ͪ", "ͫ", "ͬ", "ͭ", "ͮ", "ͯ", "̾", "͛", "͆", "̚", "̕", "̛", "̀", "́", "͘", "̡", "̢", "̧", "̨", "̴", "̵", "̶", "͏", "͜", "͝", "͞", "͟", "͠", "͢", "̸", "̷", "͡", "҉", "̖", "̗", "̘", "̙", "̜", "̝", "̞", "̟", "̠", "̤", "̥", "̦", "̩", "̪", "̫", "̬", "̭", "̮", "̯", "̰", "̱", "̲", "̳", "̹", "̺", "̻", "̼", "ͅ", "͇", "͈", "͉", "͍", "͎", "͓", "͔", "͕", "͖", "͙", "͚", "̣", "👎", "💉", "🔪", "🔫", "🚬", "👌", "ү", "f", "👀", "👎", "❌", "🚫", "ʳ", "ᶦ", "ᵍ", "ʰ", "ᵗ", "ᵉ", "✔", "ᵒ", "О", "O", "Н", "Ꮇ", "ƽ", "ԁ", "💯", " ", "▁", "▃", "▅", "▆", "✋", "🤔", "😏", "⎠", "⎝" };
if (spamCharacters.Sum(c => _originalText.Count(ot => ot.ToString() == c)) > 20)
return MuteAndIncrementHardCoded(userHistory, MagicStrings.SpamCharacters, "spam characters", wait);
var unicode = new[] { '็', 'е', };
var controlCharacters = new Regex(@"[\u0000-\u001F\u0080-\u009F\u007F-[\x0D\x0A\x09]]"); //http://stackoverflow.com/questions/3770117/what-is-the-range-of-unicode-printable-characters
if (unicode.Sum(c => _originalText.Count(ot => ot == c)) >= 1 || controlCharacters.Match(_originalText).Success)
return MuteAndIncrementHardCoded(userHistory, MagicStrings.Unicode, "unicode idiocy", wait);
if (Datastore.EmoteRegex.Matches(_message.OriginalText).Count > 7)
return MuteAndIncrementHardCoded(userHistory, MagicStrings.Facespam, "face spam", wait);
var mutedWord = Datastore.MutedWords.Select(x => x.Key).FirstOrDefault(x => _originalText.Contains(x) || _text.Contains(x));
var mute = DeterminesHasVictim(mutedWord, userHistory, MagicStrings.MutedWords, Datastore.MutedWords, new Mute(), wait);
if (mute != null) return mute;
var bannedWord = Datastore.BannedWords.Select(x => x.Key).FirstOrDefault(x => _originalText.Contains(x) || _text.Contains(x));
var ban = DeterminesHasVictim(bannedWord, userHistory, MagicStrings.BannedWords, Datastore.BannedWords, new Ban(), wait);
if (ban != null) return ban;
var mutedRegex = Datastore.MutedRegex.Select(x => new Regex(x.Key)).FirstOrDefault(x => x.Match(_originalText).Success);
if (mutedRegex != null) {
var banR = DeterminesHasVictim(mutedRegex.ToString(), userHistory, MagicStrings.MutedRegex, Datastore.MutedRegex, new Mute(), wait);
if (banR != null) return banR;
}
var bannedRegex = Datastore.BannedRegex.Select(x => new Regex(x.Key)).FirstOrDefault(x => x.Match(_originalText).Success);
if (bannedRegex != null) {
var banR = DeterminesHasVictim(bannedRegex.ToString(), userHistory, MagicStrings.BannedRegex, Datastore.BannedRegex, new Ban(), wait);
if (banR != null) return banR;
}
var longSpam = LongSpam();
if (longSpam != null) return longSpam;
var selfSpam = SelfSpam();
if (selfSpam != null) return selfSpam;
var numberSpam = NumberSpam();
if (numberSpam != null) return numberSpam;
var repeatCharacterSpam = RepeatCharacterSpam();
if (repeatCharacterSpam != null) return repeatCharacterSpam;
//var lineSpam = LineSpam();
//if (lineSpam != null) return lineSpam;
foreach (var nuke in _messageProcessor.Nukes.Where(x => x.Predicate(_message.SanitizedText) || x.Predicate(_message.OriginalText))) {
nuke.VictimList.Add(_message.SenderName);
return new Mute(_message.SenderName, nuke.Duration, null);
}
return null;
}
示例7: TestInvokeInstanceMethodViaMethodInfo
public void TestInvokeInstanceMethodViaMethodInfo()
{
RunWith( ( object person ) =>
{
var elements = new[] { 1d, 2d, 3d, 4d, 5d };
var methodInfo = person.UnwrapIfWrapped().GetType().Method( "Walk", new [] { typeof(int) }, Flags.InstanceAnyVisibility );
elements.ForEach( element => methodInfo.Call( person, element ) );
Assert.AreEqual( elements.Sum(), person.GetFieldValue( "metersTravelled" ) );
} );
}
示例8: Main
static void Main()
{
var elements = new[] { "a", "b" };
Console.WriteLine(elements.Average());
Console.WriteLine(elements.Min());
Console.WriteLine(elements.Max());
Console.WriteLine(elements.Sum());
string a = "a";
string b = "b";
Console.WriteLine(a * b);
}
示例9: Main
static void Main()
{
var numbers = new[] { 3, 5, 12, 15, 30, 35 };
Console.WriteLine("Sum: {0}", numbers.Sum());
Console.WriteLine("Product: {0}", numbers.Product());
Console.WriteLine("Min: {0}", numbers.Min());
Console.WriteLine("Max: {0}", numbers.Max());
Console.WriteLine("Average: {0}", numbers.Average());
Console.WriteLine();
}
示例10: Mean
public void Mean()
{
var rnd = new Random();
var nums = new[] { rnd.NextDouble(), rnd.NextDouble(), rnd.NextDouble(), rnd.NextDouble(), rnd.NextDouble() };
var res = API.Mean(nums);
if (!string.IsNullOrWhiteSpace(res.Error))
{
Assert.Fail("The API call failed with the following internal error: " + res.Error);
}
Assert.AreEqual(nums.Sum() / nums.Count(), res.Result, 0.0000000001);
}
示例11: Main
public static void Main()
{
// products
//int n = int.Parse(Console.ReadLine());
var weight = new[] { 3, 8, 4, 1, 2, 8 };
var cost = new[] { 2, 12, 5, 4, 3, 13 };
var capacity = 10;
Console.WriteLine("Max sum of weight: {0}", weight.Sum());
Console.WriteLine("Max sum of value: {0}", cost.Sum());
Console.WriteLine("Numbers: {0}", string.Join(", ", weight));
Console.WriteLine("Costs: {0}", string.Join(", ", cost));
Console.WriteLine("Capacity: {0}", capacity);
Solve(weight, cost, capacity);
}
示例12: GetRandomInt
public static int GetRandomInt()
{
var probabilities = new[] { 35, 35, 35, 35, 10 };
var probabilitiesSum = probabilities.Sum();
int tempRand = random.Next(0, probabilitiesSum);
var currentSum = 0;
for (int i = 0; i < probabilities.Length; i++)
{
currentSum += probabilities[i];
if (tempRand < currentSum)
{
return (i + 1);
}
}
throw new InvalidOperationException();
}
示例13: Main
static void Main()
{
var items = new[]
{
1.2,
2.3,
3.4
};
Console.Write("Elements: ");
foreach (var item in items)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
Console.WriteLine("Sum: " + items.Sum());
Console.WriteLine("Product: " + items.Product());
Console.WriteLine("Min: " + items.Min());
Console.WriteLine("Max: " + items.Max());
Console.WriteLine("Average: " + items.Average());
}
示例14: DecodeMpo
/// <summary>
/// Decodes an MPO file into jpeg images
/// </summary>
/// <remarks>
/// An MPO file is a multi-jpeg container that can hold any number of jpeg files. The container itself contains no
/// extra data so jpeg images can be found and extracted using their start signature "0xFFD8FFE1" which represents
/// the first EXIF tag
/// </remarks>
/// <param name="mpoFileName">The file name of the input mpo file</param>
public static void DecodeMpo(string mpoFileName)
{
// The MPO file will contain N jpgs packed together. There is no extra information stored with the MPO.
// JPEG images will be separated by the tag bytes FF D8 FF E1 (the start tags of the jpeg)
var jpegHeaderStart = new[] { 0xFF, 0xD8, 0xFF, 0xE1 };
// Start by reading the bytes
var mpoData = File.ReadAllBytes(mpoFileName);
// Find the location of the beginning of each image
var indices = new List<int>();
for (var i = 0; i < mpoData.Length - jpegHeaderStart.Length; i++)
{
// Use a mask to find the tag. ~A & A = 0 (this is a little screwy because we deal with FF here which matches everything)
var j = 0;
var maskResult = jpegHeaderStart.Sum(bt => ~mpoData[i + j++] & bt);
// If the masking result is 0 then the right image start tag is found
if (maskResult == 0)
{
indices.Add(i);
}
}
// For each found index, extract the jpeg
for (var i = 0; i < indices.Count; i++)
{
// Create a temporary buffer to hold the image bytes
// To do this, make a buffer with length equal to the difference between the start index of this image
// and the start index of the next image. If there is no next image, use the end of the data instead.
byte[] image = i + 1 == indices.Count ? new byte[mpoData.Length - indices[i]] : new byte[indices[i + 1] - indices[i]];
// Copy the image data to a temporary file
Array.Copy(mpoData, indices[i], image, 0, image.Length);
// Write the image data to a file
File.WriteAllBytes(i + ".jpg", image);
}
}
示例15: Main
/*2. Implement a set of extension methods for IEnumerable<T> that implement the following group functions:
* sum, product, min, max, average.*/
static void Main()
{
List<int> listInt = new List<int> { 1, 2, 3, 4, 5 };
double[] testDouble = new[] { 4.5, 6.0, 5.7, 34.7 }; //sum = 50.9
Console.WriteLine("---list <int>---");
Console.WriteLine("Sum: {0}", listInt.Sum());
Console.WriteLine("Average: {0}", listInt.Average());
Console.WriteLine("Product: {0}", listInt.Product());
Console.WriteLine("Minimum: {0}", listInt.Min());
Console.WriteLine("Minimum: {0}", listInt.Max());
Console.WriteLine("---double---");
Console.WriteLine("Sum: {0}", testDouble.Sum());
Console.WriteLine("Average: {0}", testDouble.Average());
Console.WriteLine("Product: {0}", testDouble.Product());
Console.WriteLine("Minimum: {0}", testDouble.Min());
Console.WriteLine("Minimum: {0}", testDouble.Max());
List<int> testEmpty = new List<int>();
Console.WriteLine(testEmpty.Sum()); //return error when enumaration is empty, does not work about array(return 0)
}