找到 34423 篇文章 关于编程
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
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; } ... }
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); } }解压缩要解压缩文件,请使用相同的… 阅读更多
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
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
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; } ... 阅读更多
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]; ... 阅读更多
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
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 ... 阅读更多
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP