标签:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 Angle a = new Angle(12,0,0); 7 Angle b = new Angle(24,0,0); 8 Coordinate c1 = new Coordinate(); 9 Coordinate c2 = new Coordinate(); 10 c1.Latitude = a; 11 c2.Latitude = a; 12 c1.Longitude = b; 13 c2.Longitude = b; 14 Console.WriteLine(c1 == c2); 15 Console.WriteLine(c1 != c2); 16 Console.ReadLine(); 17 18 } 19 } 20 struct Angle 21 { 22 23 public Angle(int hours, int minutes, int seconds) 24 { 25 _Hours = hours; 26 _Minutes = minutes; 27 _Seconds = seconds; 28 } 29 public int Hours 30 { 31 get 32 { 33 return _Hours; 34 } 35 } 36 private int _Hours; 37 public int Minutes 38 { 39 get 40 { 41 return _Minutes; 42 } 43 } 44 private int _Minutes; 45 public int Seconds 46 { 47 get 48 { 49 return _Seconds; 50 } 51 } 52 private int _Seconds; 53 public Angle Move(int hours, int minutes, int seconds) 54 { 55 return new Angle(Hours + hours, Minutes + minutes, Seconds + seconds); 56 } 57 } 58 59 class Coordinate 60 { 61 public Angle Latitude 62 { 63 get 64 { 65 return _Latitude; 66 } 67 set 68 { 69 _Latitude = value; 70 } 71 } 72 private Angle _Latitude; 73 74 public Angle Longitude 75 { 76 get 77 { 78 return _Longitude; 79 } 80 set 81 { 82 _Longitude = value; 83 } 84 } 85 private Angle _Longitude; 86 public static bool operator ==(Coordinate leftHandSide, Coordinate rightHandSide) 87 { 88 if (ReferenceEquals(leftHandSide, null)) 89 { 90 return ReferenceEquals(rightHandSide, null); 91 } 92 return (leftHandSide.Equals(rightHandSide)); 93 } 94 public static bool operator !=(Coordinate leftHandSide, Coordinate rightHandSide) 95 { 96 return !(leftHandSide == rightHandSide); 97 98 } 99 //重写 100 public override bool Equals(object obj) 101 { 102 if (obj == null) 103 { 104 return false; 105 } 106 //检查类型是否相同 107 if (this.GetType() != obj.GetType()) 108 { 109 return false; 110 } 111 return Equals((Coordinate)obj); 112 } 113 //重载 114 public bool Equals(Coordinate obj) 115 { 116 if (ReferenceEquals(obj, null)) 117 { 118 return false; 119 } 120 //如果引用相同,肯定为true 121 if (ReferenceEquals(this, obj)) 122 { 123 return true; 124 } 125 //如果散列码值不相同,肯定不同。散列码本身就是根据值生成的整形值 126 if (this.GetHashCode() != obj.GetHashCode()) 127 { 128 return false; 129 } 130 //检查基类是否有自定义的Equals() 131 //System.Diagnostics.Debug.Assert(base.GetType() != typeof(object)); 132 //if (!base.Equals(obj)) 133 //{ 134 // return false; 135 //} 136 //最后,写上自定义的Equals 计算方法 137 return Longitude.Equals(obj.Longitude) && (Latitude.Equals(obj.Latitude)); 138 } 139 //重写 140 public override int GetHashCode() 141 { 142 int hashCode = Longitude.GetHashCode(); 143 hashCode ^= Latitude.GetHashCode();//使用自定义的计算方法(本处常用异或 ) 144 return hashCode; 145 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 Angle a = new Angle(8, 20, 20); 7 Angle b = new Angle(6, 15, 15); 8 Coordinate c1 = new Coordinate(); 9 Coordinate c2 = new Coordinate(); 10 c1.Latitude = a; 11 c2.Latitude = a; 12 c1.Longitude = b; 13 c2.Longitude = b; 14 Console.WriteLine(c1 == c2); 15 Console.WriteLine(c1 != c2); 16 Console.WriteLine((c1 + c2).ToString()); 17 Console.ReadLine(); 18 19 } 20 } 21 struct Angle 22 { 23 24 public Angle(int hours, int minutes, int seconds) 25 { 26 _Hours = hours; 27 _Minutes = minutes; 28 _Seconds = seconds; 29 } 30 public int Hours 31 { 32 get 33 { 34 return _Hours; 35 } 36 } 37 private int _Hours; 38 public int Minutes 39 { 40 get 41 { 42 return _Minutes; 43 } 44 } 45 private int _Minutes; 46 public int Seconds 47 { 48 get 49 { 50 return _Seconds; 51 } 52 } 53 private int _Seconds; 54 public Angle Move(int hours, int minutes, int seconds) 55 { 56 return new Angle(Hours + hours, Minutes + minutes, Seconds + seconds); 57 } 58 } 59 60 class Coordinate 61 { 62 public Angle Latitude 63 { 64 get 65 { 66 return _Latitude; 67 } 68 set 69 { 70 _Latitude = value; 71 } 72 } 73 private Angle _Latitude; 74 75 public Angle Longitude 76 { 77 get 78 { 79 return _Longitude; 80 } 81 set 82 { 83 _Longitude = value; 84 } 85 } 86 private Angle _Longitude; 87 88 public static bool operator ==(Coordinate leftHandSide, Coordinate rightHandSide) 89 { 90 if (ReferenceEquals(leftHandSide, null)) 91 { 92 return ReferenceEquals(rightHandSide, null); 93 } 94 return (leftHandSide.Equals(rightHandSide)); 95 } 96 public static bool operator !=(Coordinate leftHandSide, Coordinate rightHandSide) 97 { 98 return !(leftHandSide == rightHandSide); 99 100 } 101 public static Coordinate operator +(Coordinate leftHandSide, Coordinate rightHandSide) 102 { 103 Coordinate a = new Coordinate(); 104 a.Latitude = new Angle( 105 leftHandSide.Latitude.Hours + rightHandSide.Latitude.Hours, 106 leftHandSide.Latitude.Minutes + rightHandSide.Latitude.Minutes, 107 leftHandSide.Latitude.Seconds + rightHandSide.Latitude.Seconds 108 ); 109 a.Longitude = new Angle( 110 leftHandSide.Longitude.Hours + rightHandSide.Longitude.Hours, 111 leftHandSide.Longitude.Minutes + rightHandSide.Longitude.Minutes, 112 leftHandSide.Longitude.Seconds + rightHandSide.Longitude.Seconds 113 ); 114 return a; 115 } 116 //重写 117 public override bool Equals(object obj) 118 { 119 if (obj == null) 120 { 121 return false; 122 } 123 //检查类型是否相同 124 if (this.GetType() != obj.GetType()) 125 { 126 return false; 127 } 128 return Equals((Coordinate)obj); 129 } 130 //重载 131 public bool Equals(Coordinate obj) 132 { 133 if (ReferenceEquals(obj, null)) 134 { 135 return false; 136 } 137 //如果引用相同,肯定为true 138 if (ReferenceEquals(this, obj)) 139 { 140 return true; 141 } 142 //如果散列码值不相同,肯定不同。散列码本身就是根据值生成的整形值 143 if (this.GetHashCode() != obj.GetHashCode()) 144 { 145 return false; 146 } 147 //检查基类是否有自定义的Equals() 148 //System.Diagnostics.Debug.Assert(base.GetType() != typeof(object)); 149 //if (!base.Equals(obj)) 150 //{ 151 // return false; 152 //} 153 //最后,写上自定义的Equals 计算方法 154 return Longitude.Equals(obj.Longitude) && (Latitude.Equals(obj.Latitude)); 155 } 156 //重写 157 public override int GetHashCode() 158 { 159 int hashCode = Longitude.GetHashCode(); 160 hashCode ^= Latitude.GetHashCode();//使用自定义的计算方法(本处常用异或 ) 161 return hashCode; 162 } 163 //重写 164 public override string ToString() 165 { 166 string str = ""; 167 str = "Latitude " + ((Latitude.Hours.ToString().Length == 2) ? Latitude.Hours.ToString() : "0" + Latitude.Hours.ToString()) + ":" 168 + ((Latitude.Minutes.ToString().Length == 2) ? Latitude.Minutes.ToString() : "0" + Latitude.Minutes.ToString()) + ":" 169 + ((Latitude.Hours.ToString().Length == 2) ? Latitude.Seconds.ToString() : "0" + Latitude.Seconds.ToString()); 170 str += "\n"; 171 str += "Longitude " + ((Longitude.Hours.ToString().Length == 2) ? Longitude.Hours.ToString() : "0" + Longitude.Hours.ToString()) + ":" 172 + ((Longitude.Minutes.ToString().Length == 2) ? Longitude.Minutes.ToString() : "0" + Longitude.Minutes.ToString()) + ":" 173 + ((Longitude.Hours.ToString().Length == 2) ? Longitude.Seconds.ToString() : "0" + Longitude.Seconds.ToString()); 174 return str; 175 } 176 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 Angle a = new Angle(8, 20, 20); 7 Angle b = new Angle(6, 15, 15); 8 Coordinate c1 = new Coordinate(); 9 c1.Latitude = -a; 10 c1.Longitude = -b; 11 12 Console.WriteLine(c1 ); 13 14 Console.ReadLine(); 15 16 } 17 } 18 struct Angle 19 { 20 21 public Angle(int hours, int minutes, int seconds) 22 { 23 _Hours = hours; 24 _Minutes = minutes; 25 _Seconds = seconds; 26 } 27 public int Hours 28 { 29 get 30 { 31 return _Hours; 32 } 33 } 34 private int _Hours; 35 public int Minutes 36 { 37 get 38 { 39 return _Minutes; 40 } 41 } 42 private int _Minutes; 43 public int Seconds 44 { 45 get 46 { 47 return _Seconds; 48 } 49 } 50 private int _Seconds; 51 public Angle Move(int hours, int minutes, int seconds) 52 { 53 return new Angle(Hours + hours, Minutes + minutes, Seconds + seconds); 54 } 55 //一元运算符重载 56 public static Angle operator -(Angle a) 57 { 58 Angle temp = new Angle(); 59 temp._Hours = 24-a.Hours; 60 temp._Minutes = 60-a.Minutes; 61 temp._Seconds =60- a.Seconds; 62 return temp; 63 } 64 } 65 66 class Coordinate 67 { 68 public Angle Latitude 69 { 70 get 71 { 72 return _Latitude; 73 } 74 set 75 { 76 _Latitude = value; 77 } 78 } 79 private Angle _Latitude; 80 81 public Angle Longitude 82 { 83 get 84 { 85 return _Longitude; 86 } 87 set 88 { 89 _Longitude = value; 90 } 91 } 92 private Angle _Longitude; 93 94 //二元运算符重载 95 public static bool operator ==(Coordinate leftHandSide, Coordinate rightHandSide) 96 { 97 if (ReferenceEquals(leftHandSide, null)) 98 { 99 return ReferenceEquals(rightHandSide, null); 100 } 101 return (leftHandSide.Equals(rightHandSide)); 102 } 103 public static bool operator !=(Coordinate leftHandSide, Coordinate rightHandSide) 104 { 105 return !(leftHandSide == rightHandSide); 106 107 } 108 public static Coordinate operator +(Coordinate leftHandSide, Coordinate rightHandSide) 109 { 110 Coordinate a = new Coordinate(); 111 a.Latitude = new Angle( 112 leftHandSide.Latitude.Hours + rightHandSide.Latitude.Hours, 113 leftHandSide.Latitude.Minutes + rightHandSide.Latitude.Minutes, 114 leftHandSide.Latitude.Seconds + rightHandSide.Latitude.Seconds 115 ); 116 a.Longitude = new Angle( 117 leftHandSide.Longitude.Hours + rightHandSide.Longitude.Hours, 118 leftHandSide.Longitude.Minutes + rightHandSide.Longitude.Minutes, 119 leftHandSide.Longitude.Seconds + rightHandSide.Longitude.Seconds 120 ); 121 return a; 122 } 123 124 //Object成员方法重写 125 //重写 126 public override bool Equals(object obj) 127 { 128 if (obj == null) 129 { 130 return false; 131 } 132 //检查类型是否相同 133 if (this.GetType() != obj.GetType()) 134 { 135 return false; 136 } 137 return Equals((Coordinate)obj); 138 } 139 140 public override int GetHashCode() 141 { 142 int hashCode = Longitude.GetHashCode(); 143 hashCode ^= Latitude.GetHashCode();//使用自定义的计算方法(本处常用异或 ) 144 return hashCode; 145 } 146 147 public override string ToString() 148 { 149 string str = ""; 150 str = "Latitude " + ((Latitude.Hours.ToString().Length == 2) ? Latitude.Hours.ToString() : "0" + Latitude.Hours.ToString()) + ":" 151 + ((Latitude.Minutes.ToString().Length == 2) ? Latitude.Minutes.ToString() : "0" + Latitude.Minutes.ToString()) + ":" 152 + ((Latitude.Hours.ToString().Length == 2) ? Latitude.Seconds.ToString() : "0" + Latitude.Seconds.ToString()); 153 str += "\n"; 154 str += "Longitude " + ((Longitude.Hours.ToString().Length == 2) ? Longitude.Hours.ToString() : "0" + Longitude.Hours.ToString()) + ":" 155 + ((Longitude.Minutes.ToString().Length == 2) ? Longitude.Minutes.ToString() : "0" + Longitude.Minutes.ToString()) + ":" 156 + ((Longitude.Hours.ToString().Length == 2) ? Longitude.Seconds.ToString() : "0" + Longitude.Seconds.ToString()); 157 return str; 158 } 159 //重载 160 public bool Equals(Coordinate obj) 161 { 162 if (ReferenceEquals(obj, null)) 163 { 164 return false; 165 } 166 //如果引用相同,肯定为true 167 if (ReferenceEquals(this, obj)) 168 { 169 return true; 170 } 171 //如果散列码值不相同,肯定不同。散列码本身就是根据值生成的整形值 172 if (this.GetHashCode() != obj.GetHashCode()) 173 { 174 return false; 175 } 176 //检查基类是否有自定义的Equals() 177 //System.Diagnostics.Debug.Assert(base.GetType() != typeof(object)); 178 //if (!base.Equals(obj)) 179 //{ 180 // return false; 181 //} 182 //最后,写上自定义的Equals 计算方法 183 return Longitude.Equals(obj.Longitude) && (Latitude.Equals(obj.Latitude)); 184 } 185 }
1 if (c1) 2 { 3 Console.WriteLine(c1); 4 } 5 public static bool operator false(Coordinate c) 6 { 7 if (c.Latitude.Hours < 12) 8 { 9 10 return false; 11 } 12 else 13 { 14 return true; 15 } 16 } 17 public static bool operator true(Coordinate c) 18 { 19 if (c.Latitude.Hours < 12) 20 { 21 return false; 22 } 23 else 24 { 25 return true; 26 } 27 }
1 public static implicit operator double(Coordinate c) 2 { 3 return (double)c.Latitude.Hours; 4 } 5 public static implicit operator Coordinate(double hours) 6 { 7 Coordinate c = new Coordinate(); 8 c.Latitude = new Angle((int)hours, 0, 0); 9 return c; 10 }
implict 隐式 explicit显式
1 private WeakReference Data; 2 public FileStream GetData() 3 { 4 FileStream data = (FileStream)Data.Target; 5 if (data != null) 6 { 7 return data; 8 } 9 else 10 { 11 //创建新的文件流 12 return data; 13 } 14 }
1 public class TemporaryFileStream 2 { 3 private readonly FileStream _Stream; 4 public FileStream Stream 5 { 6 get { return _Stream; } 7 } 8 private FileInfo _File = new FileInfo(Path.GetTempFileName()); 9 public FileInfo File 10 { 11 get { return _File; } 12 } 13 14 public TemporaryFileStream() 15 { 16 _File = new FileInfo(Path.GetTempFileName()); 17 _Stream = new FileStream(File.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite); 18 } 19 ~TemporaryFileStream() 20 { 21 Close(); 22 } 23 public void Close() 24 { 25 if (Stream != null) 26 { 27 Stream.Close(); 28 } 29 if (File != null) 30 { 31 File.Delete(); 32 } 33 } 34 35 }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 TemporaryFileStream fileStream = new TemporaryFileStream(); 6 //Use temporary file stream; 7 8 //.. 9 10 fileStream.Dispose(); 11 12 //.. 13 14 15 16 } 17 } 18 19 public class TemporaryFileStream : IDisposable 20 { 21 private readonly FileStream _Stream; 22 public FileStream Stream 23 { 24 get { return _Stream; } 25 } 26 private FileInfo _File = new FileInfo(Path.GetTempFileName()); 27 public FileInfo File 28 { 29 get { return _File; } 30 } 31 32 public TemporaryFileStream() 33 { 34 _File = new FileInfo(Path.GetTempFileName()); 35 _Stream = new FileStream(File.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite); 36 } 37 ~TemporaryFileStream() 38 { 39 Close(); 40 } 41 public void Close() 42 { 43 if (Stream != null) 44 { 45 Stream.Close(); 46 } 47 if (File != null) 48 { 49 File.Delete(); 50 } 51 // Turn off calling the finalizer 52 System.GC.SuppressFinalize(this); 53 } 54 public void Dispose() 55 { 56 Close(); 57 } 58 59 }
1 static void Search() 2 { 3 using (TemporaryFileStream fileStream1 = new TemporaryFileStream(), fileStream2 = new TemporaryFileStream()) 4 { 5 //出了这个范围,会自动被释放 6 } 7 }
1 class DataCache 2 { 3 private TemporaryFileStream _FileStream = null; 4 public TemporaryFileStream FileStream 5 { 6 get 7 { 8 if (_FileStream == null) 9 { 10 _FileStream = new TemporaryFileStream(); 11 } 12 return _FileStream; 13 } 14 } 15 16 }
1 class DataCache 2 { 3 4 //为泛型和lambda表达式使用推迟加载(C#4.0 CLR添加了一个新类来帮助进行推迟初始化:System.Lazy<T> 5 private Lazy<TemporaryFileStream> _FileStream = null; 6 7 public DataCache() 8 { 9 _FileStream = new Lazy<TemporaryFileStream>(()=> new TemporaryFileStream() ); 10 } 11 public TemporaryFileStream FileStream 12 { 13 get 14 { 15 return _FileStream.Value; 16 } 17 } 18 19 }
标签:
原文地址:http://www.cnblogs.com/tlxxm/p/4604538.html