RSS

DOTNET

The .NET Framework is a managed type-safe environment for application development and execution. The .NET Framework manages all aspects of your program’s execution. It allocates memory for the storage of data and instructions, grants or denies the appropriate permissions to your application, initiates and manages application execution, and manages the reallocation of memory from resources that are no longer needed. The .NET Framework consists of two main components: the common language runtime (CLR)[equivalent to JRE] and the .NET Framework class library.

CLR : --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 ensures that code is executed in a safe environment by also enforcing code access security.

The .NET Framework class library provides a collection of useful and reusable types that are designed to integrate with the CLR. The types provided by the .NET Framework are object-oriented and fully extensible.

The .NET Framework is designed for cross-language compatibility, which means, simply, that .NET components can interact with each other no matter what supported language they were written in originally. So, an application written in Microsoft Visual Basic .NET might reference a dynamic-link library (DLL) file written in Microsoft Visual C#.

This level of cross-language compatibility is possible because of the CLR. When a .NET application is compiled, it is converted from the language in which it was written (Visual Basic .NET, C#, or any other .NET-compliant language) to Microsoft Intermediate Language (MSIL or IL)[equivalent to bytecode]. MSIL is a low-level language that the CLR can read and understand. Because all .NET executables and DLLs exist as MSIL, they can freely interoperate.
The Common Language Specification (CLS) defines the minimum standards to which .NET language compilers must conform.

The CTS ensures type compatibility between .NET components. Because .NET applications are converted to IL prior to deployment and execution, all primitive data types are represented as .NET types. Thus, a Visual Basic Integer and a C# int are both represented in IL code as a System.Int32. Because both languages use a CTS, it is possible to transfer data between components and avoid time-consuming conversions or hard-to-find errors.

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. An assembly contains one or more modules. A module contains the code that makes up your application or library, and it contains metadata that describes that code. When you compile a project into an assembly, your 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. 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 CLR in the assembly manifest. A type can contain fields, properties, and methods, each of which should be related to a common functionality. For example, bank account(class).A field represents storage of a particular type of data. Properties are similar to fields, but properties usually provide some kind of validation when data is set or retrieved. Methods represent behavior, such as actions taken on data stored within the class or changes to the user interface.

Compilation and Execution of a .NET Application
------------------------------------------------------------
When you compile a .NET application, it is not compiled to binary machine code; rather, it is converted to IL. This is the form that your 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 your program begins, the first assembly is loaded into memory. At this point, the CLR examines the assembly manifest and determines the requirements to run the program. It examines security permissions requested by the assembly and compares them with the system’s security policy. If the system’s security policy does not allow the requested permissions, the application will not run. If the application passes the system’s security policy, the CLR 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 CLR's Just-In-Time (JIT) compiler. Once compiled, the code is executed and stored in memory as native code. Thus, each portion of code is compiled only once when an application executes. Whenever program execution branches to code that has not yet run, 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.

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(INHERITANCE), 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[PACKAGES].
A namespace is a logical grouping of types that perform related functions. For example, the System.Windows.Forms namespace contains all the types that make up Windows forms and the controls used in those forms.
Namespaces are logical groupings of related classes. The root of the .NET Framework is the System namespace[like java in java.lang.*]. Other namespaces can be accessed with the period operator.


System: This namespace is the root for many of the low-level types required by the .NET Framework. It is the root for primitive data types as well, and it is 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. You also can find abstract classes, such as CollectionBase, which are useful for implementing your 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: This namespace contains 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, you will find types for handling file system I/O.
System.Math: This namespace is home to common mathematics functions such as extracting roots and trigonometry.
System.Reflection: This namespace provides support for obtaining information and dynamic creation of types at runtime.
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.


Reference Types and Value Types

Application data memory is divided into two primary components, the stack and the heap. The stack is an area of memory reserved by the application to run the program. The heap, on the other hand, is a separate area of memory reserved for the creation of reusable objects. The CLR manages allocation of heap memory for objects and controls the reclamation of memory from unused objects through garbage collection.

All the data associated with a value type(STRUCTURE)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(CLASS), 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 to which it refers. When that variable goes out of scope, the object reference 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, 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 types such as Structure (struct) and Enumeration (enum). Classes represent the majority of reference types. Other reference types include the interface, delegate, and array types.
Using Value Type and Reference Type Variables
A variable that represents a value type contains all the data represented by that type. A variable that represents a reference type contains a reference to a particular object.

The Imports and Using Statements
Visual Basic .NET
Imports System.Windows.Forms
Visual C#
using System.Windows.Forms;

Members
Classes describe the properties and behaviors of the objects they represent through members-- methods, fields, properties, and events.
Fields and properties represent the data about an object—the color of the car.
A method represents something the object can do, ---turn on headlights.
An event represents something interesting that happens to the object, such as overheating or crashing.

Classes vs. Structures
Both can contain members such as fields and methods.
Both require a constructor to create a new instance of themselves
Both inherit from Object.
The key difference between classes and structures is that classes are reference types and structures are value types. On a low level, this means that the instance data for classes is allocated on the heap, whereas the instance data for structures is allocated on the stack. Access to the stack is designed to be light and fast, but storage of large amounts of data on the stack can impede overall application performance.
Structures are best used for smaller, lightweight objects that contain relatively little instance data or for objects that do not persist for long.
Classes are best used for larger objects that contain more instance data and are expected to exist in memory for extended periods.

Methods
(Sub Procedures)
Public Sub MySub()
MessageBox.Show("WELCOME")
End Sub
(Functions)
Public Function Add(ByVal first as Integer, ByVal second as Integer) As Integer
Dim Result as Integer
Result = first + second
Return Result
End Function

Variables declared within methods are said to have method scope(local).
Variables that are not destroyed after a method finishes execution---static method variables, persist in memory and retain their values through multiple executions of a method.

Public Sub myMethod()
Static Iterations as Integer
Iterations += 1
End Sub

Parameters
A parameter is an argument that is passed to the method by the method that calls it. Parameters can be passed in two ways, by value(default) or by reference. ByVal / ByRef

Optional Parameters(default Parameters)
In Visual Basic .NET, you are able to specify optional parameters for your methods. This feature is not available in Visual C#. You specify a parameter as optional using the Optional keyword. Optional parameters must be the last parameters in a method declaration, and you must supply default values for optional parameters.
Public Sub Cook(ByVal time As Integer, Optional ByVal temp As Integer = 350)

End Sub

Constructors and Destructors
The constructor is the first method that is run when an instance of a type is created. In Visual Basic, the constructor is always Sub New. Constructors can never return a value and can be overridden to provide custom initialization functionality. A constructor can also contain calls to other methods.
Public Class A
Public Sub New()

End Sub
End Class
A destructor (known as a finalizer in Visual Basic) contains code to “clean up” when a class is destroyed. This cleanup might include decrementing counters or releasing resources. A finalizer in Visual Basic .NET is always Sub Finalize().

Public Class A
Protected Overrides Sub Finalize()

End Sub
End Class

Scope and Access Levels
Access levels define how types are instantiated and how members are accessed.
Access modifiers are keywords such as Public (public), Private (private), and Friend (internal) that precede a variable or type declaration. The keyword that is used controls the level of access the member is allowed. When a modifier precedes a type declaration, it determines both the scope of its members and how that type is instanced.
Access Modifier Effect on Members
Public (Visual Basic .NET), public (Visual C#) Can be accessed from anywhere.
Private (Visual Basic .NET), private (Visual C#) Can be accessed only by members within the type that defines it.
Friend (Visual Basic .NET), internal (Visual C#) Can be accessed from all types within the assembly, but not from outside the assembly.
Protected (Visual Basic .NET), protected (Visual C#) Can be accessed only by members within the type that defines it or types that inherit from that type.
Protected Friend (Visual Basic .NET), protected ¬internal (Visual C#) Can be accessed from all types within the assembly or from types inheriting from the owning type. This is the union of Protected (protected) and Friend (internal) access.
Any member with the
Public (public) modifier is visible to all code outside the class. Thus, other objects can access and modify public fields and can call public methods.
Private (private) methods are visible only inside the type to which they belong and cannot be accessed from the outside.
Friend (internal), indicates that members can be accessed by other types in the same assembly but cannot be accessed from types outside the assembly.
The Protected (protected) modifier allows access from within the type to which the member belongs and to any types that inherit that type.
The Protected Friend (protected internal) level provides the union of Protected (protected) and Friend (internal) access.
For member variables, the access modifier can replace the Dim statement. If the Dim statement is used (in Visual Basic .NET) or no access modifier is used (in Visual C#), the variable is
private in Visual C# and Visual Basic .NET classes,
public in Visual Basic .NET structures, and
private in Visual C# structures.
If no access modifier is specified, the method is
private (private) by default in a class or structure in C#,
public (public) in a class or structure in Visual Basic .NET.

The garbage collector continuously traces the reference tree in the background and identifies objects that no longer have references. When it finds one, it deletes it and reclaims the memory.
The garbage collector is a low-priority thread under normal circumstances. It operates when processor time is not consumed by more important tasks. When memory becomes limited, however, the garbage collector thread moves up in priority. Memory is reclaimed at a more rapid pace until it is no longer limited, at which point the priority of garbage collection is again lowered.


To add a form to your application at run time
'This example assumes that you have already designed a form called DialogForm
Dim myForm As DialogForm
myForm = New DialogForm()
To create an inherited form with the Inheritance Picker
1. From the Projects menu, select Add Inherited Form. The Add New Item dialog box opens.
2. In the left pane of the dialog box, choose Local Project Items. In the right pane, select Inherited Form. Name this form in the Name box, and click Open to open the Inheritance Picker.
3. The forms in your project are displayed in the Inheritance Picker. If the form from which you want to inherit is one of these forms, choose it and click OK. A new inherited form is added to your project.
If you want to inherit from a form outside of your project, click Browse. Navigate to the project containing the form you want. Click the DLL file containing the form, and click Open.
To create an inherited form in code
'This example assumes that you are inheriting from a form class named MainForm and that that form resides in your project
Public Class myForm
Inherits MainForm
' Additional class implementation omitted
End Class
Opacity
You can create striking visual effects for your form by altering its transparency with the Opacity property controls. Opacity values range between 0 and 1. A value of 1 indicates that a form is completely opaque, and a value of 0 creates a completely transparent form. Any value between the two settings results in a partially transparent form. An Opacity of 1 (fully opaque) is the default value. The Opacity property is useful when it is necessary to keep one form in the foreground but monitor action in a background form. Generally, a control inherits the opacity of the form that hosts it(parent).
MyForm.Opacity = .5

Form Lifetime Events
Load, Activated/Deactivate, VisibleChanged, Closing, Closed
The Load event is fired when an instance of a form is first loaded into the program. This event is raised the first time that the Form.Show or Form.ShowDialog method is called for each instance of a form.
Activated is raised whenever the form receives the focus.
The Deactivate event is raised whenever the current form loses the focus.
VisibleChanged event is raised the form is made visible or invisible. The form methods that cause this event to be raised include Form.Show, Form.ShowDialog, Form.Hide, and Form.Close.
The Closing event is raised when the current form is in the process of closing but has not yet fully closed.
The Closed event is raised after a form has been closed. Use the Closed event to provide any necessary clean-up code.

The .NET Data Types
They are value types and can be broken down into the following subcategories: Integer types, floating-point types, the Boolean type, and the Char type. Two built-in reference types, the String type and the Object type, are an integral part of your application.
Integer Types
Type Visual C# Name Visual Basic .NET Name Description Range
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
Type Visual C# Name Visual Basic .NET Name Description Precision Range (Approximate)
System.Single float Single 32-bit floating-point variable 7 significant digits +/- 1.4 x 10-45 to +/-3.4 x 1038
System.Double double Double 64-bit floating-point variable 15–16 significant digits +/- 5.0 x 10-324 to +/-1.7 x 10308
System.Decimal decimal Decimal 128-bit floating-point variable 28 significant digits +/- 1.0 x 10-28 to +/- 7.9 x 1028
System.Boolean
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 Basic .NET and char in Visual C#.
System.String
is a reference type that represents a series of Char data types.
System.Object
is the supertype of all types in the .NET Framework. Every type, whether value type or reference type, derives from System.Object.
Implicit Conversions
From To
Byte (VB .NET)
byte (Visual C#) Short, Integer, Long, Single, Double, Decimal
short, ushort, int, uint, long, ulong, float, double, decimal
Short
short Integer, Long, Single, Double, Decimal
int, long, float, double, decimal
Integer
int Long, Single, Double, Decimal
long, float, double, decimal
Long
long Single, Double, Decimal
float, double, decimal
Single
float Double
double
Char
char Integer, Long, Single, Double, Decimal
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 - CType
Dim aLong As Long = 1000
Dim anInteger As Integer
anInteger = CType(aLong, Integer)

Dim anInteger As Integer = 100000
Dim aShort As Short
aShort = CType(anInteger, Short)
Option Strict in Visual Basic .NET
When Option Strict is on, strong typing is enforced and only conversions that can be performed without loss of data are allowed. When Option Strict is off, however, strong typing is not enforced and all conversions are performed implicitly. The default setting is Option Strict Off. (The primary purpose of Option Strict Off is backward compatibility with previous versions of Visual Basic.)
Option Strict On---to set it on

Using Data Type Functionality
All data types have built-in functionality. The methods are :
Equals.
Determines whether two instances are equal
GetHashCode.
Serves as a hash function for a particular type
GetType.
Returns the type object for the current instance
ToString.
Returns a human-readable form of the object
Boxing
Boxing is the implicit conversion of value types to reference types. All classes and types derive from Object, and each of these four methods is a method of the Object class. Because each class derives from Object, each class can be implicitly converted to that type. When you call one of these methods, the CLR creates a temporary reference for your value type variable and allows you to treat it as a reference type.
Dim I As Integer = 100
Dim O As Object
O = I
Parse
All of the value data types implement a Parse method. Parse is used to create a numeric value from a string. Controls that accept user input, such as TextBox, do so in the form of a string. The Parse method can be used to convert that string to usable data. In all implementations, Parse is a static (Shared) method and must be called from the type object rather than from an instance.
Dim I As Integer
Dim S As String
S = "1234"
I = Integer.Parse(S)
String Functions
Some Useful Instance String Methods
Name Description
String.Insert Inserts a specified string into the current instance
String.PadLeft, String.PadRight Adds characters to the left and right of the string, respectively
String.Remove Deletes a specified number of characters from the string, beginning at a specified character
String.Replace Replaces all occurrences of a specified character in the string with another specified character
String.Split Returns an array of substrings that are delimited by a specified character
String.Substring Returns a substring from the specified instance
String.ToCharArray Returns an array of the characters that make up the string
String.ToLower, String.ToUpper Returns the string converted to all lowercase or all uppercase, respectively
String.TrimEnd, String.TrimStart, String.Trim Removes trailing, leading, or both characters from the string, respectively

Some Useful Static (Shared) String Methods
Name Description
String.Compare Compares two specified string objects
String.Concat Returns a string that is the result of the concatenation of two or more strings
String.Format Returns a string that has been formatted according to a specified format
String.Join Returns a string that is the result of the concatenation of a specified array of strings, with a specified separation string between each member of the array

Constants allow you to assign user-friendly names to these values and refer to them in code.
Public Const Pi As Double = 3.14159265
Enumerations are user-defined sets of integral constants that correspond to a set of friendly names.
=>Public Enum DaysOfWeek
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
End Enum
=>Public Sub ScheduleDayOff(ByVal day As DaysOfWeek)
Select Case day
Case DaysOfWeek.Monday
' Implementation code omitted
Case DaysOfWeek.Tuesday
' Implementation code omitted
' Additional cases omitted
End Select
End Sub

=>Public Enum Numbers
zero ' equals zero
one ' equals one
two ' equals two
End Enum
=>MessageBox.Show((Numbers.two * 2).ToString()) 'Displays 4
Arrays
=>' This line declares and initializes an array of 33 integers, with indexes ranging from 0 to 32
Dim myIntegers(32) As Integer

=>' This line declares the array
Dim myIntegers() As Integer
' This line initializes the array to six members and sets their values
myIntegers = New Integer() {0,1,2,3,4,5}

=>Dim myIntegers(32) As Integer
' This line reinitializes the array
ReDim myIntegers(45)

=>' Declares the array and initializes it with four members
Dim myIntegers() As Integer = {0,1,2,3}
' Resizes the array, but retains the data in elements 0 through 3
ReDim Preserve myIntegers(10)

Multidimensional Arrays
Rectangular Arrays
-- all the rows have the same number of columns.

' Declares an array of 5 by 3 members
Dim intArrays(4, 2) As Integer
' Declares a two-dimensional array and sets initial values
Dim intArrays2( , ) As Integer = {{1, 2, 3}, {4, 5, 6}}
' Declares a cubical array and sets initial values
Dim cubeArray( , , ) As Integer = {{{7, 2}, {1, 4}}, {{3, 5}, {4, 4}}}
' Declares a complex array of 3 x 3 x 4 x 5 x 6 members
Dim complexArray(2, 2, 3, 4, 5) As Integer

Jagged Arrays
A two-dimensional jagged array can be thought of as a table where each row can have a different number of columns. A jagged array is really an array of arrays.

' Declares an array of 3 arrays
Dim Families(2)() As String
' Initializes the first array to 4 members and sets values
Families(0) = New String() {"Smith", "Mom", "Dad", "Uncle Phil"}
' Initializes the second array to 5 members and sets values
Families(1) = New String() {"Jones", "Mom", "Dad", "Suzie", "Little Bobby"}
' Initializes the third array to 3 members and sets values
Families(2) = New String() {"Williams", "Earl", "Bob"}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comments:

Post a Comment