1、索引器
namespace _索引器 { class Program { static void Main(string[] args) { Person p = new Person(); Console.WriteLine(p[2]); Console.ReadKey(); } } public class Person { public string[] names = { "F1","F2","F3","F4"}; public string this[int index] { get { string str = ""; switch (index) { case 0: str = names[0]; break; case 1: str = names[1]; break; case 2: str = names[2]; break; } return str; } } } }
2、ref和out
1 namespace _09ref和out 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 8 //ref 可进可出,要赋初值 9 // out 只出 不进,可以没有初值 10 int number=100; 11 12 13 Show(out number); 14 Console.WriteLine(number); 15 Console.ReadKey(); 16 } 17 18 public static void Show(out int num) 19 { 20 num = 1000;//赋值 21 } 22 23 } 24 }
3、大量字符串拼接
1 namespace _StringBuilder 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 8 StringBuilder sb = new StringBuilder(); 9 10 sb.Append("<html>"); 11 sb.Append("<head><title>这是我的html页面</title></head>"); 12 sb.Append("<body>"); 13 sb.Append("<font color=\"red\" size=\"7\" face=\"全新硬笔行书简\">一首诗。。</font>"); 14 sb.Append("</body>"); 15 sb.Append("</html>"); 16 //转成字符串类型 17 string str= sb.ToString(); 18 File.WriteAllText("1.html",str,Encoding.UTF8); 19 Console.WriteLine("输出个html"); 20 Console.ReadKey(); 21 22 23 24 } 25 } 26 }
4、泛型集合
1 namespace _分拣奇偶数 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 List<int> list = new List<int> {3,4,45,67,65,55,44,33 }; 8 List<int> listOdd = new List<int>();//奇数 9 List<int> listEven = new List<int>();//偶数 10 11 for (int i = 0; i < list.Count; i++) 12 { 13 if (list[i]%2==0) 14 { 15 listEven.Add(list[i]);//偶数 16 } 17 else 18 { 19 listOdd.Add(list[i]);//奇数 20 } 21 } 22 23 for (int i = 0; i < listEven.Count; i++) 24 { 25 Console.WriteLine(listEven[i]); 26 } 27 Console.WriteLine("==========================="); 28 for (int i = 0; i < listOdd.Count; i++) 29 { 30 Console.WriteLine(listOdd[i]); 31 } 32 Console.ReadKey(); 33 } 34 } 35 }
5、键值对
1 namespace _键值对 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 //键要唯一,不能重复 8 Dictionary<string, int> dic = new Dictionary<string, int>(); //键值对 9 10 dic.Add("卡卡西", 23); 11 dic.Add("乔峰",32); 12 dic.Add("宇智波斑",1000); 13 14 //遍历所有的键 15 foreach (string item in dic.Keys) 16 { 17 Console.WriteLine(item); 18 } 19 Console.WriteLine("=================="); 20 //遍历所有的值 21 foreach (int item in dic.Values) 22 { 23 Console.WriteLine(item); 24 } 25 Console.WriteLine("==========================="); 26 //遍历所有的键值 27 foreach (KeyValuePair<string,int> item in dic) 28 { 29 Console.WriteLine(item.Key+"==="+item.Value); 30 } 31 32 Console.ReadKey(); 33 34 } 35 } 36 }
练习1:大小写转换
1 string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零"; 2 //做一个字典 3 Dictionary<char, char> dic = new Dictionary<char, char>(); 4 string[] strNumber = str.Split(new char[] { ‘ ‘ }, StringSplitOptions.RemoveEmptyEntries); 5 for (int i = 0; i < strNumber.Length; i++) 6 { 7 if (!dic.ContainsKey(strNumber[i][0])) 8 { 9 dic.Add(strNumber[i][0], strNumber[i][1]); 10 } 11 } 12 Console.WriteLine("请输入数字"); 13 string txt = Console.ReadLine(); 14 for (int i = 0; i < txt.Length; i++) 15 { 16 if (dic.ContainsKey(txt[i]))//判断字典中是否包含用户输入的内容 17 { 18 Console.Write(dic[txt[i]]); 19 } 20 else 21 { 22 Console.Write(txt[i]); 23 } 24 } 25 Console.ReadKey();
练习2:计算字符串中每种字母出现的次数(面试题)。 “Welcome ,to Chinaworld”,不区分大小写,打印“W2”“e 2”“o 3”……
1 string str = "Welcome ,to Chinaworld"; 2 //全都转小写 3 str= str.ToLower(); 4 Dictionary<char, int> dic = new Dictionary<char, int>(); 5 for (int i = 0; i < str.Length; i++) 6 { 7 //判断当前的这个字符是不是字母 8 if (char.IsLetter(str[i])) 9 { 10 if (!dic.ContainsKey(str[i]))//判断有没有这个字母 11 { 12 dic.Add(str[i],1); 13 } 14 else 15 { 16 dic[str[i]]++; 17 } 18 } 19 } 20 21 22 foreach (KeyValuePair<char,int> item in dic) 23 { 24 Console.WriteLine("{0}字母出现了{1}次",item.Key,item.Value); 25 } 26 Console.ReadKey();
6、装箱拆箱
装箱和拆箱是损耗性能的,尽量少去装箱和拆箱
1 int num = 100;// 值类型 2 3 object obj = num;//值类型转引用类型 装箱---不准确的 4 5 string str = num.ToString();//不是装箱 6 7 8 int number =(int) obj; 9 10 // 有个直接或间接的继承关系 11 //值类型转引用类型 装箱---不准确的 12 //引用类型转值类型 拆箱 --- 不准确的 13 14 15 double d = 10; 16 17 double dou = (double)obj;//拆箱了 但是 拆箱失败了 18 19 Console.WriteLine(dou); 20 Console.ReadKey();
7、大文件移动
1 namespace _大文件移动 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 //读的流 8 using (FileStream fsRead=new FileStream(@"G:\视频\海盗.mkv",FileMode.Open, FileAccess.Read)) 9 { 10 //写的流 11 using (FileStream fsWrite=new FileStream(@"G:\电影\海盗.mkv", FileMode.Create, FileAccess.Write)) 12 { 13 //每次读取的大小是5M 14 byte[]buffer=new byte[1024*1024*5]; 15 //实际(真正读取到的大小) 16 int r= fsRead.Read(buffer, 0, buffer.Length); 17 while (r>0) 18 { 19 //写入 20 fsWrite.Write(buffer, 0, r); 21 Console.WriteLine("**"); 22 //再读取 23 r = fsRead.Read(buffer, 0, buffer.Length); 24 } 25 } 26 } 27 Console.WriteLine("ok了"); 28 Console.ReadKey(); 29 } 30 } 31 }
时间: 2024-11-02 23:38:00