Dela via


Introduction to the .NET Framework Class Library in Visual Studio

A key feature of the .NET Framework is the base class library. For more information about the common language runtime, the class library, the common language specification (CLS), and other features of the .NET Framework, see Inside the .NET Framework.

Namespaces

The .NET Framework class library is comprised of namespaces. Each namespace contains types that you can use in your program: classes, structures, enumerations, delegates, and interfaces. For an overview of the namespaces, see .NET Framework Class Library.

Namespaces provide scope: Two classes with the same name can be used in your program as long as they are in different namespaces and as long as you qualify the names to the namespaces. The namespace name is part of the fully qualified name of the type (namespace.typename).

All namespaces shipped by Microsoft begin with one of two names: System or Microsoft.

Managed DLLs (Assemblies)

The functionality of the .NET Framework class library is not contained in a single DLL. By putting the functionality of the base classes in multiple DLLs, a managed program does not need to load, at start up, a large DLL, but, rather, one or more smaller DLLs. This reduces the startup time for a program.

You can use namespaces that are defined in your project. However, you will commonly use types in namespaces that reside in a managed DLL. A managed DLL is also called an assembly.

When you create a Visual Basic or Visual C# project in Visual Studio, the most common base class DLLs (assemblies) are already referenced. However, if you need to use a type that is in a DLL not already referenced, you will need to add a reference to the DLL. The Add Reference dialog box allows you to add an assembly.

Namespaces and Assemblies: Usage Summary

The following is a summary of how you will commonly use managed DLLs (assemblies), namespaces, and the types in the namespaces:

  1. Locate a class that provides the functionality you need. For more information, see .NET Framework Class Library in Visual Studio.

  2. In the documentation overview of the type, note the name of the type's assembly and namespace.

  3. See if the assembly is already referenced in your project. Open Solution Explorer and look under the References node.

  4. If you do not see an assembly reference, right-click the References node and select Add Reference. For more information, see Adding and Removing References.

  5. After you have an assembly reference, you can access the types in the assembly.

    Tip

    If you do not want to use fully qualified names (namespace.typename), you can use the Imports statement in Visual Basic or the using keyword in Visual C#.

The following example shows how Visual Basic and Visual C# implicitly use the namespace (and in Visual Basic, the class) qualifier:

' TestImports.vb
Imports System.Console
Module Module1
   Sub Main()
      WriteLine("Hello")
      ' Without the Imports statement, you would have to use
      ' System.Console.WriteLine("Hello").
      End Sub
End Module
// TestUsing.cs
using System;
class MyClass
{
   public static void Main()
   {
      Console.WriteLine("Hello");
      //  Without the using statement, you would have to use
      // System.Console.WriteLine("Hello");
   }
}

You can put this code into a file, called test.vb or test.cs, and compile it at the command line by typing vbc TestImports.vb or csc TestUsing.cs. You do not have to explicitly reference the mscorlib.dll, which is the assembly that contains the System namespace; the Visual Basic or Visual C# compiler always references mscorlib.dll.

See Also

Reference

.NET Framework Class Library

Other Resources

.NET Framework Class Library in Visual Studio

Overview of the .NET Framework

Microsoft .NET Web Site (https://www.microsoft.com/net/)