c#中的静态导入 [英] static imports in c#
本文介绍了c#中的静态导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
C#是否具有类似Java静态导入的功能?
Does C# has feature like Java's static imports?
所以不要编写像
FileHelper.ExtractSimpleFileName(file)
我可以写
ExtractSimpleFileName(file)
和编译器会知道我的意思是来自FileHelper的方法。
and compiler would know that I mean method from FileHelper.
推荐答案
从C#6.0开始,这是可能的:
Starting with C# 6.0, this is possible:
using static FileHelper;
// in a member
ExtractSimpleFileName(file)
但是,以前版本的C#没有静态导入。
However, previous versions of C# do not have static imports.
您可以使用该类型的别名来结束。
You can get close with an alias for the type.
using FH = namespace.FileHelper;
// in a member
FH.ExtractSimpleFileName(file)
或者,将静态方法更改为类型上的扩展方法 - 然后您可以将其称为:
Alternatively, change the static method to an extension method on the type - you would then be able to call it as:
var value = file.ExtractSimpleFileName();
这篇关于c#中的静态导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文