找到 34423 篇文章 关于编程
285 次浏览
使用 PadRight() 方法用空格填充字符串的末尾。您也可以用 Unicode 字符填充它。假设以下为我们的字符串。string myStr = "Text1";要在上述字符串的末尾设置填充,请使用 PadRight 方法。myStr.PadRight(10);以下是完整的示例。示例 在线演示using System; class Demo { static void Main() { string myStr = "Text1"; // 在末尾设置填充 Console.Write(myStr.PadRight(10)); Console.WriteLine("Text2"); } }输出Text1 Text2
171 次浏览
使用 Math.DivRem 方法计算两个数字的商并返回余数。首先,设置被除数和除数。// 被除数 long dividend = 30; // 除数 long divisor = 7;现在,使用 DivRem 方法。long quotient = Math.DivRem(dividend, divisor, out rem);示例 在线演示using System; using System.Globalization; class Demo { static void Main() { // 余数 long rem; // 被除数 long dividend = 98; // 除数 long divisor = 9; long quotient = Math.DivRem(dividend, divisor, out rem); Console.WriteLine("{0} \ {1} = {2} and remainder = {3}", dividend, divisor, quotient, rem); } }输出98 \ 9 = 10 and remainder = 8
157 次浏览
使用 Math.BigMul() 方法查找两个 32 位数字的乘积。以下是我们的两个数字。int one = 345272828; int two = 887685744;现在,获取乘积。long res; res = Math.BigMul(one, two);示例 在线演示using System; using System.Globalization; class Demo { static void Main() { int one = 345272828; int two = 887685744; long res; res = Math.BigMul(one, two); Console.WriteLine("{0} * {1} = {2}", one, two, res); } }输出345272828 * 887685744 = 306493767206164032
3K+ 次浏览
使用 First() 方法从数组中获取第一个元素。首先,设置一个数组。int[] arr = {20, 40, 60, 80 , 100};现在,使用 Queryable First() 方法返回第一个元素。arr.AsQueryable().First();以下是完整的示例。示例 在线演示using System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { int[] arr = {20, 40, 60, 80 , 100}; // 获取第一个元素 int res = arr.AsQueryable().First(); Console.WriteLine(res); } }输出20
402 次浏览
可排序标准格式说明符表示自定义日期和时间格式字符串。格式字符串由 DateTimeFormatInfo.SortableDateTimePattern 属性定义。自定义格式字符串。yyyy'-'MM'-'dd'T'HH':'mm':'ss示例 在线演示using System; class Demo { static void Main() { DateTime date = new DateTime(2018, 9, 5, 2, 12, 40); Console.WriteLine(date.ToString("s")); } }输出2018-09-05T02:12:40
250 次浏览
C# 中的 ElementAt() 方法用于获取指定索引位置的元素。首先,设置字符串数组。string[] str = { "Jack", "Pat", "David"};现在,要获取特定索引处的元素,请使用 ElementAt() 方法,如下例所示 -示例 在线演示using System.IO; using System; using System.Linq; class Program { static void Main() { string[] str = { "Jack", "Pat", "David"}; Random r = new Random(DateTime.Now.Second); // 生成随机字符串 string res = str.AsQueryable().ElementAt(r.Next(0, str.Length)); Console.WriteLine("随机名称 = '{0}'", res); } }输出随机名称 = 'Jack'
3K+ 次浏览
使用 Except() 方法获取两个数组之间的差异。以下是两个数组。int[] arr = { 9, 12, 15, 20, 35, 40, 55, 67, 88, 92 }; int[] arr2 = { 20, 35 };要获取差异,请使用 Except() 方法,该方法返回第一个列表,除了第二个列表中的元素。arr.AsQueryable().Except(arr2);以下是完整的示例。示例 在线演示using System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { int[] arr = { 5, 10, 15, 20, 35, 40 }; int[] except = { 20, 35 }; ... 阅读更多
2K+ 次浏览
当参数的格式无效时,会引发 FomatException。让我们来看一个例子。当我们将 int 以外的值设置为 int.Parse() 方法时,会引发 FormatException,如下所示 -示例 在线演示using System; class Demo { static void Main() { string str = "3.5"; int res = int.Parse(str); } }当编译上述程序时,会抛出以下错误,因为我们传递的值不是整数。输出未处理的异常: System.FormatException: 输入字符串的格式不正确。
1K+ 次浏览
要获取 C# 中 3D 数组的大小,请使用 GetLength() 方法,并将参数作为维度的索引。GetLength(dimensionIndex)获取大小。arr.GetLength(0) arr.GetLength(1) arr.GetLength(2)示例 在线演示using System; class Program { static void Main() { int[,,] arr = new int[3,4,5]; // 维度的长度 Console.WriteLine(arr.GetLength(0)); Console.WriteLine(arr.GetLength(1)); Console.WriteLine(arr.GetLength(2)); // 长度 Console.WriteLine(arr.Length); } }输出3 4 5 60
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP