找到 34423 篇文章 关于编程

C# 中的 Any() 扩展方法

George John
更新于 2020年6月22日 14:47:54

397 次浏览

Any() 扩展方法是 System.Linq 命名空间的一部分。使用此方法,您可以检查任何元素是否匹配特定条件。首先,设置一个包含元素的数组:int[] arr = { 6, 7, 15, 40, 55 };以下是一个示例。它检查数组中是否有任何元素大于或等于 20:arr.Any(element => element >= 20);以下是完整的代码:示例  在线演示using System; using System.Linq; class Program {    static void Main() {       int[] arr = { 6, 7, 15, 40, 55 };       bool res = arr.Any(element => element >= 20);       Console.WriteLine(res);    } }输出True

C# 程序:从列表中获取前三个元素

Samual Sam
更新于 2020年6月22日 14:39:30

9K+ 次浏览

使用 Take() 方法获取 C# 中前几个元素。首先,设置一个列表并添加元素:List myList = new List(); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); myList.Add("Four"); myList.Add("Five"); myList.Add("Six");现在,使用 Take() 方法从列表中获取元素。将您想要的元素数量作为参数添加:myList.Take(3)以下是代码:示例  在线演示using System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       List myList = new List();       myList.Add("One");       myList.Add("Two");       myList.Add("Three");       myList.Add("Four");       ... 阅读更多

C# 程序:使用 Lambda 表达式查找数组中的最大元素

karthikeya Boyini
更新于 2020年6月22日 14:39:54

512 次浏览

声明一个数组:int[] arr = { 10, 90, 20, 19, 99, 57 };现在要从数组中获取最大元素,请使用带有 lambda 表达式的 Max() 方法:arr.Max();以下是完整的代码:示例  在线演示using System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 10, 90, 20, 19, 99, 57 };       Console.WriteLine(arr.Max(element => Math.Abs(element)));    } }输出99

C# 中的 SkipWhile 方法

Samual Sam
更新于 2020年6月22日 14:40:31

301 次浏览

SkipWhile 在条件匹配时跳过元素。例如,如果您想跳过所有偶数元素,请使用以下方法:ele => ele %2 == 0以下是一个示例,其中跳过所有偶数元素,只显示奇数元素:示例  在线演示using System.IO; using System; using System.Linq; public class Demo {    public static void Main() {       int[] arr = { 20, 35, 55 };       Console.WriteLine("初始数组...");       foreach (int value in arr) {           Console.WriteLine(value);       }       // ... 阅读更多

C# 程序:跳过数组中的初始元素

karthikeya Boyini
更新于 2020年6月22日 14:41:15

2K+ 次浏览

如果您想跳过数组中的多个元素,请在 C# 中使用 Skip() 方法。假设以下数组是我们的数组:int[] arr = { 24, 40, 55, 62, 70, 82, 89, 93, 98 };现在跳过前四个元素:var ele = arr.Skip(4);让我们看看完整的示例:示例  在线演示using System.IO; using System; using System.Linq; public class Demo {    public static void Main() {       int[] arr = { 24, 40, 55, 62, 70, 82, 89, 93, 98 };       Console.WriteLine("初始数组...");       foreach (var res in arr) ... 阅读更多

C# 中的 ElementAt() 方法

Samual Sam
更新于 2020年6月22日 14:41:37

4K+ 次浏览

ElementAt() 是 C# 中的一个 System.Linq 方法,用于获取和显示特定索引处的元素。以下是我们的字符串数组:string[] arr = { "One", "Two", "Three", "Four", "Five" };现在要获取索引 0 处的元素,请使用 ElementAt() 方法:arr.ElementAt(0);以下是完整的代码:示例  在线演示using System.IO; using System; using System.Linq; public class Demo {    public static void Main() {       string[] arr = { "One", "Two", "Three", "Four", "Five" };       // 显示索引 0 处的元素       string res = arr.ElementAt(0);       Console.WriteLine(res);    } }输出One

C# 中的字面数字后缀

karthikeya Boyini
更新于 2020年6月22日 14:42:15

321 次浏览

要在 C# 中指定数字类型,请使用后缀。字面数字后缀是数字后缀。例如,对于长整型:// 长整型后缀 long val1 = 29345L;对于双精度型:// 双精度型后缀 double val2 = 297.325D; Console.WriteLine(val2);让我们看看更多示例:示例  在线演示using System.IO; using System; public class Program {    public static void Main() {       // 长整型后缀       long val1 = 29345L;       Console.WriteLine(val1);       // 十进制后缀       decimal val5 = 3245.5678M;       Console.WriteLine(val5);       // 双精度型后缀       double val2 = 297.325D;       Console.WriteLine(val2);       // 浮点型后缀       float val3 = 250.35F;       Console.WriteLine(val3);       // 无符号后缀       uint val4 = 3456U;       Console.WriteLine(val4);    } }输出29345 3245.5678 297.325 250.35 3456

C# 中的小写后缀

Samual Sam
更新于 2020年6月22日 14:42:40

113 次浏览

为字面量设置小写后缀,例如 u、l、ul、f 等。// l 代表长整型 long a = 29876l;它也可以用于字面数字。它告诉编译器字面量是特定类型。以下是一个示例:示例  在线演示using System.IO; using System; public class Program {    public static void Main() {       long a = 29876l;       float b = 95.10f;       Console.WriteLine(a);       Console.WriteLine(b);    } }运行上述示例后,您将获得以下输出。除此之外,您还将获得一个... 阅读更多

在 C# 中获取文件的创建时间

karthikeya Boyini
更新于 2020年6月22日 14:43:02

2K+ 次浏览

要在 C# 中获取文件的创建时间,请使用 CreationTime() 方法。为此,请使用 FileInfo 和 DateTime 类。创建每个类的对象:FileInfo file = new FileInfo("new.txt"); DateTime dt = file.CreationTime;让我们看看完整的代码:示例  在线演示using System.IO; using System; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("qa.txt")) {           sw.WriteLine("Questions and Answers!");       }       FileInfo file = new FileInfo("qa.txt");       // 文件创建时间       DateTime dt = file.CreationTime;       Console.WriteLine(dt);    } }输出2018年9月5日 上午5:20:03

C# 程序:获取文件的上次访问时间

Samual Sam
更新于 2020年6月22日 14:43:40

715 次浏览

要在 C# 中获取文件的最后访问时间,可以使用 LastAccessTime() 方法。为此,需要使用 FileInfo 和 DateTime 类。创建每个类的对象 −FileInfo file = new FileInfo("new.txt"); DateTime dt = file.CreationTime; dt = file.LastAccessTime;让我们看看完整的代码 −示例 在线演示using System.IO; using System; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("quiz.txt")) {          sw.WriteLine("Quizzes!");       }       FileInfo file = new FileInfo("quiz.txt");       // 最后访问时间       DateTime dt = file.LastAccessTime;       Console.WriteLine(dt);    } }输出2018年9月5日上午5:21:43

广告
© . All rights reserved.