C#中的此参数修饰符? [英] this parameter modifier in C#?
本文介绍了C#中的此参数修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对以下代码片段感到好奇:
I'm curious about this code snippet:
public static class XNAExtensions
{
/// <summary>
/// Write a Point
/// </summary>
public static void Write(this NetOutgoingMessage message, Point value)
{
message.Write(value.X);
message.Write(value.Y);
}
// ...
};
this
关键字在参数类型旁边是什么意思?即使在C#规范中,我似乎也找不到任何有关它的信息.
What does the this
keyword mean next to the parameter type? I can't seem to find any information about it anywhere, even in the C# specification.
推荐答案
那是扩展方法.
语法意味着您可以像调用该方法一样将其视为NetOutgoingMessage类的成员:
The syntax means you can call the method as if it was a member of the NetOutgoingMessage class:
var msg = new NetOutgoingMessage();
msg.Write(somePoint);
这基本上是由编译器重写为:
This is basically rewritten by the compiler to:
var msg = new NetOutgoingMessage();
XNAExtensions.Write(msg, somePoint);
这只是很好的语法糖.
这篇关于C#中的此参数修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文