The .NET Base Class Library

The .NET base class library is a collection of object-oriented types and interfaces that provide object models and services for many of the complex programming tasks you will face.

Most of the types presented by the .NET base class library are fully extensible, allowing you to build types that incorporate your own functionality into your managed code.

The .NET Framework base class library contains the base classes that provide many of the services and objects you need when writing your applications. The class library is organized into namespaces. A namespace is a logical grouping of types that perform related functions. For example, the System.Windows.Forms namespace contains all of the types that make up windows forms and the controls used in those forms.

Namespaces are logical groupings of related classes. The namespaces in the .NET base class library are organized hierarchically. The root of the .NET Framework is the System namespace. Other namespaces can be accessed with the . (period) operator. Typical namespace construction appears as follows:
  • System
  • System.Data
  • System.Data.SQLClient
Reference Types and Value Types :

Types in the .NET Framework come in two varieties: value types and reference types. The primary difference between value types and reference types has to do with the way variable data is accessed. To understand this difference, a little bit of background on memory dynamics is required.

Application data memory is divided into two primary components, the stack and the heap. The stack is an area of memory that is reserved by the application for execution of the program. The stack is analogous to a stack of dinner plates. Plates are put onto the stack on top of one another.

When a plate is removed from the stack, it is always the one that was put on most recently. So it is with program variables. When a function is called, all of the variables used by the function are pushed onto the stack.

If that function calls additional functions, it pushes additional variables onto the stack. When the most recently called function terminates, all of its variables go out of scope and are popped off of the stack. The memory occupied by those variables is then freed up, and program execution continues.

The heap, on the other hand, is a separate area of memory that is reserved for the creation of objects that can be reused. The common language run time manages allocation of heap memory for objects and controls the reclamation of memory from unused objects through garbage collection.

All of the data associated with a value type is allocated on the stack. When a variable of a value type goes out of scope, it is destroyed, and its memory is reclaimed. A variable of a reference type, on the other hand, exists in two memory locations. The actual object data is allocated on the heap.

A variable containing a pointer to that object is allocated on the stack. When that variable is called by a function, it returns the memory address for the object it refers to. When that variable goes out of scope, the reference to the object is destroyed, but the object itself is not.

If any other references to that object exist, the object remains intact. If the object is left without any references to it, it is subject to garbage collection .

Examples of value types include primitives, such as Integer (int), Boolean (bool), Char (char), and so on, as well as user-defined Structures (structs) and Enumerations (enums). Classes represent the majority of reference types. Other types that are reference types include interfaces, delegates, and arrays.

Using .NET Framework Types in Your Application:

When you begin writing an application, you automatically begin with a reference to the .NET Framework base class library. This means that your application is aware of the base class library and is able to create instances of the types represented by it.

Value Types:

In Visual Basic .NET, you use the Dim statement to create a variable that represents a value type. In C#, you create a variable by declaring its type and then the variable name. The following is an example:

Visual Basic .NET

Dim myInteger as Integer

Visual C#

int myInteger;

This line tells the run time to allocate the appropriate amount of memory to hold an integer variable. Although this creates the variable, it does not assign a value to it. You can assign a value using the assignment operator as follows:

Visual Basic .NET

myInteger = 42

Visual C#

myInteger = 42;

You can also choose to assign a value to a variable upon creation, as shown in this example:

Reference Types:

Creating an instance of a type is a two-step process. The first step is to declare the variable as that type. This allocates the appropriate amount of memory for that variable, but does not actually create the object. You use the following syntax to declare an object:

Visual Basic .NET

Dim myForm As System.Windows.Forms.Form

Visual C#

System.Windows.Forms.Form myForm;

The preceding line tells the run time to set aside enough memory to hold a Form variable and assigns it the name myForm, but does not actually create the Form object in memory. The second step, called instantiation, actually creates the object. An example of instantiation follows:

Visual Basic .NET

myForm = New System.Windows.Forms.Form()

Visual C#

myForm = new System.Windows.Forms.Form();

This line is actually making a call to the constructor method of the type System.Windows.Forms.Form by way of the New (new) keyword. The constructor is a special method that is invoked only at the beginning of an object's lifetime. It contains any code that must be executed in order for the object to work (assigning values to properties, for example).

If any parameters were required by the constructor, they would be contained within the parentheses at the end of the line. The following example shows declaration and instantiation of a hypothetical Widget class that requires a string as a parameter in the constructor.

Visual Basic .NET

Dim myWidget as WidgetmyWidget = New Widget("This string is required by the constructor")

Visual C#

Widget myWidget;myWidget = new Widget("This string is required by the constructor");
You can also combine both declaration and instantiation into a single statement, if desired. By declaring and instantiating an object in the same line, you reserve the memory for the object and immediately create the object that resides in that memory. Although there was a significant performance penalty for this shortcut in previous versions of Visual Basic, Visual Basic .NET and Visual C# are optimized to allow this behavior without any loss of performance. The following example shows the one-step declaration and instantiation of a new Form.

Visual Basic .NET

Dim myForm As New System.Windows.Forms.Form()

Visual C#

System.Windows.Forms.Form myForm = new System.Windows.Forms.Form();
Both value types and reference types must be initialized before use. For class and structure fields in Visual Basic .NET, types are initialized with default values upon declaration. Numeric value types such as Integer or the floating-point types are assigned zero, Boolean variables are assigned False, and reference types are assigned to a null reference.

In C#, variables are not given default initialization. Any attempt to use an uninitialized variable, whether at a class level or a method level, will result in an error.

Using Value Type and Reference Type Variables

A variable that represents a value type contains all of the data represented by that type. A variable that represents a reference type contains a reference to a particular object. This is an important distinction to make. Consider the following example:

Visual Basic .NET

Dim x, y Integer x = 15y = xx = 30' What is the value of y?

Visual C#

int x, y;x = 15;y = x;x = 30;// What is the value of y?

In this example, two integer variables named x and y are created. x is assigned a value of 15, then y is assigned the value of x. Next, the value of x is changed to 30, and the question is posed: what is the value of y? The answer to this question might seem obvious, and it is: y = 15.

This is because x and y are two separate variables and have no effect on each other when changed. When the line y = x is encountered, the value of x is copied to the value of y, and there is no further connection between the two variables.

This situation changes, however in the case of reference types. Let's reconsider the previous example using a reference type (Form) instead of a value type.

Visual Basic .NET

Dim x, y as System.Windows.Forms.Form x = New System.Windows.Forms.Form()x.Text = "This is Form 1"y = xx.Text = "This is Form 2"' What value does y.Text return?

Visual C#

System.Windows.Forms.Form x,y;x = new System.Windows.Forms.Form();x.Text = "This is Form 1";y = x;x.Text = "This is Form 2";// What value does y.Text return?
What value does y.Text return? This time, the answer is not as obvious. Because System.Windows.Forms.Form is a reference type, the variable x does not actually contain a Form; rather, it points to an instance of a Form.

When the line y = x is encountered, the run time copies the reference from variable x to y. Thus, the variable y now points to the same instance of Form as the variable x does. Because these two variables actually refer to the same instance of the object, they will return the same values for properties of that object. Thus, y.Text returns "This is Form 2".

The Imports and using Statements

Up to this point, if you wanted to access a type in the .NET Framework base class library, you had to use the full name of the type including every namespace it belongs to. For example:

System.Windows.Forms.Form

You can make your development environment "aware" of various namespaces by using the Imports (Visual Basic .NET) or using (Visual C#) statement. This allows you to refer to a type using only its generic name and allows you to omit the qualifying namespaces.

Thus, you could refer to System.Windows.Forms.Form as simply Form. In Visual Basic .NET, the Imports statement must be placed at the top of the code window, preceding any other statements (except for Option statements). In Visual C#, the using statement must occur before any other namespace element, such as a class or struct.This example demonstrates the usage of this statement:

Visual Basic .NET

Imports System.Windows.Forms

Visual C#

using System.Windows.Forms;

When two types of the same name exist in more than one imported namespace, you must again use the fully qualified name in order to avoid a naming conflict. Thus, if you are using MyNameSpaceOne and MyNameSpaceTwo, and each contains a Widget class, you would have to refer to MyNameSpaceOne.Widget or MyNameSpaceTwo.Widget to ensure the correct result.

In C#, you can resolve namespace conflicts such as these by creating an alias. An alias allows you to choose one name to refer to another class. You create an alias using the using keyword, as shown below.

Visual C#

using myAlias = MyNameSpaceTwo.Widget;
After implementing an alias, you can then use it in code to represent the aliased class.

For example:

Visual C#

// You can now refer to MyNameSpaceTwo as myAlias. The // following two lines produce the same result:MyNameSpaceTwo.Widget = new myNameSpaceTwo.Widget;MyAlias anotherWidget = new myAlias;
Aliasing is not available in Visual Basic .NET.

Referencing External Libraries:

You might want to use class libraries not contained by the frame work, such as libraries developed by third-party vendors or libraries you have developed. In order to access these libraries, you must create a reference to that class library.

To create a reference to an external library:

In the Solution Explorer, right-click the References node of your project.
From the pop-up menu, choose Add Reference. The Add Reference dialog box appears.
Choose the appropriate tab for the library you want to reference. .NET libraries are available on the .NET tab. Legacy COM libraries appear on the COM tab, and local Visual Studio projects appear on the Projects tab.

Locate the library you want to reference, and double-click it to add it to the Selected components box. Click OK to confirm the choice of that reference.

RELATED POSTS

DAY 1 MICROSOFT DOT NET FRAME WORK

DAY 2 MICROSOFT DOT NET BASE CLASS LIBRARY

DAY 3 MICROSOFT DOT NET CLASSES AND STRECTURES

DAY 4 METHODS IN FRAME WORK

DAY 5 INPUT VALIDATIONS IN DOT NET PART ONE

DAY 6 INPUT VALIDATIONS IN DOT NET PART TWO

DAY 7 DATA TYPES IN DOT NET

DAY 8 DATA TYPES IN DOT NET PART TWO

DAY 9 IMPLEMENTING PROPERTIES IN DOT NET

DAY 10 DELEGATES AND EVENTS

No comments:

Post a Comment