C# Utility Class - Setting up for General Use and Compiling w/o Main -
okay, working on learning c#. great language, love working in general, i'm not understanding how on lack of utility classes. in essence, want set general utilities class can contained in folder , used project doing "using namespace globals/utilities/etc." command. in essence:
using system; namespace globals { public static class commands { public static void waitforreturn() { console.writeline("press enter continue."); console.readline(); } } }
similar above, such in other class can call functions including preprocessing directive.
using system; using globals; namespace rectangledemo { <rectangle definition here> class demo { static void main(string[] args) { rectangle r = new rectangle(); rectangle s = new rectangle(5, 6); r.display(); s.display(); waitforreturn(); } } }
as is, i'm trying compile 'utility' class (with more listed above) check errors, it's telling me can't compile because there's no main method. suggestions?
(yes i'm aware have java coding style, i'm okay that!)
c# doesn't work way you're expecting. there no relationship between importing namespaces/classes , files doing using globals;
not translate file content inclusion à la c/c++ (i.e. not same #include "globals.cs"
).
on command line, c# compiler accepts list of files make sources:
csc demo.cs globals.cs
if using ide visual studio, link globals.cs
project. , link, mean more symbolic link (though not in actual fact) static linking via linker have typically in c/c++ setup.
so make scenario of “inclusion of common code” work, need compile adding globals.cs
list of c# files making project (supplied compiler on command line or added project in ide) , using static import:
using system; using static globals.commands; // <-- import statically allows methods // used without class name qualification namespace rectangledemo { // <rectangle definition here> class demo { static void main(string[] args) { rectangle r = new rectangle(); rectangle s = new rectangle(5, 6); r.display(); s.display(); waitforreturn(); } } }
Comments
Post a Comment