中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

c# this關鍵字用法代碼詳解

發布時間:2020-09-12 02:50:02 來源:腳本之家 閱讀:141 作者:inner_cosmos 欄目:編程語言

用法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);
}

通過索引高效查詢數據

 以上就是小編整理的全部相關知識點內容,感謝大家的學習和對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

阳江市| 环江| 郸城县| 沙湾县| 中阳县| 巴彦淖尔市| 南丰县| 定远县| 海晏县| 营山县| 富锦市| 重庆市| 池州市| 德江县| 太白县| 沈阳市| 红安县| 井研县| 华宁县| 黄大仙区| 孟州市| 东平县| 西和县| 同心县| 北川| 丽江市| 界首市| 栾川县| 奉贤区| 广丰县| 壶关县| 山丹县| 黄陵县| 从江县| 崇明县| 八宿县| 固原市| 绥滨县| 南投县| 应城市| 柘城县|