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

后端 未结 9 1424
挽巷
挽巷 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:21

    The "proper" (from an OO point of view of bringing a member into scope, is to IMPLEMENT it within the scope:

    static class MySpecialStuff 
    {
        public static int MySpecialValue { get {...} }
    }
    
    class MyWorkingClass
    {
          private static int MySpecialValue { get { return ySpecialStuff.MySpecialValue; } }
    }
    

    Now in the remainder of MyWorkingClass (or in derived classes if you change the private to protected) you can reference MySpecialValue thousands of times without any additional qualifications.

提交回复
热议问题