找到 34423 篇文章,关于编程

如何在 C# 中使用 ‘foreach’ 循环迭代数组?

karthikeya Boyini
更新于 2020年6月23日 11:41:04

391 次查看

foreach 循环类似于 for 循环;但是,该循环针对数组或组中的每个元素执行。因此,foreach 循环中不存在索引。让我们来看一个冒泡排序的示例,在对元素排序后,我们将使用 foreach 循环显示这些元素。foreach (int p in arr) Console.Write(p + " ");以下是一个完整的示例。示例 在线演示using System; namespace BubbleSort {    class MySort {       static void Main(string[] args) {          int[] arr = { 78, 55, 45, 98, 13 };          int temp;          for (int j = 0; j

C# 中方法和函数的区别

George John
更新于 2020年6月23日 11:40:18

6K+ 次查看

在 C# 中,方法和函数是相同的。但是,方法用于 C#,并且是通过指定类操作的函数。方法是一组一起执行任务的语句。每个 C# 程序至少有一个包含名为 Main 的方法的类。以下是一个简单的示例,展示如何在 C# 中创建方法。示例class NumberManipulator {    public int FindMax(int num1, int num2) {       /*局部变量声明*/       int result;       if (num1 > num2) {          result = num1;       }else {          result = num2;       }       return result;    }    … }

使用 C# 中的 GZIP 格式压缩和解压缩文件

Samual Sam
更新于 2020年6月23日 11:42:42

2K+ 次查看

要使用 GZIP 格式压缩和解压缩文件,请使用 GZipStream 类。压缩要压缩文件,请将 GZipStream 类与 FileStream 类一起使用。设置以下参数。要压缩的文件和输出 zip 文件的名称。在这里,outputFile 是输出文件,文件被读入 FileStream。示例using(var compress = new GZipStream(outputFile, CompressionMode.Compress, false)) {    byte[] b = new byte[inFile.Length];    int read = inFile.Read(b, 0, b.Length);    while (read > 0) {       compress.Write(b, 0, read);       read = inFile.Read(b, 0, b.Length);    } }解压缩要解压缩文件,请使用相同的… 阅读更多

如何在 C# 中将数组按升序排序?

Ankith Reddy
更新于 2020年6月23日 11:43:34

9K+ 次查看

首先,设置未排序的数组。int[] list = {98, 23, 97, 36, 77};使用 Sort() 方法对数组进行排序。Array.Sort(list);您可以尝试运行以下代码以按升序对数组进行排序。示例 在线演示using System; namespace Demo {    public class MyApplication {       public static void Main(string[] args) {          int[] list = {98, 23, 97, 36, 77};          Console.WriteLine("原始未排序列表");          foreach (int i in list) {             Console.Write(i + " ");          }          Array.Sort(list);          Console.WriteLine("已排序列表");          for(int i=0; i

如何在 C# 中初始化二维数组?

Arjun Thakur
更新于 2020年6月23日 11:28:14

19K+ 次查看

二维数组是一维数组的列表。可以通过为每一行指定带括号的值来初始化二维数组。int [,] a = new int [4,4] {    {0, 1, 2, 3} ,    {4, 5, 6, 7} ,    {8, 9, 10, 11} ,    {12, 13, 14, 15} };以下是一个示例,展示如何在 C# 中使用二维数组。示例 在线演示using System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          /*一个包含 3 行和 2 列的数组*/          int[,] a = new int[3, 2] {{0,0}, {1,2}, {2,4} };          int i, j;          /*输出每个数组元素的值*/          for (i = 0; i < 3; i++) {             for (j = 0; j < 2; j++) {                Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);             }          }          Console.ReadKey();       }    } }输出a[0,0] = 0 a[0,1] = 0 a[1,0] = 1 a[1,1] = 2 a[2,0] = 2 a[2,1] = 4

C# 类中私有成员变量的作用域是什么?

Chandu yadav
更新于 2020年6月23日 11:29:11

332 次查看

只有相同类的函数才能访问其私有成员。私有访问说明符允许类将其成员变量和成员函数隐藏在其他函数和对象之外。示例 在线演示using System; namespace RectangleApplication {    class Rectangle {       //成员变量       private double length;       private double width;       public void Acceptdetails() {          length = 10;          width = 14;       }       public double GetArea() {          return length * width;       } … 阅读更多

C# 中 ‘as’ 运算符的用途是什么?

Chandu yadav
更新于 2020年6月23日 11:32:35

137 次查看

“as”运算符执行兼容类型之间的转换。它类似于强制转换操作,它仅执行引用转换、可空转换和装箱转换。“as”运算符不能执行其他转换,例如用户定义的转换,而应使用强制转换表达式执行这些转换。以下是一个示例,展示了在 C# 中使用 as 操作。这里 as 用于转换。string s = obj[i] as string;尝试运行以下代码以使用 C# 中的 ‘as’ 运算符。示例 在线演示using System; public class Demo {    public static void Main() {       object[] obj = new object[2]; … 阅读更多

C# 程序:按降序排列数组

Samual Sam
更新于 2020年6月23日 11:33:39

4K+ 次查看

初始化数组。int[] myArr = new int[5] {98, 76, 99, 32, 77};比较数组中的第一个元素与下一个元素以找到最大的元素,然后是第二大的元素,依此类推。if(myArr[i] < myArr[j]) {    temp = myArr[i];    myArr[i] = myArr[j];    myArr[j] = temp; }上面,i 和 j 最初设置为。i=0; j=i+1;尝试运行以下代码以按降序排列数组。示例 在线演示using System; public class Demo {    public static void Main() {       int[] myArr = new int[5] {98, 76, 99, 32, 77};       int i, j, temp;       Console.Write("元素:");       for(i=0;i

C# 程序:确定字符串是否包含所有唯一字符

George John
更新于 2020年6月23日 11:34:36

3K+ 次查看

使用 C# 中的 substring() 方法检查每个子字符串是否包含唯一字符。循环到字符串长度为止。如果任何一个子字符串与另一个匹配,则表示该字符串不包含唯一字符。您可以尝试运行以下代码以确定字符串是否包含所有唯一字符。示例 在线演示using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class Demo {    public bool CheckUnique(string str) {       string one = "";       string two = "";       for (int i = 0; i … 阅读更多

C# 和 .Net 的区别

karthikeya Boyini
更新于 2019年7月30日 22:30:23

4K+ 次查看

C# 是一种编程语言,.NET 是一个框架。.NET 具有公共语言运行时 (CLR),它是 .NET 框架的一个虚拟组件。.NET 不仅包含 C#,还可以通过它使用 VB、F# 等。C# 是 .NET 的一部分,具有以下特性:布尔条件、自动垃圾回收、标准库、程序集版本控制、属性和事件、委托和事件管理、易于使用的泛型、索引器、条件编译、简单的多线程、LINQ 和 Lambda 表达式、与 Windows 集成。

广告
© . All rights reserved.