다음을 통해 공유


What Can Vary with Development

The most significant difference among the supported programming languages is, not surprisingly, the language syntax. Both C++ and Visual Basic have an established history and significant base of existing code, which were taken into account when they were updated for .NET. Visual C#, on the other hand, starts with a cleaner slate. Several advanced topics — such as the build process and defining namespaces — will be introduced with the corresponding sample code in the following sections, but the following are some of the more obvious differences between the languages.

Case Sensitivity — C++ and Visual C# are both case sensitive, but Visual Basic is not. For a program to be CLS compliant, however, public members cannot differ only in their case. This restriction allows Visual Basic (and potentially other CLS-compliant languages) to produce components as well as to use components created in other, case-sensitive languages.

Referencing a Library — To use classes, which are located in namespaces, it is first necessary to obtain a reference to the assembly containing the required namespace. All .NET programs use — at a minimum — the System namespace, which is found in Mscorlib.dll (typically located in the Windows\System directory). Visual Basic and Visual C# implicitly reference Mscorlib.dll, but you must include the following preprocessor directive in a Managed Extensions for C++ program:

#using <mscorlib.dll>

Although Visual C# and Visual Basic implicitly reference the mscorlib assembly, it is necessary to use the /reference compile switch for other assemblies. Referenced libraries are generally located in the application directory or in a subdirectory of the application. Libraries that are designed for use by many applications — for example, tools provided by third parties — are located in the assembly cache (WindowsDirectory\Assembly) and must follow specific guidelines. Application configuration files can provide additional options. For ASP.NET applications, however, components should be located in the \Bin subdirectory under the starting point for the application's virtual directory.

Importing a Namespace — Classes can either be referenced fully (for example, System.IO.FileStream, which is similar to a fully qualified path name), or their namespace can be imported into the program, after which it is not necessary to fully qualify the contained class names. For convenient access to System objects, that namespace must be imported, as shown in the following table.

Managed Extensions for C++
using namespace System;
Visual C#
using System;
Visual Basic
Imports System

Referencing Object Members — Both Visual Basic and Visual C# support the period (.) as a scope-resolution operator which, for example, allows the syntax Console.WriteLine when referencing the WriteLine method of Console. Managed Extensions for C++ uses a double-colon (::) as a resolution operator.

Declaring Objects — In Managed Extensions for C++ and in Visual C#, but not in Visual Basic, variables must be declared before they can be used. You create a new instance of an object by using the new keyword. The following table contains sample declaration-creation statements for each of the three languages. Each statement declares and creates an object named myComp that is of type Comp, which is located in the Lib namespace.

C++
Lib::Comp* myComp = new Lib::Comp();
Visual C#
Lib.Comp myComp = new Lib.Comp();
Visual Basic
Dim myComp As New Lib.Comp

Program Entry Point — Every executable program has to have an external entry point, where the application begins its execution. The syntax has not changed for the Managed Extensions for C++ but, in Visual C# and Visual Basic, everything happens in a class. The following code shows the Managed Extensions for C++ syntax:

void main() {
}

The following code shows the Visual C# syntax:

class MainApp {
   public static void Main() {
   }
}

The following code shows the Visual Basic syntax:

Public Module modmain
   Sub Main()
   End Sub
End Module

Behind the scenes, however, the Managed Extensions for C++ compiler encapsulate the entry point in a class.

Defining a Namespace and a Class — Each of the three languages supports the creation of custom namespaces and of classes within those namespaces. All three languages handle this in code, although with slightly different syntax. For example, Managed Extensions for C++ uses a managed-class declaration, and trailing semicolons are not needed for namespace and class declarations in Visual C#. The following code shows the Managed Extensions for C++ syntax:

namespace CompVC {
   __gc class StringComponent {
   public:
      StringComponent() {
      }
   };
};

The following code shows the Visual C# syntax:

namespace CompCS {
   public class StringComponent {
      public StringComponent() {...}
   }
}

The following code shows the Visual Basic syntax:

Namespace CompVB
   Public Class StringComponent
      Public Sub New()
      End Sub
   End Class
End Namespace

See Also

Hello World | Writing Simple .NET Components | Clients for the Simple Components | Summary of Development Tutorial | Appendix A: Tools for Exploring Namespaces