How can I refer to a project from another one in c#?

First, you need to add a reference to Project2 in Project1.

If you go to Project1 -> References -> Add Reference, you should see an option to add projects in solutions and add project2.

Once you add a reference, to call a class Foo in namespace Name1.Name2, you can use the class as

Name1.Name2.Foo foo = new Name1.Name2.Foo(...);

or if you would like to avoid typing, you could add the using statement near the top of the file

using Name1.Name2;

and can now reference the class using just Foo, like

Foo foo = new Foo(...);

Note, you will have figure out the namespaces in Project2. Just using the name Project2 won’t work. Look at the file which contains the declaration of the class you want to use and look for the namespace definition.

So if you see something as

namespace Name1.Name2 {


    class Bar {
        // Blah
    }

    // Notice the word public here.
    public class Foo {
        // Blah
    }
}

Name1.Name2 is the namespace for Foo and that is what you need to use.

Also note that you will likely need to have the public access modifier for the class which you want to use in Project1. For example in the above scenario, you should be able to access class Foo, but not class Bar.

This page will help you understand namespaces: http://www.csharp-station.com/tutorials/lesson06.aspx

Leave a Comment