一、硬盘序列号
1—第一种写法
//引入空间
using System.Runtime.InteropServices;
using System.Management;
using System.Diagnostics;
//功能代码
HardDisk myHD = new HardDisk();
String hdSerial = HardDisk.GetSerialNo(0);
Console.WriteLine("硬盘序列号="+hdSerial);
Console.WriteLine("第二种方法=" +HardDisk.GetDiskID());
二、日期变量格式化
string a1=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
Console.WriteLine("值:" + a1);
a1=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
Console.WriteLine("值:" + a1);
结果如下:
三、nameof()语法糖
是取变量的简单名称字符串(非完成限定名)。用于代码中使用变量的名称,如果变量名称变更,编译器不会报错,改用nameof()函数后,将自动完成变量名称更新,减少硬编码。
private static void Main(string[] args)
{
if (args==null)
{
throw new ArgumentNullException("args");//旧的写法 变量名的字符串做参数
//throw new ArgumentNullException(nameOf(args));//新的写法 避免了args变量名更改后,忘记更改字符串"args",因为字符串编译器是不错提示错误的
}
}
更多用法
Console.WriteLine(nameof(System.Collections.Generic)); // output: Generic
Console.WriteLine(nameof(List<int>)); // output: List
Console.WriteLine(nameof(List<int>.Count)); // output: Count
Console.WriteLine(nameof(List<int>.Add)); // output: Add
var numbers = new List<int> { 1, 2, 3 };
Console.WriteLine(nameof(numbers)); // output: numbers
Console.WriteLine(nameof(numbers.Count)); // output: Count
Console.WriteLine(nameof(numbers.Add)); // output: Add