垃圾回收(先了解以后再研究)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ////FileStream fs; ////fs.Dispose( //Person p1; #region 弱引用 //引用的意思就是:当一个对象没有变量引用的时候,这时就可以被垃圾回收了。 //但是接下来如果要是再想用该对象的话也是不可能的,因为即便还没有垃圾回收,但是这个对象已经无法在引用到了(虽然堆内存中,但是找不到该对象了。) //这时,当一个对象没有任何变量引用时,我们可以使用一个WeakReference(弱引用)来保持对这个对象的“引用”,这时虽然对该对象“弱引用”了, //但是这个对象仍然是可以被垃圾回收的,如果在下次使用这个对象的时候,该对象还没有被垃圾回收,则可以通过弱引用直接找到该对象,然后继续使用,如果已经被垃圾回收了,则需要重新创建这个对象。 //Person p = new Person(); //p.Age = 25; //p.Name = "叶长种"; //p.Email = "[email protected]"; ////p.Email... ////Person tmp = p; ////p = null; ////GC.Collect(); ////p = new Person(); ////p.Name = "叶长种"; ////p.Age = 25; ////p.Email = "[email protected]"; #endregion #region 通过代码来实现弱引用 Person p = new Person(); p.Name = "叶长种"; p.Age = 25; p.Email = "[email protected]"; //弱引用 WeakReference wkReference = new WeakReference(p); p = null; GC.Collect(); //if (wkReference.Target != null) //{ // object o = wkReference.Target; //} //假设某个时间段的时候又要使用这个对象了。 object o = wkReference.Target; if (o != null && wkReference.IsAlive) { Person p1 = o as Person; Console.WriteLine("{0} {1} {2}", p1.Name, p1.Age, p1.Email); } else { Console.WriteLine("重新创建对象吧,已经被垃圾回收了。"); } Console.ReadKey(); #endregion } } public class Person : IDisposable { public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } public void Say() { } #region IDisposable 成员 public void Dispose() { //编写代码,调用api, } #endregion } }
集合
using System.Collections;(非泛型集合)
using System.Collections.Generic;(泛型集合)
“类似数组”集合:ArrayList,List<>
“键值对”集合(“哈希表”集合):Hashtable,Dictionary<K,V>
“堆栈”集合:Stack,Stack<T>(LIFO)
“队列”集合:Queue,Queue<T>(LIFO)
“可排序键值对”集合
Set集合
Dictionary集合
“双向链表”集合
class Program { static void Main(string[] args) { #region ArrayList集合 //ArrayList arrList = new ArrayList(); //arrList.Add(10); //arrList.Add("10"); //arrList.Add(new Person() { Name = "叶长种" }); //arrList.Add(99.9); //arrList.Add(new int[] { 1, 2, 3, 4, 5 }); //arrList.AddRange(new string[] { "叶长种", "詹姆斯", "科比" }); ////arrList.RemoveAt(2);//删除索引为2的集合 //for (int i = 0; i < arrList.Count; i++) //{ // arrList.RemoveAt(i); //} //结果为 4 8 ////arrList.Clear(); //清空集合 //Console.WriteLine(arrList.Count); //Console.WriteLine(arrList.Capacity); //Console.ReadKey(); //string str = "叶长种"; //int n = 0; //try //{ // for (int i = 0; i < 30; i++) // { // n = i; // str += str; // } //} //catch //{ // Console.WriteLine(n); // Console.WriteLine(str.Length); //} //Console.WriteLine(str); //Console.ReadKey(); ////Array //int[] arr = new int[] { 1, 2, 3 }; //Console.WriteLine(arr.GetType().ToString()); //Console.WriteLine(arr.GetType().BaseType.ToString()); //Console.WriteLine(arr.GetType().BaseType.BaseType.ToString()); //Console.ReadKey(); //ArrayList arrList = new ArrayList(); #region MyRegion //arrList.Add(10); //arrList.Add("叶长种"); //Person p1 = new Person(); //p1.Name = "詹姆斯"; //Person p2 = new Person(); //p2.Name = "科比"; //arrList.Add(p1); //arrList.Add(p2); //arrList.Remove(10); //arrList.Remove(p2); //Console.WriteLine(arrList.Count); //Console.WriteLine(arrList.Capacity); //Console.ReadKey(); #endregion //Person p1 = new Person(); //p1.Name = "yzk"; //p1.Age = 18; //p1.Email = "[email protected]"; //Person p2 = new Person(); //p2.Name = "yzk"; //p2.Age = 18; //p2.Email = "[email protected]"; //string s1 = new string(new char[] { ‘a‘, ‘b‘, ‘c‘ }); //arrList.Add(p1); //arrList.Add(p2); //arrList.Add(s1); //arrList.Remove(p2); //arrList.Remove("abc");//这样也会把s1删掉 ////arrList.ToArray() //把集合变成数组 //Console.WriteLine(arrList.Count); //Console.ReadKey(); #endregion #region 集合中的Sort方法 //ArrayList arrList = new ArrayList(); #region ////arrList.Add(9); ////arrList.Add(19); ////arrList.Add(3); ////arrList.Add(7); ////arrList.Add(5); ////arrList.Sort(); //arrList.Add("sk"); //arrList.Add("jk"); //arrList.Add("yzk"); //arrList.Add("nll"); //arrList.Add("yhb"); //arrList.Add("ml"); //arrList.Sort();//升序排序 //for (int i = 0; i < arrList.Count; i++) //{ // Console.WriteLine(arrList[i]); //} //Console.ReadKey(); #endregion ArrayList arrList = new ArrayList(); Person sk = new Person() { Name = "sk", Age = 18, Email = "[email protected]" }; Person jk = new Person() { Name = "jk", Age = 17, Email = "[email protected]" }; Person yzk = new Person() { Name = "yzk", Age = 16, Email = "[email protected]" }; Person yhb = new Person() { Name = "yhb", Age = 15, Email = "[email protected]" }; arrList.Add(sk); arrList.Add(jk); arrList.Add(yzk); arrList.Add(yhb); //Sort()内部只支持升序,除非自己写CompareTo方法 arrList.Sort(); //[a,b,c,d] //a.CompareTo(b) //或者另外一个解决方案是,先升序排序再调用Reverse()反转数组。来实现降序的功能。 //arrList.Reverse(); for (int i = 0; i < arrList.Count; i++) { Person p = arrList[i] as Person; Console.WriteLine(p.Name); } Console.ReadKey(); #endregion } } public class Person : IComparable { public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } #region IComparable 成员 public int CompareTo(object obj) { Person p = obj as Person; return this.Age - p.Age; } #endregion }
集合排序ICompare接口使用
class Program { static void Main(string[] args) { ArrayList arrList = new ArrayList(); Person sk = new Person() { Name = "skk", Age = 18, Email = "[email protected]" }; Person jk = new Person() { Name = "jk", Age = 17, Email = "[email protected]" }; Person yzk = new Person() { Name = "yzkk", Age = 16, Email = "[email protected]" }; Person yhb = new Person() { Name = "yhbbbbbb", Age = 15, Email = "[email protected]" }; arrList.Add(sk); arrList.Add(jk); arrList.Add(yzk); arrList.Add(yhb); //arrList.Sort(new SortByAgeAsc()); arrList.Sort(new SortByNameAsc()); for (int i = 0; i < arrList.Count; i++) { Console.WriteLine(((Person)arrList[i]).Name); } Console.ReadKey(); //1.按照年龄降序排序 //2.按照姓名升序排序 } } public class SortByAgeAsc : IComparer { #region IComparer 成员 public int Compare(object x, object y) { Person px = x as Person; Person py = y as Person; return px.Age - py.Age; } #endregion } public class SortByNameAsc : IComparer { #region IComparer 成员 public int Compare(object x, object y) { Person px = x as Person; Person py = y as Person; return px.Name.Length - py.Name.Length; } #endregion } public class Person { public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } }
.Net基础加强05
时间: 2024-11-09 00:56:40