How to Use Class Libraries in Visual Studio

See Visual Studio: Tips and Tricks for similar articles.

The steps below describe how to utilize class libraries in Visual Studio.

  1. A class library is a collection of class definitions contained in a .dll or .exe file.
  2. In order to use the class library, you must first add a reference to the library (see "How to add references to your Visual Studio Project").
  3. Once the library has been referenced, the classes in the library can be instantiated and exercised. In order to instantiate the class, you must either fully qualify the class name or bring it into scope with a using statement (C#) or Import statement (VB). To fully qualify the class name, you reference the library name and class name using dot syntax. If, for example, the library is named 'MyLib' and the class is named "MyClass", you would use the following syntax:
    C#
    
    	MyLib.MyClass = new MyLib.MyClass();
    VB
    Dim newObject as MyLib.MyClass()
  4. If you prefer, you can bring the namespace into scope with a 'using' or 'import' statement and then reference the class directly. For example,
    C#
    using MyLib;	 
    	class NewClass;
    		{
    			static void Main(string[] args)
    				{
    					MyClass = new MyClass();
    				}
    		}
    VB
    
    	imports MyLib	
    	Sub Main()
    		Dim newObject as MyClass()
    	End Sub
    	
  5. After instantiating the class, you can then use it as you would any object (exercise the properties, methods, etc.).

Related Articles

  1. How to Add References to Your Visual Studio Project
  2. How to Use Class Libraries in Visual Studio (this article)