Introduction to Built-In Libraries |
|
Introduction to the C# Library |
Unlike many other languages such as C++ or (Object) Pascal (Delphi), C# is known as being one of the languages that don't have their own library. This is not an anomaly and it doesn't make C# less efficient than any other language. That's just the way the language was designed. In fact, you will find out in this and other lessons that C# can benefit from using the libraries in the other languages, thanks to its tremendous flexibility. |
In reality, C# has a tiny library made of just a few classes you will almost never use. The library is named Microsoft.CSharp.dll. In the new C# 4.0 release of the language, the library was slightly updated and includes a new data type we must now use. If you create a console application, Microsoft Visual Studio automatically adds the Microsoft.CSharp.dll library to your project. Otherwise, if you create an empty project but you want to use this library, you can easily add it using the Add Reference dialog box. |
In previous lessons, we used different data types (short, int, float, double, decimal, and string). We saw that the data type communicates to the compiler how much memory it should reserve for the variable. In fact, if you position the mouse on top of a data type in the Code Editor, a tool tip would let you know how much space its variable would use:
We also saw that if you don't want to specify a data type, when declaring the variable, you can use the var keywork but you must initialize the variable before using it. After initializing the variable, the compiler reserves memory based on its estimation from the value you assigned. When using the variable, if you position the mouse on it, the Code Editor would show the data type that was assigned to it:
In some scenarios, you would not know how much space is appropriate, until you need to use the variable. In other words, you would not worry the compiler with the variable's logistics until the operation that needs the variable accesses it. Such a variable is referred to as dynamic. To declare a dynamic variable, you use the dynamic keyword. Here is an example of declaring a dynamic variable: public class Exercise
{
static int Main()
{
dynamic value;
return 0;
}
}
Unlike a variable declared with the var keyword, you don't have to initialize a dynamic variable when declaring it. Still, before using it, at one time or another, you must assign a value to it. When you assign a value to a dynamic variable, the compiler doesn't check how much space would suit the variable and if you position your mouse on the variable, the Code Editor would only tell you that the variable is dynamic:
A dynamic variable is mostly used if you are creating a project that would communicate with an extenal application and it would use values coming from that application. An example is a Microsoft Word document or a Microsoft Excel spreadsheet developed using the C# language (or from Microsoft Visual C#) that imports an external library.
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|