Bru*_*lip 1 c# static-methods static-libraries
所以我有一个静态类让我们说它叫做Worker,并且让我说它里面有一个名为Wait(float f)的方法,所有公共所以我可以在任何地方访问它,如下所示:
Worker.Wait(1000);
Run Code Online (Sandbox Code Playgroud)
现在我想知道有什么方法可以定义某种独特的特殊方法,所以我可以像这样做:
Wait(1000);
Run Code Online (Sandbox Code Playgroud)
(没有在课堂上我会使用它)?
使用C#6可以完成.在文件的顶部,您需要添加一个using static Your.Type.Name.Here;.
namespace MyNamespace
{
public static class Worker
{
public static void Wait(int msec)
{
....
}
}
}
//In another file
using static MyNamespace.Worker;
public class Foo
{
public void Bar()
{
Wait(500); //Is the same as calling "MyNamespace.Worker.Wait(500);" here
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
119 次 |
| 最近记录: |