C# 基础

1 Files 文件

  • 创建文件夹

     Directory.CreateDirectory("D:\\code");
    
  • 创建文件

      FileStream fs = File.Create("D:\\code\\test.txt");
    
  • 获取文件信息

    文件创建时间: File.GetCreationTime(path)
    文件最后被写入时间:File.GetLastWriteTime(path)
    文件最后被访问时间:File.GetLastAccessTime(path)	
    文件内容:File.ReadAllText(path, Encoding.Default)
    
  • 关闭文件流

     fs.Close();
    
  • 判断文件是否存在

    File.Exists(path)
    
  • 删除文件

    File.Delete(path);
    
  • 移动文件

    File.Move(path, newPath);
    
  • 写入文件内容

      // 方法1
    	File.WriteAllText(path,"6666");
    	
    	// 方法2
        string str = "kkkkk"; 
     	byte[] a = Encoding.Default.GetBytes(str); 
    	File.WriteAllBytes(path,a); 
    
        // 方法3  会跨行
     string[] a  = { "1","2","3","4","5"};
    File.WriteAllLines(path,a);
    
    // 方法4 写在文件尾
    File.AppendAllText(path, "6666");
    

2 String 字符串

  • 比较字符串:String.Compare(str1, str2)
  • 查找字符串:Contains()
  • 转小写:ToLower()
  • 转大写:ToUpper()
  • 清楚前后空格:Trim
  • 分割:Split(‘’)
  • 替换:Replace
  • 移除:Remove
  • 最后一次出现的下标:LastIndexOf
  • 连接:Join,Concat
  • 判断空或者Null:IsNullOrEmpty
  • 在指定位置插入:Insert
  • 字符第一次出现的下标:IndexOfAny
  • 字符第一次出现的下标:IndexOf
  • 格式化:Format
  • 判断值是否一致:Equals
  • 复制指定位置内容:CopyTo
  • 复制:Copy

3 Dates 日期

  • 今天:DateTime.Now

  • 昨天:DateTime.Now.AddDays(-1)

  • 明天:DateTime.Now.AddDays(1)

  • 本周:

    开头:DateTime.Now.AddDays(Convert.ToDouble((0 -
    Convert.ToInt16(DateTime.Now.DayOfWeek))))

    结尾:DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))))

  • 星期几:DateTime.Now.DayOfWeek()

  • 取日期:DateTime.Now.ToShortDateString()

  • 上个月:DateTime.Now.AddMonths(-1)

  • 去年:DateTime.Now.AddYears(-1)

  • 年:DateTime.Now.Year

  • 月:DateTime.Now.Month

  • 日:DateTime.Now.Day

  • 时:DateTime.Now.Hour

  • 分:DateTime.Now.Minute

  • 秒:DateTime.Now.Second

  • 转换格式:DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss”);

  • 公历转阴历:

    DateTime本身依赖于日历Calendar类。Calendar是一个抽象类,在System.Globalization命名空间下,也在System.Runtime.dll中。而在Calendar类下面,提供了很多不同类型的日历。跟我们有关系的,是中国的阴历ChineseLunisolarCalendar。
    DateTime date = DateTime.Now;
     
    Calendar calendar = new ChineseLunisolarCalendar();
     
    int year = calendar.GetYear(date);
    /* 2020 */
    int month = calendar.GetMonth(date);
    /* 6 */
    int day = calendar.GetDayOfMonth(date);
    /* 24 */
    
  • 示例

    1.获取日期和时间
    
    DateTime.Now.ToString();            // 2019-09-4 20:02:10
    DateTime.Now.ToLocalTime().ToString();        // 2019-9-4 20:12:12
    
    2.获取日期
    
    DateTime.Now.ToLongDateString().ToString();    // 2019年9月4日
    DateTime.Now.ToShortDateString().ToString();    // 2019-9-4
    DateTime.Now.ToString("yyyy-MM-dd");        // 2019-09-04
    DateTime.Now.Date.ToString();            // 2019-9-4 0:00:00
    
    3.获取时间
    
    DateTime.Now.ToLongTimeString().ToString();   // 20:16:16
    DateTime.Now.ToShortTimeString().ToString();   // 20:16
    DateTime.Now.ToString("hh:mm:ss");        // 08:05:57
    DateTime.Now.TimeOfDay.ToString();        // 20:33:50.7187500
    
    4.其他
    
    DateTime.ToFileTime().ToString();       // 128650040212500000
    DateTime.Now.ToFileTimeUtc().ToString();   // 128650040772968750
    DateTime.Now.ToOADate().ToString();       // 39695.8461709606
    DateTime.Now.ToUniversalTime().ToString();   // 2019-9-4 12:19:14
    
    DateTime.Now.Year.ToString();         获取年份  // 2019
    DateTime.Now.Month.ToString();      获取月份   // 9
    DateTime.Now.DayOfWeek.ToString(); 获取星期   // Thursday
    DateTime.Now.DayOfYear.ToString(); 获取第几天   // 248
    DateTime.Now.Hour.ToString();          获取小时   // 20
    DateTime.Now.Minute.ToString();     获取分钟   // 31
    DateTime.Now.Second.ToString();     获取秒数   // 45
    
    //n为一个数,可以数整数,也可以事小数
    dt.AddYears(n).ToString();   //时间加n年
    dt.AddDays(n).ToString();   //加n天
    dt.AddHours(n).ToString();   //加n小时
    dt.AddMonths(n).ToString();   //加n个月
    dt.AddSeconds(n).ToString();   //加n秒
    dt.AddMinutes(n).ToString();   //加n分
    

4 Arrays and List 数组与列表

集合与字典

5 Control Flow 控制流

  • if else
  • switch
  • for
  • while:通过while判断才执行
  • do…while:先执行一次do的内容,再进行while判断
  • foreach
  • break
  • continue
  • return
  • goto:不咋用,也没理解到使用和意思

6 Non-Primitive Types 扩展类型

  • 数组:数组
  • 元组:没理解到是什么东西,疑惑???
  • 对象:Object
  • string 类型

7 Primitive Types 基本数据类型

  • 值类型
    • 整型:sbyte(8位有符号整数),short(16),int(32),long(64),byte(8位无符号整数),ushort,uint,ulong
    • float,double:浮点类型
    • decimal 类型:表示更高精度浮点类型
    • bool 类型:(true,false)
    • char 类型:字符类型,单引号
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值