找到 34423 篇文章 适用于编程
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
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"); ... 阅读更多
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
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); } // ... 阅读更多
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) ... 阅读更多
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
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
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); } }运行以上示例后,您将获得以下输出。除此之外,您还将获得一个 ... 阅读更多
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); } }输出9/5/2018 5:20:03 AM
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
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP