【筆記】《C#高效編程改進C#代碼的50個行之有效的辦法》第1章C#語言習慣(1)--屬性的特性以及索引器(SamWang)


**************************************************************************

書名:《C#高效編程改進C#代碼的50個行之有效的辦法》

**************************************************************************

 

第1章 C#語言習慣/1

--------------------------------------------------------------

建議1: 使用屬性而不是可訪問的數據成員/ 1

--------------------------------------------------------------

  該建議點主要介紹屬性的特性以及索引器的使用。

  使用建議:

      1.無論何時需要在類型的公有或保護接口中暴露數據,都應該使用屬性。

      2.你也應該使用索引器來暴露序列或字典。

      3.所有的數據成員都應該是私有的,沒有任何例外。

  本人測試代碼:  

   

  1 /******************************************************************
  2  * 創 建 人:  王申和
  3  * 創建時間:  2012-11-1 15:45
  4  * 描    述:
  5  *             測試屬性相關功能:屬性特性與索引器  
  6  * 環    境:  VS2010
  7 ******************************************************************/
  8 using System;
  9 using System.Collections.Generic;
 10 using System.Linq;
 11 using System.Text;
 12 using System.Threading;
 13 
 14 namespace _01_UseProperty
 15 {
 16     class Program
 17     {
 18         static void Main(string[] args)
 19         {
 20             try
 21             {
 22                 var c = new Customer<string>();
 23                 //c.Name = null;
 24                 c.Text = "測試無字段屬性";
 25                 Console.WriteLine(c.Text);
 26 
 27                 //測試索引器
 28                 c[30] = "測試一維序列索引器";
 29                 Console.WriteLine(c[30]);
 30                 Console.WriteLine(c[120]);
 31 
 32                 c["字典1"] = "測試字典索引器";
 33                 Console.WriteLine("內容:" + c["字典2"]);
 34                 Console.WriteLine("內容:" + c["字典1"]);
 35 
 36                 Name name = new Name("", "");
 37                 c[name] = "測試字典索引器:參數為類";
 38                 Console.WriteLine("內容:" + c[name]);
 39 
 40                 c[10, 20, 30] = "測試多維序列索引器";
 41                 Console.WriteLine("內容:" + c[10, 20, 30]);
 42 
 43                 c["", ""] = "測試多維字典索引器";
 44                 Console.WriteLine("內容:" + c["", ""]);
 45 
 46                 c[2, "李四"] = "四個人才";
 47                 Console.WriteLine("內容:" + c[2, "李四"]);
 48 
 49             }
 50             catch (ArgumentException ex)
 51             {
 52                 Console.WriteLine(ex.Message);
 53             }
 54             Console.ReadLine();
 55         }
 56     }
 57 
 58     public class Customer<T>
 59     {
 60         #region 屬性兩種寫法
 61         //盡量不要使用可訪問的數據成員,應使用屬性
 62         //public string text;
 63 
 64         //無字段屬性,等同於下面
 65         public string Text { get; set; }
 66 
 67         //有字段屬性
 68         //private string text;
 69         //public string text
 70         //{
 71         //    get { return text; }
 72         //    set { text = value; }
 73         //}
 74         #endregion
 75 
 76         #region 屬性為方法,可加異常處理
 77         private string name;
 78         public string Name
 79         {
 80             get { return name; }
 81             set
 82             {
 83                 if (string.IsNullOrEmpty(value))
 84                     throw new ArgumentException("Name cannot be blank", "Name");
 85                 name = value;
 86             }
 87         }
 88         #endregion
 89 
 90         #region 屬性支持interface、virtual、abstract(屬性擁有方法的所有語言特性)
 91         public interface INameValuePair<T>
 92         {
 93             string Name { get; }
 94             T Value { get; set; }
 95         }
 96 
 97         public virtual string vtest
 98         {
 99             get;
100             protected set;
101         }
102 
103         public abstract class AbsClass
104         {
105             public abstract string atest { get; set; }
106         }
107         #endregion
108 
109         #region 支持多線程:訪問器中增加鎖
110         private object Sync = new object();
111         private int count;
112         public int Count
113         {
114             get
115             {
116                 lock (Sync)
117                     return count;
118             }
119             set
120             {
121                 lock (Sync)
122                     count = value;
123             }
124         }
125         #endregion
126 
127         #region 屬性索引器
128         //1.索引器都使用this關鍵字聲明。C#不支持為索引器命名
129         //2.索引器是屬性的一種,它本質上和屬性一樣是方法。
130         //3.索引器可以重載,因此一個類中可以有多個索引器。
131 
132         //一維序列索引器,注意索引器范圍
133         string[] theValues = new string[100];
134         public string this[int index]
135         {
136             get
137             {
138                 if (index > 99)
139                     return default(string);
140                 return theValues[index];
141             }
142             set { theValues[index] = value; }
143         }
144 
145         //字典索引器
146         Dictionary<T, T> dic = new Dictionary<T, T>();
147         public T this[T key]
148         {
149             get
150             {
151                 if (!dic.ContainsKey(key))
152                     return default(T);
153                 return dic[key];
154             }
155             set { dic[key] = value; }
156         }
157 
158         //字典索引器:參數為類
159         Dictionary<Name, T> dicClass = new Dictionary<Name, T>();
160         public T this[Name key]
161         {
162             get
163             {
164                 if (!dicClass.ContainsKey(key))
165                     return default(T);
166                 return dicClass[key];
167             }
168             set { dicClass[key] = value; }
169         }
170 
171         //多維序列索引器
172         T[, ,] multArray = new T[100, 100, 100];
173         public T this[int x, int y, int z]
174         {
175             get { return multArray[x, y, z]; }
176             set { multArray[x, y, z] = value; }
177         }
178 
179         //多維字典索引:此處利用了結構體為值類型的特性。
180         struct PeopleName
181         {
182             public PeopleName(string fn, string ln)
183                 : this() //結構體的構造函數重載需要調用默認構造函數
184             {
185                 FirstName = fn;
186                 LastName = ln;
187             }
188             public string FirstName { get; set; }
189             public string LastName { get; set; }
190         }
191         Dictionary<PeopleName, T> dicMult = new Dictionary<PeopleName, T>();
192         public T this[string fn, string ln]
193         {
194             get
195             {
196                 var pn = new PeopleName(fn, ln);
197                 if (!dicMult.ContainsKey(pn))
198                     return default(T);
199                 return dicMult[pn];
200             }
201             set { dicMult[new PeopleName(fn, ln)] = value; }
202         }
203 
204         //多維字段索引:采用嵌套(嵌套有很多種,如數組嵌套字典、字典嵌套字典、甚至可以嵌套類等等)
205         Dictionary<T, T>[] dics = new Dictionary<T, T>[100];
206         public T this[int i, T key]
207         {
208             get
209             {
210                 if (i>99 || !dics[i].ContainsKey(key))
211                     return default(T);
212                 return dics[i][key];
213             }
214             set 
215             {
216                 if (dics[i] == null)
217                     dics[i] = new Dictionary<T, T>();
218                 dics[i][key] = value; 
219             }
220         }
221         #endregion
222     }
223 
224     public class Name
225     {
226         public Name(string fn, string ln)
227         {
228             FirstName = fn;
229             LastName = ln;
230         }
231         public string FirstName { get; set; }
232         public string LastName { get; set; }
233     }
234 }

  

作者:SamWang
出處:http://wangshenhe.cnblogs.com/
本文版權歸作者和博客園共有,歡迎圍觀轉載。轉載時請您務必在文章明顯位置給出原文鏈接,謝謝您的合作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM