C# importing class into another class doesn’t work

UPDATE: As of C# 6, if you want to use the static members of a class without specifying the class name, you can use the using static directive to import the static members into the current scope, like this:

using static MyNamespace.MyClass;

Although this is not what the original question is about, you get a similar error to the one OP gets, so I’m providing it for completeness.

Answer to OP’s question:

using is for namespaces only – if both classes are in the same namespace just drop the using.

You have to reference the assembly created in the first step when you compile the .exe:

csc /t:library /out:MyClass.dll MyClass.cs
csc /reference:MyClass.dll /t:exe /out:MyProgram.exe MyMainClass.cs

You can make things simpler if you just compile the files together:

csc /t:exe /out:MyProgram.exe MyMainClass.cs MyClass.cs

or

csc /t:exe /out:MyProgram.exe *.cs

EDIT:
Here’s how the files should look like:

MyClass.cs:

namespace MyNamespace {
    public class MyClass {
        void stuff() {
 
        }
    }
}

MyMainClass.cs:

using System;

namespace MyNamespace {
    public class MyMainClass {
        static void Main() {
            MyClass test = new MyClass();
        }
    }
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)