Visual C#

Using Libraries

The Windows controls featured in the .NET Framework are highly varied and provide all the necessary regular functionality a normal application would need…

2 min read540 words3 code samples

Introduction#

The .NET Framework includes a wide array of Windows controls that provide the standard features required by most applications. These controls offer various properties and methods to handle common tasks. To further expand your application's capabilities, you can build your own custom libraries or incorporate those written in different programming languages.

Using a Library#

If the .NET Framework doesn't have a class you are looking for, you can create one and be able to use it over and over again in different programs. You can even create a commercial class and be able to distribute or sell it. To make this possible, you can "package" one or more classes in a library. A library is a program that contains classes and/or other resources that other programs can use. Such a program is created with the same approach as the programs we have done so far. Because a library is not an executable, it doesn't need the Main() function. A library usually has the extension .dll. A library can be made of a single file or as many files as necessary. A file that is part of a library can contain one or more classes. Each class should implement a behavior that can eventually be useful and accessible to other classes. The classes in a library are created exactly like those we have used so far. Everything depends on how you compile it. To create a library, start by typing its code in a text file. Once the library is ready, to compile it, at the Command Prompt, you would type:

C#
csc /target:library NameOfFile.cs

syntax: following the use name, custom a want you If created. be would .dll extension the and file the of name the with library a this, doing After Enter. press and

C#
csc /target:library /out:DesiredNameOfLibrary.dll NameOfFile.cs

Using a Visual C++/CLI Library#

A primary objective of the .NET ecosystem is to facilitate seamless collaboration between different languages, particularly for code reuse. This means you can easily integrate functionality from one language into another, such as using Visual Basic's extensive function library within a C# project. Since no single library can cover every possible need, you might occasionally require external code, perhaps from a C++ team. Fortunately, Visual C++ now includes wizards that simplify the process of creating class libraries. To start, select 'Class Library' under Visual C++ in the New Project dialog, name it, and then add your classes and functions to the source file.

Using the Win32 Library#

The Microsoft Windows operating system was originally written in C, the parent language of C++ and C# (also of Java and JavaScript). To allow programmers to create applications, Microsoft released a library called Win32. This is a series of functions and classes, etc, that you previously had to use. As time has changed, you don't need to exclusively use Win32 anymore to create a Windows application. Nonetheless, Win32 is still everywhere and it is not completely avoidable because many or some of the actions you would want to perform in a Windows application are still available only in Win32. Fortunately, in most cases, it is not always difficult to use some of these functions in a C# applications, as long as you observe some rules. Here is an example:

C#
using System;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("Kernel32.dll")]
    public static extern bool SetConsoleTitle(string strMessage);

    static int Main()
    {
 	SetConsoleTitle("C# Programming");

	return 0;
    }
}

Main Page Copyright © 2010-2011 C# Key