找到 34423 篇文章 关于编程

如何复制或克隆 C# 列表?

Samual Sam
更新于 2020-06-21 15:06:41

3K+ 次浏览

要复制或克隆 C# 列表,首先设置一个列表 - List < string > list1 = new List < string > (); list1.Add("One"); list1.Add("Two"); list1.Add("Three"); list1.Add("Four"); 现在声明一个字符串数组并使用 CopyTo() 方法进行复制。string[] arr = new string[20]; list1.CopyTo(arr); 让我们看看将列表复制到一维数组的完整代码。示例 using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new List < string > ();       list1.Add("One");       list1.Add("Two");       list1.Add("Three");   ... 阅读更多

C# 中的泛型

Chandu yadav
更新于 2020-06-21 14:43:04

325 次浏览

泛型允许您编写可以处理任何数据类型的类或方法。编写类或方法的规范,使用替代参数表示数据类型。当编译器遇到类的构造函数或方法的函数调用时,它会生成代码来处理特定数据类型。泛型是一种可以丰富程序的技术,它可以 −帮助您最大限度地提高代码重用率、类型安全性和性能。您可以创建泛型集合类。.NET Framework 类库在 System.Collections.Generic 命名空间中包含多个新的泛型集合类。您可以使用这些泛型 ... 阅读更多

C# 中的流数组

karthikeya Boyini
更新于 2020-06-21 14:44:13

592 次浏览

设置字符串数组的值 - string[] names = new string[] {"Jack", "Tom"}; 现在使用 foreach 数组,将内容写入文件 - using (StreamWriter sw = new StreamWriter("names.txt")) {    foreach (string s in names) {       sw.WriteLine(s);    } } 下面是一个示例,展示了用于将文本写入文件的流数组 - 示例 using System; using System.IO; namespace FileApplication {    class Program {       static void Main(string[] args) {          string[] names = new string[] {"Jack", "Tom"};          using (StreamWriter sw = ... 阅读更多

如何在 C# 中将第二个列表追加到现有列表?

Arjun Thakur
更新于 2023-10-04 21:27:32

32K+ 次浏览

使用 AddRange() 方法将第二个列表追加到现有列表。这是列表一 - List < string > list1 = new List < string > (); list1.Add("One"); list1.Add("Two"); 这是列表二 - List < string > list2 = new List < string > (); list2.Add("Three"); ist2.Add("Four"); 现在让我们追加 - list1.AddRange(list2); 让我们看看完整的代码。示例 using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new List < string > ();       list1.Add("One");       list1.Add("Two");     ... 阅读更多

如何在 C# 中添加只读属性?

Samual Sam
更新于 2020-06-21 14:47:38

211 次浏览

标记为“只读”的字段只能在对象的构造期间设置一次。它不能更改 -让我们看一个例子。class Employee {    readonly int salary;    Employee(int salary) {       this.salary = salary;    }    void UpdateSalary() {       //salary = 50000; // 编译错误    } } 上面,我们将 salary 字段设置为只读。如果您更改它,则会发生编译时错误。这在上面的示例中显示。现在让我们看看如何检查数组是否为只读 - 示例 using ... 阅读更多

如何在 C# 列表中添加字符串值?

karthikeya Boyini
更新于 2020-06-21 14:51:06

16K+ 次浏览

要在 C# 中向列表添加字符串值,请使用 Add() 方法。首先,在 C# 中声明一个字符串列表 - List list1 = new List(); 现在添加字符串项 - myList.Add("Jack"); myList.Add("Ben"); myList.Add("Eon"); myList.Add("Tim"); 让我们看看完整的代码 - 示例 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo {    public class Program {       public static void Main(String[] args) {          List myList = new List();          myList.Add("Jack");          myList.Add("Ben");          myList.Add("Eon");          myList.Add("Tim");          Console.WriteLine(myList.Count);       }    } }

如何在 C# 中向现有的锯齿形数组添加项/元素?

Ankith Reddy
更新于 2020-06-21 14:50:24

685 次浏览

要向现有的锯齿形数组添加元素,只需使用新值设置元素的值即可。假设您需要在以下位置添加元素 - a[3][1] 只需设置值 - a[3][1] = 500; 上面,我们访问了锯齿形数组中第 3 个数组的第一个元素。让我们看看完整的代码 - 示例 using System; namespace Demo {    class Program {       static void Main(string[] args) {          int[][] x = new int[][]{new int[]{10, 20}, new int[]{30, 40}, new int[]{50, 60}, new int[]{ 70, 80 }, new int[]{ 90, 100 ... 阅读更多

如何在 C# 列表中添加整数值?

Samual Sam
更新于 2020-06-21 14:58:47

8K+ 次浏览

要在 C# 中向列表添加整数值,请使用 Add() 方法。首先,在 C# 中声明一个整数列表 - List list1 = new List(); 现在添加整数值 - list1.Add(900); list1.Add(400); list1.Add(300); 让我们看看完整的代码 - 示例 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo {    public class Program {       public static void Main(String[] args) {          List list1 = new List();          list1.Add(900);          list1.Add(400);          list1.Add(300);          Console.WriteLine(list1.Count);       }    } }

如何在 C# 中向列表添加项?

Arjun Thakur
更新于 2020-06-21 14:58:17

761 次浏览

首先,声明一个列表 - var teams = new List(); 要向 C# 列表添加项,请使用 Add() 方法 - teams.Add("US"); teams.Add("Canada"); teams.Add("India"); teams.Add("Australia"); 您可以尝试运行以下代码,以在 C# 中向列表添加项 - 示例 using System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       var teams = new List();       teams.Add("US");       teams.Add("Canada");       teams.Add("India");       teams.Add("Australia");           Console.WriteLine("Elements...");       foreach (var countries in teams) {          Console.WriteLine(countries);       }    } }

使用 C# 查找字符串中的所有子字符串

karthikeya Boyini
更新于 2020-06-21 14:25:18

395 次浏览

使用 C# 中的 substring() 方法查找字符串中的所有子字符串。假设我们的字符串是 - pqr 遍历字符串的长度,并从字符串的开头到结尾使用 Substring 函数 - for (int start = 0; start < pqr.Length; start++) {    for (int end = start + 1; end <= pqr.Length; end++) {       Console.WriteLine(pqr.Substring(start, end - start));    }}

广告
© . All rights reserved.