找到关于编程的34423 篇文章

C#程序返回两个序列之间的差异

Ankith Reddy
更新于 2020年6月23日 08:34:54

188 次浏览

设置两个序列。double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 }; double[] arr2 = { 15.6, 30.5, 50.2 }; 要获取这两个数组之间的差异,请使用 Except() 方法。IEnumerable res = arr1.AsQueryable().Except(arr2); 以下是完整的代码。示例 在线演示using System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 };       double[] arr2 = { 15.6, 30.5, 50.2 };       Console.WriteLine("初始列表...");       foreach(double ele in arr1) {           Console.WriteLine(ele);       }       IEnumerable res = arr1.AsQueryable().Except(arr2);       Console.WriteLine("新列表...");       foreach (double a in res) {           Console.WriteLine(a);       }    } }输出初始列表... 10.2 15.6 23.3 30.5 50.2 新列表... 10.2 23.3

C# 枚举 TryParse() 方法

karthikeya Boyini
更新于 2020年6月23日 08:36:02

9K+ 次浏览

TryParse() 方法将一个或多个枚举常量的字符串表示形式转换为等效的枚举对象。首先,设置一个枚举。enum Vehicle { Bus = 2, Truck = 4, Car = 10 }; 现在,让我们声明一个字符串数组并设置一些值。string[] VehicleList = { "2", "3", "4", "bus", "Truck", "CAR" }; 现在使用 Enum TryParse() 方法相应地解析值。示例 在线演示using System; public class Demo {    enum Vehicle { Bus = 2, Truck = 4, Car = 10 };    public static void Main() {       string[] VehicleList = { "2", "3", "4", "bus", "Truck", "CAR" ... 阅读更多

C# 可查询 SkipWhile() 方法

Arjun Thakur
更新于 2020年6月23日 08:36:30

116 次浏览

使用 SkipWhile() 方法跳过数组中的元素并返回剩余元素。以下是我们的数组 −int[] marks = { 45, 88, 55, 90, 95, 85 }; 现在,让我们跳过大于或等于 60 的元素。我们使用 Lambda 表达式设置的条件。IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipWhile(s => s >= 60); 示例 在线演示using System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 45, 88, 55, 90, 95, 85 };       // 跳过大于 60 的元素       IEnumerable selMarks = ... 阅读更多

C# Linq SkipLast 方法

Samual Sam
更新于 2020年6月23日 08:36:50

2K+ 次浏览

使用 SkipLast() 方法跳过末尾的元素并返回剩余元素。以下是一个数组。int[] marks = { 45, 88, 50, 90, 95, 85 }; 现在,让我们使用 SkipLast() 和 Lambda 表达式跳过末尾的两个元素,但这在按降序排列元素后完成。IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipLast(2); 示例 在线演示using System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 45, 88, 50, 90, 95, 85 };       IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipLast(2);       Console.WriteLine("跳过... 阅读更多

C# 数字 (“N”) 格式说明符

Chandu yadav
更新于 2020年6月23日 08:24:48

2K+ 次浏览

数字 (“N”) 格式说明符将数字转换为以下形式的字符串:"-d,ddd,ddd.ddd…" 上面,“-”是根据需要使用的负数符号,“d”是数字 (0-9),“,”表示组分隔符,“.”是小数点符号 示例 在线演示using System; using System.Globalization; class Demo {    static void Main() {       double val1 = -5566.789;       Console.WriteLine(val1.ToString("N", CultureInfo.InvariantCulture));       int val2 = 87987766;       Console.WriteLine(val2.ToString("N3", CultureInfo.InvariantCulture));    } }输出-5,566.79 87,987,766.000

C# Linq Skip() 方法

George John
更新于 2020年6月23日 08:25:44

2K+ 次浏览

跳过元素并使用 Skip() 方法返回剩余元素。以下是一个数组。int[] marks = { 80, 55, 79, 99 }; 现在,让我们使用 Lambda 表达式跳过 2 个元素,但这在按降序排列元素后完成。IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).Skip(2); 示例using System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 80, 55, 79, 99 };       IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).Skip(2);       Console.WriteLine("跳过了前两名学生的结果...");       foreach (int res in selMarks) {           console.WriteLine(res);       }    } }

使用 C# 正则表达式删除空格

karthikeya Boyini
更新于 2020年6月23日 08:25:19

1K+ 次浏览

假设我们要从以下字符串 str1 中删除空格 string str1 = "Brad Pitt"; 现在,使用 Regex Replace 将空格替换为空。在这里,我们使用了 System.Text.RegularExpressions。string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", ""); 让我们看看完整的示例。示例 在线演示using System; using System.Text.RegularExpressions; namespace Demo {    class Program {       static void Main(string[] args) {           string str1 = "Brad Pitt";           Console.WriteLine(str1);           string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", "");           Console.WriteLine(str2);       }    } }输出Brad Pitt BradPitt

C# SingleorDefault() 方法

Samual Sam
更新于 2020年6月23日 08:26:22

3K+ 次浏览

该方法返回序列的单个特定元素。如果序列中不存在该元素,则返回默认值。我们这里有两个字符串数组。string[] str1 = { "one" }; string[] str2 = { }; 第一个数组检查单个元素,而第二个数组为空并使用 SingleorDefault 检查。str2.AsQueryable().SingleOrDefault(); 以下是一个显示 SingleorDefault() 方法用法的示例。示例 在线演示using System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] str1 = { "one" };       string[] str2 = { }; ... 阅读更多

C# 程序返回满足条件的唯一元素

Ankith Reddy
更新于 2020年6月23日 08:26:45

302 次浏览

Single() 方法返回满足条件的唯一元素。如果可见多个这样的元素,则会抛出错误。以下是我们的字符串数组。string[] str = { "jack", "tom", "henry", "time"}; 现在,使用 Single() 方法获取每个元素。然后,我们使用 Lambda 表达式来计算长度大于四的元素。str.AsQueryable().Single(name => name.Length > 4); 示例 在线演示using System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] str = { "jack", "tom", "henry", "time"};       // 查找长度为... 阅读更多

C# Single() 方法

karthikeya Boyini
更新于 2020年6月23日 08:27:09

2K+ 次浏览

使用 Single() 方法仅获取序列的单个元素。假设我们有一个只有一个元素的字符串数组。string[] str = { "one" }; 现在,获取元素。str.AsQueryable().Single(); 这是我们的代码。示例 在线演示using System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] str = { "one" };       string res = str.AsQueryable().Single();       Console.WriteLine(res);    } }输出one

广告
© . All rights reserved.