c# - Quickly reference static methods -
is chance possible call method without referencing class?
for instance, have helper class:
class helpertools { public static void dowork() { /*...*/ } }
and need call it:
class mainclass { public static void main() { helpertools.dowork(); } }
is possible call dowork();
without reference? this:
public static void main() { dowork(); }
just sake of simplicity.
not quite, here 5 patterns close:
namespace my.namespace { using h = myhelperclass; public class myhelperclass { public static void helperfunc1() { console.writeline("here's help!"); } } public class myhelperclass2 { public static void helperfunc4() { console.writeline("here's help!"); } } public interface ihelper{ } public static class helperextensions { public static void helperfunc3(this ihelper self) { console.writeline("here's help!"); } } public class myclass : myhelperclass2, ihelper { private static readonly action helperfunc2 = myhelperclass.helperfunc1; private static void helperfunc5() { console.writeline("here's help!"); } public void myfunction() { //method 1 use alias make helper class name shorter h.helperfunc1(); //method 2 use class property helperfunc2(); //method 3 extend interface has extension methods. //note: you'll have use keyword when calling extension this.helperfunc3(); //method 4 have access methods on classes extend. helperfunc4(); //method 5 put helper method in class helperfunc5(); } } }
Comments
Post a Comment