To avoid repetitive definitions, I am trying to move internal function prototypes from the class One into a C# using My.Class.One directive (like import My.Class.One in Java).
In Java, this would change this code:
public class One {
public static void func1();
public static void func2();
public static int main(String[] args) { ... }
}
…into that code split into different files:
import My.Include;
public class One {
public static int Main(String[] args) { ... }
}
------------------------->8--------------------------------
package Include;
public class My {
public static void func1();
public static void func2();
...
}
After learning that Java 'packages' are named 'namespaces' in C# I came up with the following C# code which fails to compile (mcs One.cs My_include.cs -out:One.exe) with the error:
> “error CS0103: The name `func1′ does not exist in the current context”
using System;
using My.Include;
public class One {
public static int Main(String[] args) { return funct1(); }
}
------------------------->8--------------------------------
using System;
namespace My {
namespace Include {
public class functions {
public static void func1();
public static void func2();
...
}
}
}
I tried many different naming conventions but I still get the same error. Can you tell me what I do wrong?
It looks like you want a
partial class, so your main class file would look like:And you would have a second file which looks like:
At compile-time, these two (or more) files are automatically combined to a single class by the compiler. This gives you the separation you appear to be looking for, but keeps the relevant definitions within the correct scopes.
The
usingdirective is only partially synonymous to java’simportdirective – in C#usingonly provides a shortcut into anamespacewhereas you’re used to referring to a class or partial class withimport.