c# this关键字用法代码详解
程序员文章站
2022-06-29 22:38:25
用法1 为原始类型扩展方法先说一下,this 后面跟的类型,就是要拓展方法的类型。注意要写在静态类中的静态方法,不然有些情况下访问/// /// 扩展类 用于为原始类...
用法1 为原始类型扩展方法
先说一下,this 后面跟的类型,就是要拓展方法的类型。注意要写在静态类中的静态方法,不然有些情况下访问
/// <summary> /// 扩展类 用于为原始类扩展方法 /// </summary> public static class am_extends { /// <summary> /// 为string类扩展了一个child方法,实现某功能 /// </summary> /// <param name="str"></param> /// <param name="new_str"></param> public static void child( this string str,string new_str) { object obj = str; str=new_str; } } 定义扩展方法
private void form1_load(object sender, eventargs e) { string st1 = "123"; string st2 = ""; string st3 = ""; st3 = st2.child(st1);//st3的值为“123” } 调用实例
用法2 this代表当前类的实例对象
用法3 用this串联构造函数
public class test { public test() { console.writeline("无参构造函数"); } // this()对应无参构造方法test() // 先执行test(),后执行test(string text) public test(string text) : this() { console.writeline(text); console.writeline("有参构造函数"); } }
用法4 索引器(基于索引器封装eplist,用于优化大数据下频发的linq查询引发的程序性能问题,通过索引从list集合中查询数据)
using system; using system.collections.generic; using system.linq; using system.reflection; using system.text; namespace mydemo.web { /// <summary> /// eplist 支持为list创建索引 /// </summary> /// <typeparam name="t">类型</typeparam> public class eplist<t> { #region 成员变量 /// <summary> /// 索引 /// </summary> private list<string[]> m_index = new list<string[]>(); /// <summary> /// 缓存数据 /// </summary> private dictionary<string, list<t>> m_cacheddata = new dictionary<string, list<t>>(); /// <summary> /// list数据源 /// </summary> private list<t> m_listdata = new list<t>(); /// <summary> /// 通过索引值取数据 /// </summary> /// <param name="indexfields">索引字段</param> /// <param name="fieldvalues">字段值</param> /// <returns></returns> public list<t> this[string[] indexfields] { get { string key = string.join(",", indexfields); if (m_cacheddata.containskey(key)) return m_cacheddata[key]; return new list<t>(); } } #endregion #region 公共方法 /// <summary> /// 创建索引 /// </summary> /// <param name="indexfields">索引字段</param> public void createindex(string[] indexfields) { if (m_index.contains(indexfields)) return; m_index.add(indexfields); } /// <summary> /// 添加 /// </summary> /// <param name="record">记录</param> public void add(t record) { m_listdata.add(record); m_index.foreach(indexfields => { string key = getkey(record, indexfields); if (m_cacheddata.containskey(key)) { m_cacheddata[key].add(record); } else { list<t> list = new list<t> { record }; m_cacheddata.add(key, list); } }); } #endregion #region 私有方法 /// <summary> /// 获取值 /// </summary> /// <param name="record">记录</param> /// <param name="fieldname">字段名</param> /// <returns></returns> private object getvalue(t record, string fieldname) { type type = typeof(t); propertyinfo propertyinfo = type.getproperty(fieldname); return propertyinfo.getvalue(record, null); } /// <summary> /// 获取key /// </summary> /// <param name="record">记录</param> /// <param name="indexfields">索引字段</param> private string getkey(t record, string[] indexfields) { list<string> values = new list<string>(); foreach (var field in indexfields) { string value = convert.tostring(getvalue(record, field)); values.add(field + ":" + value); } return string.join(",", values); } /// <summary> /// 获取key /// </summary> /// <param name="indexfields">索引字段</param> /// <param name="fieldvalues">字段值</param> /// <returns></returns> private string getkey(string[] indexfields, object[] fieldvalues) { if (indexfields.length != fieldvalues.length) return string.empty; for (int i = 0; i < indexfields.length; i++) { fieldvalues[i] = indexfields[i] + ":" + fieldvalues[i]; } string key = string.join(",", fieldvalues); return key; } #endregion } } 创建eplist
给eplist创建索引,并添加数据
private eplist<sysdepartinfo> geteplistdata() { eplist<sysdepartinfo> eplist = new eplist<sysdepartinfo>(); eplist.createindex(new string[] { "parentid" }); string sql = "select id,parentid,code,name from sysdepart"; sqlhelper.executereader(sql, null, (reader) => { sysdepartinfo record = new sysdepartinfo(); record.id = convert.tostring(reader["id"]); record.parentid = convert.tostring(reader["parentid"]); record.code = convert.tostring(reader["code"]); record.name = convert.tostring(reader["name"]); eplist.add(record); }); return eplist; } 给eplist创建索引,并添加数据
private eplist<sysdepartinfo> geteplistdata() { eplist<sysdepartinfo> eplist = new eplist<sysdepartinfo>(); eplist.createindex(new string[] { "parentid" }); string sql = "select id,parentid,code,name from sysdepart"; sqlhelper.executereader(sql, null, (reader) => { sysdepartinfo record = new sysdepartinfo(); record.id = convert.tostring(reader["id"]); record.parentid = convert.tostring(reader["parentid"]); record.code = convert.tostring(reader["code"]); record.name = convert.tostring(reader["name"]); eplist.add(record); }); return eplist; }
给eplist创建索引,并添加数据
/// <summary> /// 获取子节点 /// </summary> /// <param name="data"></param> /// <param name="parentid"></param> private ienumerable<treeinfo> createchildren(eplist<sysdepartinfo> data, treeinfo node) { string id = node == null ? "0" : node.id; list<treeinfo> childnodes = new list<treeinfo>(); // parentid字段上创建了索引,所以这里就可以通过索引值直接取出下一层子节点数据,避免linq查询引发的效率问题 var indexvalues = new string[] { "parentid:" + id }; var childdata = data[indexvalues]; childdata.foreach(record => { var childnode = new treeinfo { id = record.id, text = record.code + " " + record.name }; childnodes.add(childnode); childnode.children = createchildren(data, childnode); }); return childnodes.orderby(record => record.text); } 通过索引高效查询数据
以上就是小编整理的全部相关知识点内容,感谢大家的学习和对的支持。