• 友链

  • 首页

  • 文章归档
h u a n b l o g
h u a n b l o g

欢

HI,Friend

03月
16
C#

C#中this的几种用法

发表于 2022-03-16 • 字数统计 2972 • 被 752 人看爆

用法一

代表当前类的实例对象
public class Test
{
    private string strText = "GolbalValue";
    public string GetText()
    {
        
        string strText = "LocalValue";
        return this.strText + "-" + strText;
    }
}

class Program
    {
        static void Main(string[] args)
        {
            
            Test test = new Test();
            Console.WriteLine(test.GetText());

            Console.ReadKey();
        }
    }

运行结果

GolbalValue-LocalValue

用法二

串联构造函数
public class Test
{
    public Test()
    {
        Console.WriteLine("No parameter constructor");
    }

    public Test(string text):this()
    {
        Console.WriteLine("No parameter constructor");
        Console.WriteLine(text);
    }
}

class Program
    {
        static void Main(string[] args)
        {
            
            Test test = new Test("你好");

            Console.ReadKey();
        }
    }

运行结果

No parameter constructor
No parameter constructor
你好

方法三

为原始类型扩展方法
拓展方法的类型。
注意要写在静态类中的静态方法

用途

扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。
它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。仅当您使用 using 指令将命名空间显式导入到源代码中之后,扩展方法才位于范围中。

定义和调用扩展方法:

  • 1、定义一个静态类以包含扩展方法。该类必须对客户端代码可见。有关可访问性规则的更多信息,请参见访问修饰符(C# 编程指南)。
  • 2、将该扩展方法实现为静态方法,并使其至少具有与包含类相同的可见性。
  • 3、该方法的第一个参数指定方法所操作的类型;该参数必须以 this 修饰符开头。
  • 4、在调用代码中,添加一条 using 指令以指定包含扩展方法类的命名空间。
  • 5、按照与调用类型上的实例方法一样的方式调用扩展方法。

请注意,第一个参数不是由调用代码指定的,因为它表示正应用运算符的类型,并且编译器已经知道对象的类型。您只需通过 n 为这两个形参提供实参

using Test;

namespace Test
{
    public static class MyTest
    {
        public static int WordCount(this int index)
        {
            return index+1;
        }
    }
}

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int s = 6;
            int i = s.WordCount();
            Console.WriteLine("WorldCount={0}", i);
            Console.ReadKey();
        }
    }
}

运行结果

WorldCount=7

方法四

索引器
索引器是一组get和set访问器,与属性类似。
 class Test
    {
        int temp0 = 1;
        int temp1 = 2;
        public int this[int index]
        {
            get { return (index == temp0) ? temp0 : temp1; }
            set
            {
                if (index == temp0)
                    temp0 = value;
                else
                    temp1 = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            Test test = new Test();
            Console.WriteLine("Value -- T0:{0}, T1:{1}", test[0], test[1]);
            test[0] = 1;
            test[1] = 2;
            Console.WriteLine("Value -- T0:{0}, T1:{1}", test[0], test[1]);
            Console.ReadKey();
        }
    }

运行结果

Value -- T0:2, T1:1
Value -- T0:1, T1:1

参考

xuanyin235-C#入门一一this的几种用法

分享到:
广义表
队列
  • 文章目录
  • 站点概览
欢

网红 欢

你能抓到我么?

Email RSS
看爆 Top5
  • mac系统版本与Xcode版本有冲突 4,090次看爆
  • JAVA_HOME环境配置问题 3,740次看爆
  • AssetBundle使用 3,508次看爆
  • VSCode配置C++开发环境 3,263次看爆
  • Lua反射 3,139次看爆

Copyright © 2025 欢 粤ICP备2020105803号-1

由 Halo 强力驱动 · Theme by Sagiri · 站点地图