服务热线400-700-0056

主菜单

UPlooking home slide

错误信息

Notice: Undefined index: mobile 在 isMobile() (行 66/alidata/www/syuplooking/sites/all/themes/shenyang/template.php).

当前位置

震惊小伙伴的单行代码●C#篇

1、让列表中的每个元素都乘以2


Print("Multiple each item in a list by 2", Enumerable.Range(1, 10).Select(i => i * 2));


2、求列表中的所有元素之和


Print("Sum a list of numbers", Enumerable.Range(1, 1000).Sum());


3、判断一个字符串中是否存在某些词


var wordlist = new[] { "C#", "and stuff" };

var tweet = "This is an example tweet talking about C# and stuff";

Print("Verify if a word exists in string", wordlist.Any(word => tweet.IndexOf(word) > -1));

Print("Show matched words in string", wordlist.Where(word => tweet.IndexOf(word) > -1));


4、读取文件


Print("Read in a File", File.ReadAllBytes("oneliners.exe").Length);


5、祝你生日快乐!


Print("Happy Birthday", Enumerable.Range(1, 4).Select((i) => string.Format("Happy Birthday {0} ", i == 3 ? "dear NAME" : "to You")));


6. 过滤列表中的数值


var passed = new List<int>();

var failed = new List<int>();


(from bucket in new[] { passed, failed } from i in new[] { 49, 58, 76, 82, 88, 90 } select new { bucket, i }).ToList().ForEach((tuple) => tuple.bucket.AddRange(Enumerable.Repeat(tuple, 1).Where((tup) => (tup.bucket == passed && tup.i > 60) || (tup.bucket == failed && tup.i <= 60)).Select((tup) => tup.i)));

Print("Filter list of numbers >60", (IEnumerable<int>)passed);

Print("Filter list of numbers <=60", (IEnumerable<int>)failed);


7. 获取XML web service数据并分析


Print("Fetch and Parse an XML web service", XDocument.Load("http://search.twitter.com/search.atom?&q=scala"));


8. 找到列表中最小或最大的一个数字


Print("Find minimum in a list", Enumerable.Min(new[] { 14, 35, -7, 46, 98 }));

Print("Find maximum in a list", Enumerable.Max(new[] { 14, 35, -7, 46, 98 }));


9. 并行处理


Print("Parallel Processing", Enumerable.Range(1, 10).AsParallel().Select((i)=>i*2).AsEnumerable());


10. “Sieve of Eratosthenes”算法


Print("Sieve of Eratosthenes", Enumerable.Range(1, 9999).Select(num =>