栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#中this的用法集锦

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
{
 
  /// 
  /// 普通用户
  /// 
  public class User
  {
    /// 
    /// 全局变量
    /// 
    Dictionary dictInfo = null;
 
    /// 
    /// 构造器
    /// 
    public User()
    {
      dictInfo = new Dictionary();
    }
 
    /// 
    /// 构造函数重载
    /// 
    /// 
    /// 
    public User(int UserID, string UserName)
    {
      this.UserName = UserName;
      this.ID = UserID;
    }
 
    /// 
    /// this,第【1】种用法,索引器
    /// 
    /// 
    /// 
    public object this[string name]
    {
      get { return dictInfo[name]; }
      set { dictInfo[name] = value; }
    }
 
 
    /// 
    /// 编号
    /// 
    public int ID { get; set; }
 
 
    /// 
    /// 用户名
    /// 
    public string UserName { get; set; }
 
    /// 
    /// this第【2】种用法,当做参数传递
    /// 
    public void Said()
    {
      new VIP().Say(this);
    }
 
  }
 
  /// 
  /// 会员
  /// 
  public class VIP : User
  {
 
 
    /// 
    /// 积分
    /// 
    public int integral { get; set; }
 
    /// 
    /// 构造函数
    /// 
    public VIP()
    {
      ID = 520;
      integral = 1000;
    }
 
    /// 
    /// this第【3】种用法,通过this()调用无参构造函数
    /// 
    /// 
    public VIP(string UserName)
      : this()
    {
      this.UserName = UserName;
    }
 
    /// 
    /// 构造函数重载
    /// 
    /// 
    /// 
    public VIP(int UserID, string UserName)
      : base(UserID, UserName)
    {
 
    }
 
    /// 
    ///Say方法
    /// 
    /// 
    public void Say([LCQAttribute] User user)
    {
      Console.WriteLine(string.Format("嗨,大家好!我的编号是{0},大家可以叫我{1}!", user.ID, user.UserName));
    }
 
  }
 
  /// 
  /// 静态类,来扩展User类
  /// 
  public static class Helper
  {
 
    /// 
    /// 第【4】种用法: this扩展User类
    /// 
    /// 
    public static void Sing(this User user)
    {
      Console.WriteLine(string.Format("嗨,大家好!我的编号是{0},大家可以叫我{1}!", user.ID, user.UserName));
    }
  }
 
  /// 
  /// 特性类:指定特性仅适用于方法和方法的参数
  /// 
  [System.AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter)]
  public class LCQAttribute : System.Attribute
  {
 
  }
}

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

/// 
/// 全局变量
/// 
Dictionary dictInfo = null;
 
/// 
/// this,第【1】种用法,索引器
/// 
/// 
/// 
public object this[string name]
{
  get { return dictInfo[name]; }
  set { dictInfo[name] = value; }
}

this第【2】种用法,当做参数传递

/// 
/// this第【2】种用法,当做参数传递
/// 
public void Said()
{
  new VIP().Say(this);
}

this第【3】种用法,通过this()调用无参构造函数

/// 
/// this第【3】种用法,通过this()调用无参构造函数
/// 
/// 
public VIP(string UserName)
  : this()
{
  this.UserName = UserName;
}

this第【4】种用法:扩展User类

/// 
/// 静态类,来扩展User类
/// 
public static class Helper
{
 
  /// 
  /// 第【4】种用法: this扩展User类
  /// 
  /// 
  public static void Sing(this User user)
  {
    Console.WriteLine(string.Format("嗨,大家好!我的编号是{0},大家可以叫我{1}!", user.ID, user.UserName));
  }
}

最后,控制台测试

/// 
/// 主程序入口
/// 
/// 
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();
 
}

最终结果示意图

以上所述上就是本文的全部内容了,希望大家能够喜欢。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/125194.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号