Sie sind auf Seite 1von 10

Microsoft.NET? .NET is a set of technologies designed to transform the internet into a full scale distributed platform.

It provides new ways of connecting systems, information and devices through a collection of web services. It also provides a language independent, consistent programming model across all tiers of an application. The goal of the .NET platform is to simplify web development by providing all of the tools and technologies that one needs to build distributed web applications.

.NET Framework? The .NET Framework is set of technologies that form an integral part of the .NET Platform. It is Microsoft's managed code programming model for building applications that have visually stunning user experiences, seamless and secure communication, and the ability to model a range of business processes.The .NET Framework has two main components: the common language runtime (CLR) and .NET Framework class library. The CLR is the foundation of the .NET framework and provides a common set of services for projects that act as building blocks to build up applications across all tiers. It simplifies development and provides a robust and simplified environment which provides common services to build application. The .NET framework class library is a collection of reusable types and exposes features of the runtime. It contains of a set of classes that is used to access common functionality.

CLR? The .NET Framework provides a runtime environment called the Common Language Runtime or CLR. The CLR can be compared to the Java Virtual Machine or JVM in Java. It is the runtime that converts a MSIL code into the host machine language code, which is then executed appropriately. The CLR is the execution engine for .NET Framework applications. CLR handles the execution of code and provides useful services for the implementation of the program. In addition to executing code, CLR provides services such as : ? Code management (loading and execution) ? Application memory isolation ? Verification of type safety ? Conversion of IL to native code. ? Access to metadata (enhanced type information) ? Managing memory for managed objects ? Enforcement of code access security

? Exception handling, including cross-language exceptions ? Interoperation between managed code, COM objects, and pre-existing DLL's (unmanaged code and data) ? Automation of object layout ? Support for developer services (profiling, debugging, and so on).

Metadata: Metadata is binary information describing your program that is stored either in a common language runtime portable executable (PE) file or in memory. When you compile your code into a PE file, metadata is inserted into one portion of the file, while your code is converted to Microsoft intermediate language (MSIL) and inserted into another portion of the file. Every type and member defined and referenced in a module or assembly is described within metadata. When code is executed, the runtime loads metadata into memory and references it to discover information about your code's classes, members, inheritance, and so on.

MSIL? When the code is compiled, the compiler translates your code into Microsoft intermediate language (MSIL). The common language runtime includes a JIT compiler for converting this MSIL then to native code. MSIL contains metadata. Since this metadata is standardized across all .NET languages, a program written in one language can understand the metadata and execute code, written in a different language. MSIL is similar to Java Byte code. MSIL is the CPU-independent instruction set into which .NET Framework programs are compiled. MSIL includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations.

JIT? JIT is a compiler that converts MSIL to native code. The native code consists of hardware specific instructions that can be executed by the CPU. Rather than converting the entire MSIL (in a portable executable[PE]file) to native code, the JIT converts the MSIL as it is needed during execution. This converted native code is stored so that it is accessible for subsequent calls.

portable executable (PE)? PE is the file format defining the structure that all executable files (EXE) and Dynamic Link Libraries (DLL) must use to allow them to be loaded and executed by Windows. PE is derived from the Microsoft Common Object File Format (COFF). The EXE and DLL files created using the .NET Framework obey the PE/COFF formats and also add additional header and data sections to the files that are only used by the CLR. Managed Code? The .NET Framework provides a run-time environment called the Common Language Runtime, which manages the execution of code and provides services that make the development process easier. Compilers and tools expose the runtime's functionality and enable you to write code that benefits from this managed execution environment. The code that runs within the common language runtime is called managed code.

CLS? Common Language Specification (CLS) defines the rules and standards to which languages must adhere to in order to be compatible with other .NET languages. This enables C# developers to inherit from classes defined in VB.NET or other .NET compatible languages.

CTS? Common Type System (CTS) describes all of the basic types that can be used in the .NET Framework and the operations performed on those type.CTS defines how these types are declared, used and managed in the runtime. It facilitates cross-language integration, type safety, and high performance code execution. The rules defined in CTS can be used to define your own classes and values. All this time we have been talking about language interoperability, and .NET Class Framework. None of this is possible without all the language sharing the same data types. What this means is that an int should mean the same in VB, VC++, C# and all other .NET compliant languages. This is achieved through introduction of Common Type System (CTS). how CLS (Common Language Specification) provides a foundation, which when adhered to by compiler vendors, allows them to write compilers compliant with the CLR (Common Language Runtime), and hence, be interoperable with other compliant compilers. This interoperability stems from the fact that CLS defines a set of data types, which all compliant compilers must provide. This set of data types forms the .Net CTS (Common Type System), which is a part of the CLS.

The CTS makes available a common set of data types so that compiled code of one language could easily interoperate with compiled code of another language by understanding each others? data types. All data types are objects in nature under CTS, and more importantly, they all derive from a common class, System.Object. Hence, all CTS data types derive from a common, most generic data type, called object. And since they all derive from one class, they share some common functionality, and one type can easily be converted to another. CTS divides the available data types into two categories. Value and reference types The CTS data types are categorized as either value types or reference types, depending on how they are created in the memory. The value types are constituted of the following:

Simple type, like integers, floats, doubles, char, byte, short, long, bool Structures Enums

Likewise, the reference types include the following:


Classes Interfaces (new to C#) Delegates (new to C#) Arrays Objects Strings To understand how the two categories differ in their creation, suppose, we perform the following assignment: int a = 2; Since a is a variable of the type int, which happens to be a value, we end up allocating space on the stack for the value type variable, and the assigned value, that is, 2 is stored there. Likewise, if we perform the following assignment, int b = a; we allocate another space on the stack for the variable b, storing the value 2 there. Thus, both memory locations, corresponding to the value type variables a and b contain the value 2. This boils down to the fact that all value types contain some data. Reference type allocations work differently. For instance, in the following assignment, string s = Nannu misses me.. but I dont!; Instead of the stack, memory is allocated from the heap. The assigned string is stored there and the memory address is stored in the reference type variable s. Thus, s doesnt contain the string, but it points, or refers, to the memory location which contains the assigned string.

When we write a statement as below in C#, int a = 2; What we are actually doing is telling the compiler that a is an object of the System.Int32 class. int is just an alias for the System.Int32 class.

Value type & reference types


Most programming languages provide built-in data types, such as integers and floating-point numbers, that are copied when they are passed as arguments (that is, they are passed by value). In the .NET Framework, these are called value types. The runtime supports two kinds of value types: Built-in value types The .NET Framework defines built-in value types, such as System.Int32 and System.Boolean, which correspond and are identical to primitive data types used by programming languages. User-defined value types Your language will provide ways to define your own value types, which derive from System.ValueType. If you want to define a type representing a value that is small, such as a complex number (using two floating-point numbers), you might choose to define it as a value type because you can pass the value type efficiently by value. If the type you are defining would be more efficiently passed by reference, you should define it as a class instead. Variables of reference types, referred to as objects, store references to the actual data. This following are the reference types: class interface delegate This following are the built-in reference types: object string

How to Declare Value Types


To use a type, you must first declare a symbol as an instance of that type. Value types have an implicit constructor, so declaring them instantiates the type automatically; you dont have to include the New keyword as you do with classes. The constructor assigns a default value (usually null or 0) to the new instance, but you should always explicitly initialize the variable within the declaration, as shown in the following code block:
' VB Dim b As Boolean = False // C# bool b = false;

C# is case-sensitive, but Visual Basic is not case-sensitive. Traditionally, variable names begin with a lowercase letter in C# and are capitalized in Visual Basic. For consistency between the languages, this book will use lowercase variable names for most Visual Basic examples. Feel free to capitalize Visual Basic variables in your own codeit will not affect how the runtime processes your code.

How to Create User-Defined Types


User-defined types are also called structures or simply structs, after the language keyword used to create them. As with other value types, instances of user-defined types are stored on the stack and they contain their data directly. In most other ways, structures behave nearly identical to classes. Structures are a composite of other types that make it easier to work with related data.
using System;

class Program { struct Cycle { // Private fields int _val, _min, _max; // Constructor public Cycle(int min, int max) { _val = min; _min = min; _max = max; } public int Value { get { return _val; } set { if (value > _max)

else {

_val = _min; if (value < _min) _val = _max; else _val = value;

} }

} public override string ToString() { return Value.ToString(); } public int ToInteger() { return Value; } // Operators (new in .NET 2.0) public static Cycle operator +(Cycle arg1, int arg2) { arg1.Value += arg2; return arg1; } public static Cycle operator -(Cycle arg1, int arg2) { arg1.Value -= arg2; return arg1; } static void Main(string[] args) { Cycle degrees = new Cycle(0, 359); Cycle quarters = new Cycle(1, 4); for (int i = 0; i <= 8; i++) { degrees += 90; quarters += 1; Console.WriteLine("degrees = {0}, quarters = {1}", degrees, quarters); } } } }

How to Create Enumerations


Enumerations are related symbols that have fixed values. Use enumerations to provide a list of choices for developers using your class. For example, the following enumeration contains a set of titles:

' VB Enum Titles As Integer Mr Ms Mrs Dr End Enum

' VB Dim t As Titles = Titles.Dr Console.WriteLine("{0}.", t) ' Displays "Dr."

//C# using System; class Program { enum Titles : int { Mr, Ms, Mrs, Dr }; static void Main(string[] args) { Titles t = Titles.Dr; Console.WriteLine("{0}.", t); // Displays "Dr." } }

The purpose of enumerations is to simplify coding and improve code readability by enabling you to use meaningful symbols instead of simple numeric values. Use enumerations when developers consuming your types must choose from a limited set of choices for a value.

What Is a Reference Type?


Reference types store the address of their data, also known as a pointer, on the stack. The actual data that address refers to is stored in an area of memory called the heap. The runtime manages the memory used by the heap through a process called garbage collection. Garbage collection recovers memory periodically as needed by disposing of items that are no longer referenced.

BEST PRACTICES Garbage collection

Garbage collection occurs only when needed or when triggered by a call to GC.Collect. Automatic garbage collection is optimized for applications where most instances are short-lived, except for those allocated at the beginning of the application. Following that design pattern will result in the best performance.

Built-in Reference Types


There are about 2500 built-in reference types in the .NET Framework. Everything not

derived from System.ValueType is a reference type, including these 2500 or so builtin reference types. Table 1-3 lists the most commonly used types, from which many other reference types are derived. Type System.Object Use for System.Object The Object type is the most general type in the Framework. You can convert any type to System.Object, and you can rely on any type having ToString, GetType, and Equals members inherited from this type. System.String Text data. Dynamic text data.

System.Text.StringBuilder

System.Array Arrays of data. This is the base class for all arrays. Array declarations use language-specific array syntax.

System.IO.Stream Buffer for file, device, and network I/O. This is an abstract base class; task-specific classes are derived from Stream. System.Exception Handling system and application-defined exceptions. Taskspecific exceptions inherit from this type.

Reference types contain the address of data rather than the actual data. When you copy a value type, a second copy of the value is created. When you copy a reference type, only the pointer is copied. Therefore, if you copy a reference type and then modify the copy, both the copy and the original variables are changed. The .NET Framework includes a large number of built-in reference types that you can use directly or use to build your own custom types. Strings are immutable; use the StringBuilder class to create a string dynamically. Use streams to read from and write to files, memory, and the network.

Garbage Collector (GC) Works?

The methods in this class influence when an object is garbage collected and when resources allocated by an object are released. Properties in this class provide information about the total amount of memory available in the system and the age

category, or generation, of memory allocated to an object. Periodically, the garbage collector performs garbage collection to reclaim memory allocated to objects for which there are no valid references. Garbage collection happens automatically when a request for memory cannot be satisfied using available free memory. Alternatively, an application can force garbage collection using the Collect method. Garbage collection consists of the following steps:
The garbage collector searches for managed objects that are referenced in managed code. The garbage collector attempts to finalize objects that are not referenced. The garbage collector frees objects that are not referenced and reclaims their memory

Das könnte Ihnen auch gefallen