程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#中this的用法集錦

C#中this的用法集錦

編輯:C#入門知識

C#中this的用法集錦。本站提示廣大學習愛好者:(C#中this的用法集錦)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中this的用法集錦正文


this 症結字援用類確當前實例,還可用作擴大辦法的第一個參數的潤飾符。上面就針對this的四種用法,做簡略的總結。

起首,我們分離創立User和VIP兩個C#類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace AppConsole
{
 
  /// <summary>
  /// 通俗用戶
  /// </summary>
  public class User
  {
    /// <summary>
    /// 全局變量
    /// </summary>
    Dictionary<string, object> dictInfo = null;
 
    /// <summary>
    /// 結構器
    /// </summary>
    public User()
    {
      dictInfo = new Dictionary<string, object>();
    }
 
    /// <summary>
    /// 結構函數重載
    /// </summary>
    /// <param name="UserID"></param>
    /// <param name="UserName"></param>
    public User(int UserID, string UserName)
    {
      this.UserName = UserName;
      this.ID = UserID;
    }
 
    /// <summary>
    /// this,第【1】種用法,索引器
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public object this[string name]
    {
      get { return dictInfo[name]; }
      set { dictInfo[name] = value; }
    }
 
 
    /// <summary>
    /// 編號
    /// </summary>
    public int ID { get; set; }
 
 
    /// <summary>
    /// 用戶名
    /// </summary>
    public string UserName { get; set; }
 
    /// <summary>
    /// this第【2】種用法,當作參數傳遞
    /// </summary>
    public void Said()
    {
      new VIP().Say(this);
    }
 
  }
 
  /// <summary>
  /// 會員
  /// </summary>
  public class VIP : User
  {
 
 
    /// <summary>
    /// 積分
    /// </summary>
    public int integral { get; set; }
 
    /// <summary>
    /// 結構函數
    /// </summary>
    public VIP()
    {
      ID = 520;
      integral = 1000;
    }
 
    /// <summary>
    /// this第【3】種用法,經由過程this()挪用無參結構函數
    /// </summary>
    /// <param name="UserName"></param>
    public VIP(string UserName)
      : this()
    {
      this.UserName = UserName;
    }
 
    /// <summary>
    /// 結構函數重載
    /// </summary>
    /// <param name="UserID"></param>
    /// <param name="UserName"></param>
    public VIP(int UserID, string UserName)
      : base(UserID, UserName)
    {
 
    }
 
    /// <summary>
    ///Say辦法
    /// </summary>
    /// <param name="user"></param>
    public void Say([LCQAttribute] User user)
    {
      Console.WriteLine(string.Format("嗨,年夜家好!我的編號是{0},年夜家可以叫我{1}!", user.ID, user.UserName));
    }
 
  }
 
  /// <summary>
  /// 靜態類,來擴大User類
  /// </summary>
  public static class Helper
  {
 
    /// <summary>
    /// 第【4】種用法: this擴大User類
    /// </summary>
    /// <param name="user"></param>
    public static void Sing(this User user)
    {
      Console.WriteLine(string.Format("嗨,年夜家好!我的編號是{0},年夜家可以叫我{1}!", user.ID, user.UserName));
    }
  }
 
  /// <summary>
  /// 特征類:指定特征僅實用於辦法和辦法的參數
  /// </summary>
  [System.AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter)]
  public class LCQAttribute : System.Attribute
  {
 
  }
}

this第【1】種用法,索引器

/// <summary>
/// 全局變量
/// </summary>
Dictionary<string, object> dictInfo = null;
 
/// <summary>
/// this,第【1】種用法,索引器
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public object this[string name]
{
  get { return dictInfo[name]; }
  set { dictInfo[name] = value; }
}

this第【2】種用法,當作參數傳遞

/// <summary>
/// this第【2】種用法,當作參數傳遞
/// </summary>
public void Said()
{
  new VIP().Say(this);
}

this第【3】種用法,經由過程this()挪用無參結構函數

/// <summary>
/// this第【3】種用法,經由過程this()挪用無參結構函數
/// </summary>
/// <param name="UserName"></param>
public VIP(string UserName)
  : this()
{
  this.UserName = UserName;
}

this第【4】種用法:擴大User類

/// <summary>
/// 靜態類,來擴大User類
/// </summary>
public static class Helper
{
 
  /// <summary>
  /// 第【4】種用法: this擴大User類
  /// </summary>
  /// <param name="user"></param>
  public static void Sing(this User user)
  {
    Console.WriteLine(string.Format("嗨,年夜家好!我的編號是{0},年夜家可以叫我{1}!", user.ID, user.UserName));
  }
}

最初,掌握台測試

/// <summary>
/// 主法式進口
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
 
  //0>聲明實體
  User user = new User();
  user.ID = 1;
  user.UserName = "lichaoqiang";
 
  //第【一】種用法:this用作索引器 public object this[string name]{……}
  user["UserID"] = 1;
  Console.WriteLine("第【一】種用法:this用作索引器");
 
  //第【二】種用法:this用作參數傳遞 user.Say(this);
  Console.WriteLine("第【二】種用法:this用作參數傳遞");
  user.Said();
 
  //第【三】種用法:this() public VIP:this(){  }
  VIP vip = new VIP("yezi");
  vip.Said();
  Console.WriteLine("第【三】種用法:this()");
 
  //第【四】種用法: this擴大VIP類 public static Sing(this User user){……}
  Console.WriteLine("第【四】種用法: this擴大VIP類");
  user.Sing();
 
 
  Console.Read();
 
}

終究成果表示圖

以上所述上就是本文的全體內容了,願望年夜家可以或許愛好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved