How to “import” a static class in C#?

后端 未结 9 1447
挽巷
挽巷 2020-12-28 19:08

I have created a public static class utils.cs I want to use it in other classes without prefixing method with utils, what\'s the syntax to do this ?

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-28 19:27

    There's no way of doing this in C# - no direct equivalent of Java's static import feature, for example.

    For more information about why this is the case, see Eric Lippert's post on the topic and a similar SO question.

    In certain circumstances, however, it may make sense to write extension methods which live in non-nested static classes but "pretend" to be instance methods of a different class. It's worth thinking carefully before you introduce these, as they can be confusing - but they can also improve readability when used judiciously.

    What sort of utility methods were you thinking of?

提交回复
热议问题