c# this关键字用法
- 用法1 为原始类型扩展方法
先说一下,this 后面跟的类型,就是要拓展方法的类型。注意要写在静态类中的静态方法,不然有些情况下访问
1 /// <summary> 2 /// 扩展类 用于为原始类扩展方法 3 /// </summary> 4 public static class AM_Extends 5 { 6 /// <summary> 7 /// 为string类扩展了一个child方法,实现某功能 8 /// </summary> 9 /// <param name="str"></param> 10 /// <param name="new_str"></param> 11 public static void Child( this string str,string new_str) 12 { 13 object obj = str; 14 str=new_str; 15 } 16 }
1 private void Form1_Load(object sender, EventArgs e) 2 { 3 string st1 = "123"; 4 string st2 = ""; 5 string st3 = ""; 6 st3 = st2.Child(st1);//st3的值为“123” 7 }
- 用法2 this代表当前类的实例对象
- 用法3 用this串联构造函数
1 public class Test 2 { 3 public Test() 4 { 5 Console.WriteLine("无参构造函数"); 6 } 7 // this()对应无参构造方法Test() 8 // 先执行Test(),后执行Test(string text) 9 public Test(string text) : this() 10 { 11 Console.WriteLine(text); 12 Console.WriteLine("有参构造函数"); 13 } 14 }
- 用法4 索引器(基于索引器封装EPList,用于优化大数据下频发的Linq查询引发的程序性能问题,通过索引从list集合中查询数据)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Reflection; 5 using System.Text; 6 7 namespace MyDemo.Web 8 { 9 /// <summary> 10 /// EPList 支持为List创建索引 11 /// </summary> 12 /// <typeparam name="T">类型</typeparam> 13 public class EPList<T> 14 { 15 #region 成员变量 16 17 /// <summary> 18 /// 索引 19 /// </summary> 20 private List<string[]> m_Index = new List<string[]>(); 21 22 /// <summary> 23 /// 缓存数据 24 /// </summary> 25 private Dictionary<string, List<T>> m_CachedData = new Dictionary<string, List<T>>(); 26 27 /// <summary> 28 /// List数据源 29 /// </summary> 30 private List<T> m_ListData = new List<T>(); 31 32 /// <summary> 33 /// 通过索引值取数据 34 /// </summary> 35 /// <param name="indexFields">索引字段</param> 36 /// <param name="fieldValues">字段值</param> 37 /// <returns></returns> 38 public List<T> this[string[] indexFields] 39 { 40 get 41 { 42 string key = string.Join(",", indexFields); 43 if (m_CachedData.ContainsKey(key)) return m_CachedData[key]; 44 return new List<T>(); 45 } 46 } 47 48 #endregion 49 50 #region 公共方法 51 52 /// <summary> 53 /// 创建索引 54 /// </summary> 55 /// <param name="indexFields">索引字段</param> 56 public void CreateIndex(string[] indexFields) 57 { 58 if (m_Index.Contains(indexFields)) return; 59 m_Index.Add(indexFields); 60 } 61 62 /// <summary> 63 /// 添加 64 /// </summary> 65 /// <param name="record">记录</param> 66 public void Add(T record) 67 { 68 m_ListData.Add(record); 69 m_Index.ForEach(indexFields => 70 { 71 string key = getKey(record, indexFields); 72 if (m_CachedData.ContainsKey(key)) 73 { 74 m_CachedData[key].Add(record); 75 } 76 else 77 { 78 List<T> list = new List<T> { record }; 79 m_CachedData.Add(key, list); 80 } 81 }); 82 } 83 84 #endregion 85 86 #region 私有方法 87 88 /// <summary> 89 /// 获取值 90 /// </summary> 91 /// <param name="record">记录</param> 92 /// <param name="fieldName">字段名</param> 93 /// <returns></returns> 94 private object getValue(T record, string fieldName) 95 { 96 Type type = typeof(T); 97 PropertyInfo propertyInfo = type.GetProperty(fieldName); 98 return propertyInfo.GetValue(record, null); 99 } 100 101 /// <summary> 102 /// 获取Key 103 /// </summary> 104 /// <param name="record">记录</param> 105 /// <param name="indexFields">索引字段</param> 106 private string getKey(T record, string[] indexFields) 107 { 108 List<string> values = new List<string>(); 109 foreach (var field in indexFields) 110 { 111 string value = Convert.ToString(getValue(record, field)); 112 values.Add(field + ":" + value); 113 } 114 return string.Join(",", values); 115 } 116 117 /// <summary> 118 /// 获取Key 119 /// </summary> 120 /// <param name="indexFields">索引字段</param> 121 /// <param name="fieldValues">字段值</param> 122 /// <returns></returns> 123 private string getKey(string[] indexFields, object[] fieldValues) 124 { 125 if (indexFields.Length != fieldValues.Length) return string.Empty; 126 for (int i = 0; i < indexFields.Length; i++) 127 { 128 fieldValues[i] = indexFields[i] + ":" + fieldValues[i]; 129 } 130 string key = string.Join(",", fieldValues); 131 return key; 132 } 133 134 #endregion 135 } 136 }
1 private EPList<SysDepartInfo> GetEPListData() 2 { 3 EPList<SysDepartInfo> eplist = new EPList<SysDepartInfo>(); 4 eplist.CreateIndex(new string[] { "ParentId" }); 5 string sql = "select Id,ParentId,Code,Name from SysDepart"; 6 SqlHelper.ExecuteReader(sql, null, (reader) => 7 { 8 SysDepartInfo record = new SysDepartInfo(); 9 record.Id = Convert.ToString(reader["Id"]); 10 record.ParentId = Convert.ToString(reader["ParentId"]); 11 record.Code = Convert.ToString(reader["Code"]); 12 record.Name = Convert.ToString(reader["Name"]); 13 eplist.Add(record); 14 }); 15 return eplist; 16 }
1 /// <summary> 2 /// 获取子节点 3 /// </summary> 4 /// <param name="data"></param> 5 /// <param name="parentId"></param> 6 private IEnumerable<TreeInfo> CreateChildren(EPList<SysDepartInfo> data, TreeInfo node) 7 { 8 string id = node == null ? "0" : node.id; 9 List<TreeInfo> childNodes = new List<TreeInfo>(); 10 // ParentId字段上创建了索引,所以这里就可以通过索引值直接取出下一层子节点数据,避免Linq查询引发的效率问题 11 var indexValues = new string[] { "ParentId:" + id }; 12 var childData = data[indexValues]; 13 childData.ForEach(record => 14 { 15 var childNode = new TreeInfo 16 { 17 id = record.Id, 18 text = record.Code + " " + record.Name 19 }; 20 childNodes.Add(childNode); 21 childNode.children = CreateChildren(data, childNode); 22 }); 23 return childNodes.OrderBy(record => record.text); 24 }
参考文献: https://www.cnblogs.com/yellowcool/p/7908607.html