• 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. 十进制转二进制
       public static void Dec2Bin(long num) {
    	     if (num > 0)
    	       {
    	           Dec2Bin(num >> 1);
    	           Console.Write(num & 0x01);
    	       }
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. ?的用法
    1. sendmsg?.invoke("123"); // sendmsg不为空则执行invork
    2. int?x = null;           // 声明这个值可以为null 且初始值为null
    3. int vel = x??5;         // 如果不为null,则返回x 否则返回5
    
    • 1
    • 2
    • 3

    二、类型转换

    1. 强转(略)
    2. Convert类
    // 十进制转二进制
     Console.Write(Convert.ToString(num,2));
    
    • 1
    • 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. ToString
     // 十进制转十六进制
     Console.Write(Convert.ToString(num, 16));
     Console.Write(num.ToString("x2"));
    
    • 1
    • 2
    • 3

    三、数学运算

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. Math 数学类 ,计算平方、绝对值、立方、拟合等
    Console.WriteLine(Math.Abs(-3)); ;
    
    • 1

    四、时间

    1. 星期
     Console.WriteLine(DateTime.Today.DayOfWeek);
    
    • 1
    1. 农历
    ChineseLunisolarCalendar chineseLunisolarCalendar = new ChineseLunisolarCalendar();
    Console.WriteLine($"{DateTime.Today}是农历{chineseLunisolarCalendar.GetYear(DateTime.Today)}{chineseLunisolarCalendar.GetMonth(DateTime.Today)}{chineseLunisolarCalendar.GetDayOfMonth(DateTime.Today)}日" );
    
    • 1
    • 2
    1. 时间
     Console.WriteLine($"一天总共有多少秒{new TimeSpan(24, 0, 0).TotalSeconds}");
    
    • 1
    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)}");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 运算
    Console.WriteLine(time.AddDays(1));  
    Console.WriteLine(time.AddHours(-1));
    
    • 1
    • 2

    五、字符串

    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
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1. @ 忽略转移字符

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

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

  • 相关阅读:
    PlantUML——类图(持续更新)
    Pikachu靶场——越权访问漏洞(over permission)
    【Unity3D日常开发】Unity3D工具之UnityForSVN
    PostGIS学习教程六:几何图形(geometry)
    java boot项目读取yml配置信息
    二、java版 SpringCloud分布式微服务云架构之Java 开发环境配置
    Java 变量初始化的两种方式和优缺点比较
    mysql 的语法(1)
    显示 frames per second
    vxe-table 表格尾部小计列项再合计展示
  • 原文地址:https://blog.csdn.net/LyRics1996/article/details/126255036