Saturday, 25 June 2011

The .NET Data Types

The .NET data types are the types we use to store our data. They are value types and can be broken down into subcategories: Integer types, Floating-point types, the Boolean type, and the Char type. Two built-in reference types that are an integral part of our application will also be discussed: the String type and the Object type.
Integer Types

The .NET Framework provides a variety of Integer types. Table 3.1 summarizes these types and lists their corresponding Microsoft Visual Basic .NET and Microsoft Visual C# types.


Integer Types in the .NET Framework
Type Visual C# Name Visual Basic .NET Name DescriptionRange
System.Byte byte Byte 8-bit unsigned integer 0 to 255
System.Int16 short Short 16-bit signed integer -32768 to 32767
System.Int32 int Integer 32-bit signed integer -231 to 231-1
System.Int64 long Long 64-bit signed integer -263 to 263-1
System.SByte sbyte (Not implemented) 8-bit signed integer -128 to 127
System.UInt16 ushort (Not implemented) 16-bit unsigned integer 0 to 65535
System.UInt32 uint (Not implemented) 32-bit unsigned integer 0 to 232-1
System.UInt64 ulong (Not implemented) 64-bit unsigned integer 0 to 264-1
Floating-Point Types
There are three floating-point types that can be used to represent numbers that have a fractional component.
Non-Numeric Types
Four additional types that do not represent numbers are discussed in this section: System.Boolean, System.Char, System.String, and System.Object.
System.Boolean

The System.Boolean type is used to represent a value that is either true or false. It is called Boolean in Visual Basic .NET, and bool in Visual C#. The values that are valid for Boolean variables are True and False (Visual Basic .NET) or true and false (Visual C#).

System.Char
The System.Char type represents a single instance of a 16-bit Unicode character. It is called char in Visual C# and Char in Visual Basic .NET. We can assign a character literal to a variable of this type by enclosing the literal in single quotes (Visual C#) or in double quotes using the suffix "c" (Visual Basic .NET)

System.String
The System.String type is a reference type, and it represents a series of Char data types. In everyday terms, a string can represent a word, a paragraph, a key value, or any other string of characters.

System.Object
The Object type is the supertype of all types in the .NET Framework. Every type, whether value type or reference type, derives from System.Object. In Visual Basic .NET it is called Object, and in Visual C# it is called object. We can assign any object or value to an object variable.

Converting Types
At times, we will need to convert data from one type to another. Data can be converted in two ways: implicitly, which means the conversion is performed automatically, and explicitly, which means we must specifically ask for the conversion to be performed.

Implicit Conversions
Implicit conversions between types are automatically performed whenever the conversion can be performed without the loss of data. For example:
Visual C#
int AnInteger = 100;
long aLong;
// Because a long can be assigned to every possible value of integer,
// there is no chance of losing data
aLong = anInteger;
Implicit Conversions in Visual Basic .NET and Visual C#
From To
Byte (Visual Basic .NET) Short, Integer, Long, Single, Double, Decimal
byte (Visual C#) short, ushort, int, uint, long, ulong, float, double, decimal
Short Integer, Long, Single, Double, Decimal
short int, long, float, double, decimal
Integer Long, Single, Double, Decimal
int long, float, double, decimal
Long Single, Double, Decimal
long float, double, decimal
Single Double
float Double
Char Integer, Long, Single, Double, Decimal
char Int, uint, long, ulong, float, double, decimal
sbyte (Visual C# only) short, int, long, float, double, decimal
ushort (Visual C# only) int, uint, long, ulong, float, double, decimal
uint (Visual C# only) long, ulong, float, double, decimal
ulong (Visual C# only) float, double, decimal
Explicit Conversions
When performing a conversion where types cannot be implicitly converted, we must explicitly convert the types. This conversion is called a cast. Explicit conversions are accomplished in Visual Basic .NET using the CType function and using a special syntax in Visual C#.
Visual C#
long aLong = 1000;
int anInteger;
anInteger = (int)aLong;

Friday, 24 June 2011

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.
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

Thursday, 23 June 2011

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 we will face. Most of the types presented by the .NET base class library are fully extensible, allowing we to build types that incorporate our own functionality into our managed code. In this lesson, we are introduced to some of the .NET base class library namespaces and learn how to reference the library and use its types and methods.
The .NET Framework base class library contains the base classes that provide many of the services and objects we need when writing our 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


Some Representative .NET Namespaces

Namespace Description
System:

This namespace is the root for many of the low-level types required by the .NET Framework, and is the root for primitive data types as well. This namespace is also the root for all the other namespaces in the .NET base class library.

System.Collections:

This namespace contains classes that represent a variety of different container types, such as ArrayList, SortedList, Queue, and Stack. We can also find abstract classes such as CollectionBase, which are useful for implementing our own collection functionality.

System.ComponentModel:

This namespace contains classes involved in component creation and containment, such as attributes, type converters, and license providers.

System.Data:

This namespace contains classes required for database access and manipulations, as well as additional namespaces used for data access.

System.Data.Common:

This namespace contains a set of classes that are shared by the .NET managed data providers.

System.Data.OleDb:

This namespace contains classes that make up the managed data provider for OLE DB data access.

System.Data.SqlClient:

In this namespace, we will find classes that are optimized for interacting with Microsoft SQL Server.

System.Drawing:

This namespace exposes GDI+ functionality and provides classes that facilitate graphics rendering.

System.IO

In this namespace, we will find types for handling file system IO.
System.Math This namespace is home to mathematics functions such as extracting roots, trigonometry functions, and other common mathematical functions.

System.Reflection

This namespace provides support for obtaining information and dynamic creation of types at run time.

System.Security:

This namespace is home to types dealing with permissions, cryptography, and code access security.

System.Threading

This namespace contains classes that facilitate the implementation of multithreaded applications.

System.Windows.Forms

This namespace contains types involved in creating standard windows applications. Classes that represent forms and controls reside here as well.

Tuesday, 21 June 2011

Compilation and Execution of a .NET Application

When we compile a .NET application, it is not compiled to binary machine code; rather, it is converted to IL, which is a low-level set of instructions understood by the common language run time. This is the form that our deployed application takes—one or more assemblies consisting of executable files and DLL files in IL form. At least one of these assemblies will contain an executable file that has been designated as the entry point for the application.
When execution of our program begins, the first assembly is loaded into memory. At this point, the common language run time examines the assembly manifest and determines the requirements to run the program. It examines security permissions requested by the assembly and compares them to the system's security policy. If the system's security policy does not allow the requested permissions, the application will not be run. If the application passes the system's security policy, the common language run time executes the code. It creates a process for the application to run in and begins application execution. When execution starts, the first bit of code that needs to be executed is loaded into memory and compiled into native binary code from IL by the common language run time's Just-In-Time (JIT) compiler. Once compiled, the code is executed and stored in memory as native code, so each portion of code is compiled only once during the execution of an application. Whenever program execution branches to code that have not yet been executed, the JIT compiler compiles it ahead of execution and stores it in memory as binary code. This way, application performance is maximized because only the parts of a program that are executed are compiled.

Monday, 20 June 2011

The Structure of a .NET Application

To understand how the common language run time manages the execution of code, we must examine the structure of a .NET application. The primary unit of a .NET application is the assembly. An assembly is a self-describing collection of code, resources, and metadata. The assembly manifest contains information about what is contained within the assembly. The assembly manifest provides
• Identity information, such as the name and version number of the assembly.
• A list of all types exposed by the assembly.
• A list of other assemblies required by the assembly.
• A list of code access security instructions for the assembly. This includes a list of permissions required by the assembly and permissions to be denied the assembly.

Each assembly has one and only one assembly manifest, and it contains all the description information for the assembly. The assembly manifest can be contained in its own separate file, or it can be contained within one of the assembly's modules.
An assembly also contains one or more modules. A module contains the code that makes up our application or library, and metadata that describes that code. When we compile a project into an assembly, our code is converted from high-level code to IL. Because all managed code is first converted to IL code, applications written in different languages can easily interact. For example, one developer might write an application in Visual C# that accesses a DLL in Visual Basic .NET. Both resources will be converted to IL modules before being executed, thus avoiding any language incompatibility issues.
Each module also contains a number of types. Types are templates that describe a set of data encapsulation and functionality. There are two kinds of types: reference types (classes) and value types (structures).Each type is described to the common language run time in the assembly manifest. A type can contain fields, properties, and methods, each of which should be related to a common functionality. For example, we might have a class that represents a bank account. It would contain fields, properties, and methods related to the functions needed to implement a bank account. A field represents storage of a particular type of data. We might have a field that stores the name of an account holder. Properties are similar to fields, but usually provide some kind of validation when the data is set or retrieved. We might have a property that represents the balance available in an account. When an attempt is made to change the value, the property could check to see if the attempted change was greater than a predetermined limit, and if so, could disallow the change. Methods represent behavior, such as actions taken on data stored within the class or changes to the user interface. Continuing with the bank account example, we might have a Transfer method that transfers a balance from a checking account to a savings account, or an Alert method that warns the user when his balance has fallen below a predetermined level.

Saturday, 18 June 2011

What is C#?


C# (pronounced "see sharp" or "C Sharp") is one of many .NET programming languages. It is object-oriented and allows you to build reusable components for a wide variety of application types.  Microsoft introduced C# on June 26th, 2000 and it became a v1.0 product on Feb 13th 2002.
C# is an evolution of the C and C++ family of languages. However, it borrows features from other programming languages, such as Delphi and Java. If you look at the most basic syntax of both C# and Java, the code looks very similar, but then again, the code looks a lot like C++ too, which is intentional. Developers often ask questions about why C# supports certain features or works in a certain way. The answer is often rooted in it's C++ heritage.

Friday, 17 June 2011

Overview of the .NET Framework


The .NET Framework is a managed, type-safe environment for application development and execution. The framework manages all aspects of the execution of our program: it allocates memory for the storage of data and instructions, grants or denies the appropriate permissions to our application, initiates and manages application execution, and manages the reallocation of memory for resources that are no longer needed. The .NET Framework consists of two main components: the common language runtime and the .NET Framework class library.
The common language runtime can be thought of as the environment that manages code execution. It provides core services, such as code compilation, memory allocation, thread management, and garbage collection. Through the common type system (CTS), it enforces strict type safety, and it ensures that code is executed in a safe environment by enforcing code access security.
The .NET Framework class library provides a collection of useful and reusable types that are designed to integrate with the common language runtime. The types provided by the .NET Framework are object-oriented and fully extensible, and allow we to seamlessly integrate our applications with the .NET Framework.