首页 > 代码库 > .Net基础加强06
.Net基础加强06
弱引用
集合 Hashtable Dictionary<K,V>
ArrayList List<T>
class Program { static void Main(string[] args) { //ArrayList list = new ArrayList() //{ //new Person(){Name="科比",Age=35}, //new Person(){Name="詹姆斯",Age=30}, //new Person(){Name="叶长种",Age=25}, //}; //list.Sort(); //for (int i = 0; i < list.Count; i++) //{ // Person p= list[i] as Person; // Console.WriteLine(p.Name); //} //Console.ReadKey(); int[] nums = {3,5,2,6,9,1 }; for (int i = 0; i < nums.Length; i++) { for (int j = nums.Length-1; j > i; j--) { //if (nums[j].CompareTo(nums[j-1])<0) //{ // int n = nums[j]; // nums[j]=nums[j-1]; // nums[j - 1] = n; //} IComparable com =(IComparable)nums[j]; if (com.CompareTo(nums[j - 1]) < 0) { int n = nums[j]; nums[j] = nums[j - 1]; nums[j - 1] = n; } } } for (int i = 0; i < nums.Length; i++) { Console.WriteLine(nums[i]); } Console.ReadKey(); } } public class Person:IComparable { public string Name { get; set; } public string Email { get; set; } public int Age { get; set; } public int CompareTo(object obj) { Person p = obj as Person; return this.Age - p.Age; } }
装箱和拆箱
值类型直接转为为引用类型为装箱;引用类型直接转换为值类型为拆箱。
class Program { static void Main(string[] args) { //int n = 10; //string s = n.ToString();//不是装箱 //int m = int.Parse(s);//不是拆箱 //object o = n;//装箱 //int x = (int)o;//拆箱 //Console.WriteLine(m); //在IL语言中出现 box unbox int n = 10; object o = n; int m = (int)o; Console.WriteLine(m); Console.ReadKey(); } }
class Program { static void Main(string[] args) { //Chinese ch1 = new Chinese(); //Person p = ch1;//没有发生装箱,因为都是引用类型 //Chinese ch2 =(Chinese)p; //double d = 999.9; //object o = d; //double d1 = (double)o;//装箱 //Console.WriteLine(d1);//拆箱,输出999.9 //Console.ReadKey(); //int d = 999; //object o = d; //double d1 = (double)o;//装箱 //Console.WriteLine(d1);//发生异常,装箱时整型拆箱也必须是整型 //Console.ReadKey(); //int n = 10; //IComparable com = n;//发生了装箱 接口都是引用类型 //int m = (int)com; //Console.WriteLine(m); //Console.ReadKey(); int n = 10; Console.WriteLine(n); object o = n; //装箱 Console.WriteLine((int)o);//拆箱 Console.WriteLine(o); Console.ReadKey(); } } public class Person { public string Name { get; set; } public string Email { get; set; } public int Age { get; set; } } public class Chinese : Person { }
class Program { static void Main(string[] args) { int n = 100; M1(n); //调用void M1(double d) 没有去装箱 string s1 = "a"; double d1 = 10; string s2 = "b"; int n1 = 9; string full = s1 + d1 + s2 + n1;// 装箱两次,d1 和 n1 Console.WriteLine(full); Console.ReadKey(); } private static void M1(double d) { Console.WriteLine(d); } private static void M1(object o) { Console.WriteLine(o); } }
class Program { static void Main(string[] args) { int n = 10; object o = n;//装箱一次 n = 100; Console.WriteLine(n+","+(int)o); //装了两次 拆了一次 Console.ReadKey(); } }
class Program { static void Main(string[] args) { //ArrayList arriList = new ArrayList(); //Stopwatch watch = new Stopwatch(); //watch.Start(); //for (int i = 0; i < 1000000; i++) //{ // arriList.Add(i); //} //watch.Stop(); //Console.WriteLine(watch.Elapsed);//00:00:00.99816 //Console.ReadKey(); List<int> arriList = new List<int>(); Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 1000000; i++) { arriList.Add(i); } watch.Stop(); Console.WriteLine(watch.Elapsed);//00:00:00.014241 Console.ReadKey(); } }
var类型推断:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。