Home

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.

 

The dynamic Data Type

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:

Data Type

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:

Data Type

Data Type

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:

Dynamic Variable

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.

ApplicationApplication: Using a Dynamic Variable

  1. To start a new project, on the main menu, click File -> New Project...
  2. In the middle list, click Empty Project
  3. Change the Name to DepartmentStore6 and click Add
  4. In the Solution Explorer, right-click DepartmentStore6 -> Add -> New Item...
  5. In the middle list, click Code File
  6. Change the name to PayrollProcessing and click Add
  7. On the main menu, click Project -> Add Reference...
  8. In the Add Reference dialog box, click the .NET tab
  9. In the list, click Microsoft.CSharp
     
    Add Referencce
  10. Click OK
  11. Change the file as follows:
    using System;
    
    public class Payroll
    {
        static int Main()
        {
            dynamic employeeName;
            dynamic hourlySalary, weeklyTime, weeklySalary;
    
            employeeName = "Patricia Katts";
            hourlySalary = 22.75;
            weeklyTime = 38.50;
            weeklySalary = hourlySalary * weeklyTime;
    
            Console.WriteLine("==============================");
            Console.WriteLine("Payroll Summary");
            Console.WriteLine("------------------------------");
            Console.Write("Employee Name: ");
            Console.WriteLine(employeeName);
            Console.Write("Hourly Salary: ");
            Console.WriteLine(hourlySalary);
            Console.Write("Weekly Time:   ");
            Console.WriteLine(weeklyTime);
            Console.Write("Weekly Salary: ");
            Console.WriteLine(weeklySalary);
            Console.WriteLine("==============================");
    
            System.Console.ReadKey();
            return 0;
        }
    }
  12. Execute the application:
    ==============================
    Payroll Summary
    ------------------------------
    Employee Name: Patricia Katts
    Hourly Salary: 22.75
    Weekly Time:   38.5
    Weekly Salary: 875.875
    ==============================
  13. Press any key and return to your programming environment
 

Home Copyright © 2010-2011 FunctionX