找到 34423 篇文章,关于编程

C#程序打印列表的所有子列表

karthikeya Boyini
更新于 2020年6月20日 14:34:45

503 次浏览

首先,创建一个列表 - List list = new List(); 此处的字符串为“xyz”,我们将找到其子列表。在循环中,我们将声明另一个列表,该列表将在每次 true 迭代时生成子列表 - for (int i = 1; i < str.Length; i++) {    list.Add(str[i - 1].ToString());    List newlist = new List();    for (int j = 0; j < list.Count; j++) {       string list2 = list[j] + str[i];       newlist.Add(list2);    }    list.AddRange(newlist); } 完整的代码如下 - 示例 在线演示 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo ... 阅读更多

如何在 C# 中用单个语句将相同的值赋给多个变量?

Ankith Reddy
更新于 2020年6月20日 14:35:30

3K+ 次浏览

要在单行中将相同的值赋给多个变量,请使用 = 运算符 - val1 = val2 = 20; 以上语句将 20 分配给变量 val1 和 val2,如下面的代码所示 - 示例 在线演示 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          int val1, val2;          val1 = val2 = 20;          Console.WriteLine("Value1 = "+val1);          Console.WriteLine("Value2 = "+val2);       }    } }输出Value1 = 20 Value2 = 20

C#程序打印给定数字中所有能被 3 和 5 整除的数字

Samual Sam
更新于 2020年6月20日 14:36:21

5K+ 次浏览

要打印能被 3 和 5 整除的数字,请使用 && 运算符并检查两个条件 - f (num % 3 == 0 && num % 5 == 0) {} 如果上述条件为真,则表示该数字能被 3 和 5 整除。完整的代码如下 - 示例 在线演示 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          int num;          num = 15;          Console.WriteLine("Number: "+num);          // 检查数字是否能被 3 和 5 整除          if (num % 3 == 0 && num % 5 == 0) {             Console.WriteLine("能被 3 和 5 整除");          } else {             Console.WriteLine("不能被 3 和 5 整除");          }          Console.ReadLine();       }    } }输出Number: 15 能被 3 和 5 整除

C#程序打印两个列表的所有公共元素

karthikeya Boyini
更新于 2020年6月20日 14:37:48

2K+ 次浏览

首先创建两个列表 - List list1 = new List() {40, 20, 60, 3, 55}; List list2 = new List() {20, 70, 55, 80}; 要查找公共元素,请使用 Intersect - list1.Intersect(list2) 以下是查找两个列表之间公共元素的完整代码 - 示例 在线演示 using System; using System.Linq; using System.Collections.Generic; namespace Demo {    class Program {       static void Main(string[] args) {          List list1 = new List() {40, 20, 60, 3, 55};          List list2 = new List() {20, 70, 55, 80};          Console.WriteLine("公共元素:");          foreach(int value in list1.Intersect(list2))          Console.WriteLine(value);       }    } }输出公共元素: 20 55

如何在 C# 中编写多行注释?

Samual Sam
更新于 2020年6月20日 14:40:21

767 次浏览

跨越多行的注释称为多行注释 - /* 以下是在 C# 中的多行注释 */ /*...*/ 将被编译器忽略,用于在程序中添加注释。以下是一个示例 C# 程序,展示如何添加多行注释 - using System; namespace Demo {    class Program {       static void Main(string[] args) {          /* 以下是在 C# 中的多行          注释          /*          // 打印          Console.WriteLine("Hello World");          Console.ReadKey();       }    } }

如何在 C# 中从枚举获取 int 值?

karthikeya Boyini
更新于 2020年6月20日 14:41:32

171 次浏览

首先,设置枚举 - public enum Vehicle { Car, Bus, Truck } 现在使用类型转换将枚举转换为 int - int a = (int)Vehicle.Car; 以下是将枚举转换为 int 的完整代码 - 示例 在线演示 using System; public class Demo {    public enum Vehicle { Car, Bus, Truck }    public static void Main() {       int a = (int)Vehicle.Car;       int b = (int)Vehicle.Bus;       int c = (int)Vehicle.Truck;       Console.WriteLine("Car = {0}", a);       Console.WriteLine("Bus = {0}", b);       Console.WriteLine("Truck = {0}", c);    } }输出Car = 0 Bus = 1 Truck = 2

什么是 C# 中的垃圾回收?

Chandu yadav
更新于 2020年6月20日 14:40:47

9K+ 次浏览

垃圾收集器 (GC) 管理内存的分配和释放。垃圾收集器充当自动内存管理器。您无需了解如何分配和释放内存或管理使用该内存的对象的生命周期。每当您使用“new”关键字声明对象或值类型被装箱时,就会进行分配。分配通常非常快。当没有足够的内存来分配对象时,GC 必须收集和处置垃圾内存,以便为新的分配提供内存。此过程称为垃圾回收。C# 中的垃圾回收... 阅读更多

如何使用 C# 计算 3 的幂?

George John
更新于 2020年6月20日 14:13:57

223 次浏览

对于 3 的幂,将幂设置为 3 并应用如下所示的递归代码 - if (p!=0) { return (n * power(n, p - 1)); } 假设数字为 5,则迭代将为 - power(5, 3 - 1)); // 25 power (5,2-1): // 5 以上将返回 5*25 即 125,如下所示 - 示例 在线演示 using System; using System.IO; public class Demo {    public static void Main(string[] args) {       int n = 5;       int p = 3;       long res;       res = power(n, p);       Console.WriteLine(res);    }    static long power (int n, int p) {       if (p!=0) {          return (n * power(n, p - 1));       }       return 1;    } }输出125

什么是 C# 中 SortedList 类的 Values 属性?

Samual Sam
更新于 2020年6月20日 14:15:10

72 次浏览

首先,声明 SortedList 类 - SortedList list = new SortedList(); 现在添加值 - list.Add("S1", "Wallets"); list.Add("S2", "Sunglasses"); list.Add("S3", "Backpacks"); 以下是使用 SortedList 类 Values 属性的代码 - 示例 在线演示 using System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          SortedList list = new SortedList();          list.Add("S1", "Wallets");          list.Add("S2", "Sunglasses");          list.Add("S3", "Backpacks");          foreach (string value in list.Values) {             Console.WriteLine(value);          }       }    } }输出Wallets Sunglasses Backpacks

什么是 C# 中 Hashtable 类的 Values 属性?

Ankith Reddy
更新于 2020年6月20日 14:15:50

81 次浏览

Values 属性获取一个包含 Hashtable 中值的 ICollection。声明 Hashtable 集合 - Hashtable ht = new Hashtable(); 现在添加值 ht.Add("One", "Henry"); ht.Add("Two", "Kevin"); ht.Add("Three", "David"); 要显示 Hashtable 中的值,代码如下 - 示例 在线演示 using System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          Hashtable ht = new Hashtable();          ht.Add("One", "Henry");          ht.Add("Two", "Kevin");          ht.Add("Three", "David");          // 显示值          foreach (string value in ht.Values) {             Console.WriteLine(value);          }          Console.ReadKey();       }    } }输出David Henry Kevin

广告
© . All rights reserved.