gpt4 book ai didi

c# - "this"有什么用?

转载 作者:行者123 更新时间:2023-11-30 18:49:18 24 4
gpt4 key购买 nike

我看了一些c#代码,看不懂函数参数中的“this”关键字?有人能告诉我它是做什么用的吗?谢谢。

public static class ControlExtensions
{
public static void InvokeIfNeeded(this Control ctl,
Action doit)
{
if (ctl.InvokeRequired)
ctl.Invoke(doit);
else
doit();
}

public static void InvokeIfNeeded<T>(this Control ctl,
Action<T> doit, T args)
{
if (ctl.InvokeRequired)
ctl.Invoke(doit, args);
else
doit(args);
}
}

最佳答案

它用于指定一个类型,extension method运作。也就是说,public static void InvokeIfNeeded(this Control ctl, Action doit)Control 类(以及所有派生类)“添加”一个 InvokeIfNeeded 方法).但是,只有当您将声明它们的类的命名空间显式导入您的范围时,才能使用此方法。

关于c# - "this"有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1595553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com