Dot Net Perls
Top 37 C# Example Pages

[".rywsrydryh..B`CEST~~}T~~YF849979F.CCECXBCECP696FXBB*BXS}T~~}T~~","Take"," returns the first specified number of elements. It acts upon an IEnumerable sequence. It returns another IEnumerable sequence containing the specified elements. We see basic usages of this extension method. ","Extension ","extension","Example."," Here we use the Take extension method. Take is a method in the System.Linq namespace that allows you to get the first several elements from a sequence. It receives an element count. ","Here: ","We look at a couple simple examples of how Take works. Then we look at using Reverse with Take.","Reverse ","reverse","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","Based on:"," .NET 4.7 (2017)\n\n","C# program that uses Take","\n\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\n\nclass Program\n{\n static void Main()\n {\n List<string> list = new List<string>();\n list.Add(","\"cat\"",");\n list.Add(","\"dog\"",");\n list.Add(","\"programmer\"",");","\n\n // Get first 2 elements\n ","var first = list.","Take","(2);\n foreach (string s in first)\n {\n Console.WriteLine(s);\n }\n Console.WriteLine();","\n\n // Get last 2 elements reversed\n ","var last = list.","Reverse","<string>().","Take","(2);\n foreach (string s in last)\n {\n Console.WriteLine(s);\n }\n Console.WriteLine();","\n\n // Get first 1000 elements\n ","var all = list.","Take","(1000);\n foreach (string s in all)\n {\n Console.WriteLine(s);\n }\n Console.WriteLine();\n }\n}\n\n","Output","\n\ncat\ndog\n\nprogrammer\ndog\n\ncat\ndog\nprogrammer","In this example,"," the first Take call returns the first two elements in the List. This could display the two oldest, first elements. The second Take call above is chained after a Reverse<string> call. It operates on the result of Reverse. ","Note: ","This part will print out the last two elements in Reverse order. This could be used to display the newest elements.","And: ","The final Take call above uses the top 1000 elements, which is nonsense as there are not 1000 elements in the List.","List ","list","However: ","The final Take call shows that Take will manage bad parameters without a problem.","TakeWhile."," TakeWhile returns elements while a Predicate matches. The Predicate returns true or false. When it returns false, TakeWhile returns. TakeWhile operates from the beginning of an IEnumerable collection. ","Predicate ","predicate","Next: ","In this program, an integer array of five elements is declared. The first three numbers in it are odd, while the last two are even.","Odd, Even ","odd","And: ","The TakeWhile extension method is invoked. It returns the first three odd numbers, but not the even numbers at the end.","C# program that uses TakeWhile method","\n\nusing System;\nusing System.Linq;\n\nclass Program\n{\n static void Main()\n {","\n // Contains five numbers.\n ","int[] values = { 1, 3, 5, 8, 10 };","\n\n // Take all non-even (odd) numbers.\n ","var result = values.","TakeWhile","(item => item % 2 != 0);","\n\n // Display result.\n ","foreach (int value in result)\n {\n Console.WriteLine(value);\n }\n }\n}\n\n","Output","\n\n1\n3\n5","Reading the lambda expression."," How can you read the lambda expression? The name 'item' is just a temporary variable name. Each element becomes an item, and then the function returns true only if the value is not evenly divisible by two. ","Lambdas ","lambda","Modulo ","modulo","Discussion."," Skip is useful when paired with Take. It allows you to avoid the first several elements in the sequence. When you combine Skip with Take, you can indicate elements that you want to avoid based on a numeric pattern. ","Skip, SkipWhile ","skip","Summary."," Take() gets elements from a collection. Also, we saw Reverse with Take, and how to use simple extension methods. Take method performance is good, but it is not as fast as custom extension methods may be. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"]

%iVBORw0KG;)NSUhEUg?ALw?ABGC)ABTK6EI?ACe0lEQVR4XtXa4XHCMAyG4W7iQTSPxvEu2kWTaIL0aO69a48kwjnsIv0PPMjoIzj+8kdtY8U1Cyrcump7lGo3j9+Or/aoQUB2TaPs5gsgN20U1W0VXu7hobfDUluDb3Yf79LOSmMJXm/jrVGq9lNdBb34Cnzzm3hHbvEL4AyBxFz8/v79Hj5kp/sTwIQlnYrf9AGIW/i+J8vRxa5M01S88SbD+NjtV6uik/Gb@ZDeBovcTkPNhVP68fxge6kdG/9RDwIHccbjb9sfczEs/w+jO/k1GXrbTLeUYzhhc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