代码如下:
//int 类型加 string 类型,得到字符串
var str = 1 + "11"; //返回 111 字符串
//替换字符不存在时不报错
var str1 = "xxx".Replace(".zip", string.Empty);
var str2 = "xxx.zip".Replace(".zip", string.Empty);
var result = str1 == str2; //返回 ture
//单个元素遍历不会改变原数据,对象遍历会改变原数据
var list = new List<int> { 1, 2, 3 };
list.ForEach(t=> t = 6); //数据不会改变
var students = new List<Student>
{
new Student() { Id = 1, Name = "张三", Age = 18, Sex = 0 },
new Student() { Id = 2, Name = "李四", Age = 20, Sex = 0 },
};
students.ForEach(t => t.Sex = 1); //数据会改变
//根据当前日期推算出本周星期一和周日:
var mondayDate = DateTime.Now.AddDays(0 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 1); //本周一
var sundayDate = DateTime.Now.AddDays(6 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 1); //本周日
//ValueKind = Object :{} 遇到这种数据,先转为object类型,然后转为string类型,再序列化为dynamic类型
dynamic data = JsonConvert.DeserializeObject<dynamic>((res.Data as object).ToString());
var id = data?.id;
//Environment.NewLine 输出 \r\n
Console.Write($"你好啊{Environment.NewLine}你好啊{Environment.NewLine}你好啊{Environment.NewLine}");
//获取当前日期对应的中英文星期
var week_en = DateTime.Now.DayOfWeek; //返回 Friday
var week_zh = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn")); //返回 星期五
//获取 url 信息
Uri uri = new("https://www.baidu.com/xx/yy?v=1");
var scheme = uri.Scheme; //返回 https
var host = uri.Host; //返回 www.baidu.com
var port = uri.Port; //返回 443
var localPath = uri.LocalPath; //返回 /xx/yy
var absolutePath = uri.AbsolutePath; //返回 /xx/yy
var pathAndQuery = uri.PathAndQuery; //返回 /xx/yy?v=1
var query = uri.Query; //返回 ?v=1
//获取当前项目路径
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; //返回当前项目绝对路径 D:\xx\ConsoleApp1\bin\Debug\net6.0\
var applicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //返回当前项目绝对路径 D:\xx\ConsoleApp1\bin\Debug\net6.0\
var currentDirectory = Environment.CurrentDirectory; //返回当前项目文件路径 D:\xx\ConsoleApp1\bin\Debug\net6.0
var location = typeof(Program).Assembly.Location; //返回当前项目dll文件 D:\xx\ConsoleApp1\bin\Debug\net6.0\ConsoleApp1.dll
var fileName = Process.GetCurrentProcess().MainModule.FileName; //返回当前项目可执行文件exe D:\xx\ConsoleApp1\bin\Debug\net6.0\ConsoleApp1.exe
//查询 DataTable 数据
DataTable studentDate = new DataTable();
var name = studentDate?.Select($"Id = {id}").FirstOrDefault()?["Name"].ToString() ?? string.Empty;
//简单定时器实现
System.Timers.Timer timer = new System.Timers.Timer(2000)
{
AutoReset = true,
Enabled = true
};
timer.Elapsed += (o, e) => { Console.WriteLine($"这里是定时任务,每隔2秒执行一次,object:{o.GetType().Name},ElapsedEventArgs:{e.SignalTime.ToString("yyyy-MM-dd HH:mm:ss:fff")}"); };
timer.Start();
//利用Json序列化/反序列化实现对象转换
var student = JsonConvert.DeserializeObject<Student>(JsonConvert.SerializeObject(StudentDto),
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore, //空值忽略
MissingMemberHandling = MissingMemberHandling.Ignore //未匹配字段忽略
});
//获取json字符串中的信息
string jsonStr = "{\"message\":true,\"content\":{\"amcount\":4,\"pmcount\":4,\"nightcount\":4,\"weekcourse\":{\"monday\":[{\"weekday\":\"星期一\",\"coursename\":\"纲要\"},{\"weekday\":\"星期一\",\"coursename\":\"纲要\"}]}}}";
JObject jObject = (JObject)JsonConvert.DeserializeObject(jsonStr ?? string.Empty)!; //当 jsonStr 为null时,不报错
//JObject jObject = JObject.Parse(jsonStr ?? string.Empty); //当 jsonStr 为null时,报错
var contentJToken = jObject?["content"];
var weekcourseJToken = contentJToken?["weekcourse"];
if (weekcourseJToken != null)
{
foreach (var item in weekcourseJToken)
{
var name = ((JProperty)item).Name; //返回 "monday"
var values = ((JProperty)item).Value; //返回 [{"weekday": "星期一","coursename": "纲要"},{"weekday": "星期一","coursename": "纲要"}]
var value = ((JProperty)item).Value[0]; //返回 {"weekday": "星期一","coursename": "纲要"}
var weekday = value?["weekday"]?.ToString(); //返回 "星期一"
}
}
后续遇到再更新