找到 34423 篇文章 适用于编程

C# 程序显示一个月的天数

Arjun Thakur
更新于 2020-06-22 14:12:25

636 次查看

使用 DaysInMonth 显示一个月的天数。将年份和月份作为 DaysInMonth() 方法的参数 -DateTime.DaysInMonth(2018, 8);以下是一个示例 -示例 实时演示using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       Console.WriteLine("今天 = {0}", DateTime.Today);       int num_days = DateTime.DaysInMonth(2018, 8);       Console.WriteLine("8 月份的天数: "+num_days);    } }输出今天 = 2018/9/4 上午 12:00:00 8 月份的天数: 31

C# 程序显示下一天

Ankith Reddy
更新于 2020-06-22 14:12:50

3K+ 次查看

要显示下一天,请使用 AddDays() 方法和值 +1 以获取下一天。首先,使用以下方法获取当前日期 -DateTime.Today现在,将 1 添加到 AddDays() 方法以获取下一天 -DateTime.Today.AddDays(1)以下是代码 -示例 实时演示using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       Console.WriteLine("今天 = {0}", DateTime.Today);       Console.WriteLine("前一天 = {0}", DateTime.Today.AddDays(-1));       Console.WriteLine("下一天(明天) = {0}", DateTime.Today.AddDays(1));    } }输出今天 = 2018/9/4 上午 12:00:00 前一天 = 2018/9/3 上午 12:00:00 下一天(明天) = 2018/9/5 上午 12:00:00

C# 程序显示前一天

George John
更新于 2020-06-22 14:13:12

4K+ 次查看

要显示前一天,请使用 AddDays() 方法和值 -1 以获取前一天。首先,使用以下方法获取当前日期 -DateTime.Today现在,将 -1 添加到 AddDays() 方法以获取前一天 -DateTime.Today.AddDays(-1)以下是代码 -示例 实时演示using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       Console.WriteLine("今天 = {0}", DateTime.Today);       Console.WriteLine("前一天 = {0}", DateTime.Today.AddDays(-1));    } }输出今天 = 2018/9/4 上午 12:00:00 前一天 = 2018/9/3 上午 12:00:00

C# 程序遍历二维数组

karthikeya Boyini
更新于 2020-06-22 14:13:58

4K+ 次查看

声明一个二维数组 -string[, ] array = new string[3, 3];设置数组中的元素 -array[0, 0] = "One"; array[0, 1] = "Two"; array[0, 2] = "Three"; array[1, 0] = "Four"; array[1, 1] = "Five"; array[1, 2] = "Six"; array[2, 0] = "Seven"; array[2, 1] = "Eight"; array[2, 2] = "Nine";现在,获取上界以获取遍历数组的维度 -int uBound0 = array.GetUpperBound(0); int uBound1 = array.GetUpperBound(1);迭代嵌套循环,直到上述两个值,如下面的代码所示 -示例 实时演示using System; using System.Collections.Generic; using System.Linq; public class Demo {   ... 阅读更多

C# 中的空列表

Samual Sam
更新于 2020-06-22 14:14:20

8K+ 次查看

设置一个具有零个元素的列表 -List myList = new List();现在检查列表是否为空或为 null -Console.WriteLine(myList == null);上面返回“False”,即列表不为 null - 列表为空。让我们看看完整的代码 -示例 实时演示using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List myList = new List();       // 返回 false,即空列表(而不是 null 列表)       Console.WriteLine(myList == null);    } }输出False

C# 程序减去两个 TimeSpan

karthikeya Boyini
更新于 2020-06-22 14:04:46

369 次查看

首先,设置两个 TimeSpans -TimeSpan t1 = TimeSpan.FromMinutes(2); TimeSpan t2 = TimeSpan.FromMinutes(1);要添加它,请使用 Subtract() 方法 -imeSpan res = t1.Subtract(t2);这是完整的代码 -示例 实时演示using System; using System.Linq; public class Demo {    public static void Main() {       TimeSpan t1 = TimeSpan.FromMinutes(2);       TimeSpan t2 = TimeSpan.FromMinutes(1);       Console.WriteLine("第一个 TimeSpan: "+t1);       Console.WriteLine("第二个 TimeSpan: "+t2);       // 减法       TimeSpan res = t1.Subtract(t2);       Console.WriteLine("结果 TimeSpan: "+res);    } }输出第一个 TimeSpan: 00:02:00 第二个 TimeSpan: 00:01:00 结果 TimeSpan: 00:01:00

C# 中的 Null 列表

Samual Sam
更新于 2020-06-22 14:05:11

2K+ 次查看

C# 中存在 null 列表。要检查列表是否为 null,请将其与 null 字面量进行比较。像这样设置 null -List myList = null;现在,要检查 null,请使用相等运算符 -myList == null;以下是一个示例 -示例 实时演示using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List myList = null;       // 检查 null       Console.WriteLine(myList == null);    } }输出True

从 C# 列表中删除重复项

karthikeya Boyini
更新于 2020-06-22 14:06:01

6K+ 次查看

使用 Distinct() 方法从 C# 中的列表中删除重复项。首先,添加一个新列表 -List arr1 = new List(); arr1.Add(10); arr1.Add(20); arr1.Add(30); arr1.Add(40); arr1.Add(50); arr1.Add(30); arr1.Add(40); arr1.Add(50);要删除重复元素,请使用 Distinct() 方法,如下所示 -List distinct = arr1.Distinct().ToList();这是完整的代码 -示例 实时演示using System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List arr1 = new List();       arr1.Add(10);       arr1.Add(20);       arr1.Add(30);       arr1.Add(40);       arr1.Add(50);       arr1.Add(30);       arr1.Add(40); ... 阅读更多

获取 C# 列表中元素的范围

Samual Sam
更新于 2020-06-22 14:06:43

2K+ 次查看

使用 GetRange() 方法获取元素的范围 -首先,设置一个列表并添加元素 -List arr1 = new List(); arr1.Add(10); arr1.Add(20); arr1.Add(30); arr1.Add(40); arr1.Add(50);现在,在一个新列表中获取索引 1 和 3 之间的元素范围 -List myList = arr1.GetRange(1, 3);这是完整的代码 -示例 实时演示using System; using System.Collections.Generic; public class Demo {    public static void Main() {       List arr1 = new List();       arr1.Add(10);       arr1.Add(20);       arr1.Add(30);       arr1.Add(40);       arr1.Add(50);       Console.WriteLine("初始列表 ...");   ... 阅读更多

在 C# 列表中一次插入多个元素

karthikeya Boyini
更新于 2020-06-22 14:07:28

3K+ 次查看

使用 InsertRange() 方法在 C# 中的现有列表之间插入列表。通过此方法,您可以轻松地将多个元素添加到现有列表中。让我们首先设置一个列表 -List arr1 = new List(); arr1.Add(10); arr1.Add(20); arr1.Add(30); arr1.Add(40); arr1.Add(50);现在,让我们设置一个数组。此数组的元素是我们将添加到上述列表中的元素 -int[] arr2 = new int[4]; arr2[0] = 60; arr2[1] = 70; arr2[2] = 80; arr2[3] = 90;我们将上述元素添加到列表中 -arr1.InsertRange(5, arr2);这是完整的代码 -示例 实时演示using System; using System.Collections.Generic; public ... 阅读更多

广告

© . All rights reserved.