C# 基础(四)

一、运算符

  1. 阶乘
  public static long Factorial(int num) {
      if (num == 0) {
          return 1;
      }
      else { 
          return Factorial(num - 1)*num;
      }
     // 等价于 return num == 0 ? 1: Factorial(num - 1) * num;
    }
  1. 十进制转二进制
   public static void Dec2Bin(long num) {
	     if (num > 0)
	       {
	           Dec2Bin(num >> 1);
	           Console.Write(num & 0x01);
	       }
   }
  1. ?的用法
1. sendmsg?.invoke("123"); // sendmsg不为空则执行invork
2. int?x = null;           // 声明这个值可以为null 且初始值为null
3. int vel = x??5;         // 如果不为null,则返回x 否则返回5

二、类型转换

  1. 强转(略)
  2. Convert类
// 十进制转二进制
 Console.Write(Convert.ToString(num,2));

在这里插入图片描述

  1. BitConvert类
 public static void Bytes2String(byte[] data)
 {
     Console.Write(BitConverter.ToString(data));
 }

 public static byte[] Int2Bytes(int data)
 {
     return BitConverter.GetBytes(data);
 }
  1. ToString
 // 十进制转十六进制
 Console.Write(Convert.ToString(num, 16));
 Console.Write(num.ToString("x2"));

三、数学运算

  1. 最大值、最小值、平均值、、求和
    int[] a = new int[] { 10, 20, 64, 15 };

	Console.WriteLine(a.Max());
	Console.WriteLine(a.Min());
	Console.WriteLine(a.Average());
	Console.WriteLine(a.Sum());
	Console.WriteLine(a.Sum(r => r*2)); // 每个值×2再相加
  1. Math 数学类 ,计算平方、绝对值、立方、拟合等
Console.WriteLine(Math.Abs(-3)); ;

四、时间

  1. 星期
 Console.WriteLine(DateTime.Today.DayOfWeek);
  1. 农历
ChineseLunisolarCalendar chineseLunisolarCalendar = new ChineseLunisolarCalendar();
Console.WriteLine($"{DateTime.Today}是农历{chineseLunisolarCalendar.GetYear(DateTime.Today)}{chineseLunisolarCalendar.GetMonth(DateTime.Today)}{chineseLunisolarCalendar.GetDayOfMonth(DateTime.Today)}日" );
  1. 时间
 Console.WriteLine($"一天总共有多少秒{new TimeSpan(24, 0, 0).TotalSeconds}");
  1. 解析
var time = DateTime.Now;
Console.WriteLine(time);
foreach (var t in DateTime.Parse(time.ToString()).GetType().GetProperties()) {
    Console.WriteLine($"{t.Name}:{t.GetValue(time)}");
}
Date:2022/8/10 0:00:00
Day:10
DayOfWeek:Wednesday
DayOfYear:222
Hour:21
Kind:Local
Millisecond:202
Minute:30
Month:8
Now:2022/8/10 21:30:33
Second:33
Ticks:637957638332029467
TimeOfDay:21:30:33.2029467
Today:2022/8/10 0:00:00
Year:2022
UtcNow:2022/8/10 13:30:33
  1. 运算
Console.WriteLine(time.AddDays(1));  
Console.WriteLine(time.AddHours(-1));

五、字符串

  1. 拼接、大小写、结尾、分割、替换、反转、插入、移除、填补空白、去除空白、截取、查找、比较
string[] strs = { "今天","天气","真好" };
Console.WriteLine(string.Concat(strs));
Console.WriteLine(string.Join("_",strs));
Console.WriteLine(string.Join("_", strs).Contains("好"));
Console.WriteLine(string.Concat(strs).ToLower());
Console.WriteLine(string.Concat(strs).EndsWith("好"));
Console.WriteLine(string.Join("_", strs).Split("_"));
Console.WriteLine(string.Join("_", strs).Replace("_",""));
var array = string.Join("_", strs).ToCharArray();
Array.Reverse(array);
Console.WriteLine(new string(array));
Console.WriteLine(string.Join("_", strs).Insert(3,"的"));
Console.WriteLine(string.Join("_", strs).Remove(4,2));
Console.WriteLine(string.Join("_", strs).PadLeft(20));
Console.WriteLine(string.Join("_", strs).Substring(5));
  1. @ 忽略转移字符

  2. 格式控制符
    在这里插入图片描述

  3. 编码 Encoding类
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值