C#中的lambda表达式描述了一种模式。Lambda表达式在表达式上下文中具有标记=>。这被解读为“goes to”运算符,在声明lambda表达式时使用。在这里,我们正在查找列表中第一个大于50的元素。list.FindIndex(x => x > 50);上面使用了标记=>。如下所示:示例 实时演示 using System; using System.Collections.Generic; class Demo { static void Main() { List<int> list = new List<int> { 44, 6, 34, 23, 78 }; int res = list.FindIndex(x => x > 50); Console.WriteLine("Index: "+res); } }输出Index: 4
关键字是预定义给C#编译器的保留字。这些关键字不能用作标识符。但是,如果您想将这些关键字用作标识符,则可以在关键字前加上@字符。以下是C#中两种类型的关键字:保留关键字 abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in in (泛型修饰符) int interface internal is lock long namespace new null object operator out out (泛型修饰符) override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while上下文关键字 add alias ascending descending dynamic from get global group into join let orderby partial (类型) partial(方法) remove select set
要将数组传递给方法,需要将数组作为方法参数传递。int displaySum(int[] arr, int size) { }现在调用它 −sum = d.displaySum(myArr, 5 ) ;示例 实时演示 using System; namespace Test { class Demo { int displaySum(int[] arr, int size) { int i; int sum = 0; for (i = 0; i < size; ++i) { sum += arr[i]; } return sum; } static void Main(string[] args) { Demo d = new Demo(); int [] myArr = new int[] {59, 34, 76, 65, 12, 99}; int sum; sum = d.displaySum(myArr, 5 ) ; /* 输出返回值 */ Console.WriteLine( "Sum: {0} ", sum ); Console.ReadKey(); } } }输出Sum: 246
要快速将十进制转换为其他进制,请使用堆栈。让我们看一个例子。首先,我将变量“baseNum”设置为2 int baseNum = 2;同样,如果您想要另一个进制,则 −// 8进制 int baseNum = 8; // 10进制 int baseNum = 10;获取值后,设置一个堆栈并通过获取余数和其他计算来获取值,如下所示。这里,n是十进制数。Stack s = new Stack(); do { s.Push(n % baseNum); n /= baseNum; } while (n != 0);使用堆栈后,弹出元素。 ... 阅读更多
首先,设置一个二维数组。int[, ] arr = new int[10, 10];现在,从用户那里获取元素 −for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { arr[i, j] = Convert.ToInt16(Console.ReadLine()); } }让我们看看显示矩阵的完整示例。示例 实时演示 using System; using System.Linq; class Demo { static void Main() { int m, n, i, j; // 矩阵的行和列+ m = 2; n = ... 阅读更多
要执行矩阵加法,请取两个矩阵。输入矩阵一和矩阵二的行和列。记住,两个矩阵都应该是方阵才能相加。现在将元素添加到两个矩阵中。声明一个新数组并将两个数组添加到其中。arr3[i, j] = arr1[i, j] + arr2[i, j];让我们看看完整的代码 −示例 using System; using System.Linq; class Demo { static void Main() { int m, n, i, j; Console.Write("Enter number of rows and columns of the matrix "); ... 阅读更多