C#课堂笔记(.Net基础类库中的常用类)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _.Net基础类库中的常用类 { internal class Program { static void Main1(string[] args) { int[] arr = { 22, 4, 5 }; //Array.Clear(arr, 0, arr.Length);//清空数组 Array.Sort(arr);//升序快排 Array.ForEach(arr, a => Console.WriteLine(a));//输出数组 int index = Array.IndexOf(arr, 4);//返回元素在数组中的索引 Console.WriteLine(index); //CopyTo();复制 //Clone();克隆出来一个新数组 ps:浅拷贝:string指向地址不变 //String类 //string.Format();格式化 float maxHp = 100; float currentHp = 80; string str1 = string.Format("当前血量{0:p}", currentHp / maxHp); Console.WriteLine(str1); string s = string.Format("今年是{0:yyyy年MM月dd日},当前时间是{0:HH时mm分ss秒}", DateTime.Now); Console.WriteLine(s); } //字符串修改,分割 static void Main2(string[] args) { //插入 string str1 = "天气不错,心情很好"; string str2 = str1.Insert(2, "真"); Console.WriteLine(str1); //看似修改字符串的方法,都不会影响原来的字符串对象,而是创建新的字符串对象 string name1 = "张三"; string name2 = "张三"; //截取,分割,替换 string str3 = "ABC"; string str4 = str3.Replace("B", ""); Console.WriteLine(str4); StringBuilder sb = new StringBuilder(); sb.Append(str1); sb.Append(str2); sb.Append(str3); sb.Append(str4); string str5 = sb.ToString(); } //内置集合 泛型List static void Main3(string[] args) { //List <string/*字符类型*/> list=new List<string>(); List<int> list1 = new List<int>(); list1.Add(11); list1.Add(12); //访问元素 Console.WriteLine(list1[0]); //遍历 for (int i = 0; i < list1.Count; i++) { Console.WriteLine(list1[i]); } } static void Main4(string[] args) { //内置集合 泛型Dictionary Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("用户账号", "Admin"); dict.Add("用户密码", "123456"); dict.Add("是否记住密码", "false"); Console.WriteLine(dict["用户账号"]); } } }
To be continued......