Monday, May 21, 2007

.NET Data Types

Different Data Types are as follows:
1. Types
a. Value Type
1. Built in Value Types
2. Enumerations
3. User-defined Value Types
b. Reference Type
1. Interface Types
2. Pointer Types
3. Self-describing Types

· Self-describing Types
· Class Types
i. Delegates
ii. Boxed Value Types
iii. User-defined Reference Types
· Arrays

Type - Base class that represents any type
Value Type - Base class that represents any value type
Reference Types - Any data types that are accessed through a reference and stored on the heap
Built-in Value Types - Includes most standard primitive types, which represent number, Boolean values, or characters
Enumerations - Sets of enumerated values
User-defined Value Types - Types that have been defined in source code and are stored as value types. In C#, this means any struct
Interface Types - Interfaces
Pointer Types - Pointers
Self-describing Types - Data Types that provide information about themselves for the benefit if the garbage collector.
Arrays - Any type that contain an array of objects
Class Types - Types that are self-describing but are not arrays
Delegates - Types that are designed to hold references to methods
User-defined Reference Types - Types that have been defined in source code and are sorted as reference types. In C# terms, this means any class.
Boxed Value Types - A value type that is temporarily wrapped in a reference so that it can be stored on the heap.


Distinct Value and Reference Types?
As with any programming language, IL provides a number of predefined primitive data types. One characteristic of IL, is that it makes a strong distinction between value and reference types.
1. Values Types: are those for which a variable directly stores data
2. Reference Types: are those for which variable simply stores the address at which the corresponding data can be found.

What is the difference between "Value Types" and "Reference Types"?
Many programming languages provide built-in data types such as integers and floating-point numbers. These are copied when they are passed in to arguments i.e. they are passed "By Value". In .NET terms, these are called Value Types".
The RunTime supports two kinds of Value Types:
1. 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 in programming languages.
2. User-defined value types: The language you are using will provide functionality to define your own Value Types. These user defined Types derive from System.ValueType. If you want to define a Type representing a value that is a complex number (two floating-point numbers), you might choose to define it as a value type. Why? Because you can pass the Value Type efficiently "By Value". If the Type you are defining could be more efficiently passed "By Reference", you should define it as a class instead. Variables of Reference Types are referred to as objects. These store references to the actual data.

The following are the Reference Types:
· class
· interface
· delegate
This following are the "built-in" Reference Types:
· object
· string

How do you convert a value type to a reference type?
Use Boxing.

What happens in memory when you Box and Unbox a value-type?
Boxing converts a value-type to a reference-type, thus storing the object on the heap. Unboxing converts a reference-type to a value-type, thus storing the value on the stack.

What is Boxing?
Encapsulating a copy of a value type in an object.

Where are the instances of reference types and the value types stored?
Instances of reference types are always stored in an area of memory known as the managed heap.
Value types are normally stored on the stack (although if value types are declared as fields within reference types they will be stored inline on the heap).

Strong Data Typing
One very important aspect of IL is that it is based on exceptionally strong data typing. That means all variables are clearly marked as being of a particular specific data type.

What is the importance of strong data typing for language interoperability?
If a class is to derive from or contains instances of other classes, it needs to know about all the data types used by other classes. This is why strong data typing is so important.

How are the data type problems solved in .NET?
The data type problem is solved in .NET through the use of the Common Type System (CTS). The CTS defines the predefined data types that are available in IL, so that all languages that target the .NET Framework will produce compiled code that is ultimately based on these types.

What is the difference between “Value Types” and “Reference Types”?
Many programming languages provide built-in data types such as integers and floating-point numbers. These are copied when they are passed in to arguments i.e. they are passed "By Value". In .NET terms, these are called Value Types".
The RunTime supports two kinds of Value Types:
1. 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 in programming languages.
2. User-defined value types
The language you are using will provide functionality to define your own Value Types. These user defined Types derive from System.ValueType. If you want to define a Type representing a value that is a complex number (two floating-point numbers), you might choose to define it as a value type. Why? Because you can pass the Value Type efficiently "By Value". If the Type you are defining could be more efficiently passed "By Reference", you should define it as a class instead. Variables of Reference Types are referred to as objects. These store references to the actual data.

The following are the Reference Types:
· class
· interface
· delegate
This following are the "built-in" Reference Types:
· object
· string

How to convert a string numeric value of an Enum to an enumerated object?
You can use the Parse method of the Enum object.
For Example, say this is the enum defined:enum Colors {Red=1, Green=2, Blue=4}
string mystring="Red";

Colors myEnum=(Colors)Enum.Parse(typeof(Colors),mystring);
Console.WriteLine("My Color is : {0}", myEnum.ToString());

How do I have an object notify me of any changes in real time?
Events provide a generally useful way for objects to signal state changes that may be useful to clients of that object. In order to support this, you need to create an event of type EventHandler with the name Changed. Then, in the property setter of the object, after you store the value (or do whatever you have to do to ensure that getting the value will return the new value), fire the event.
The event model in the .NET Framework is based on having an event delegate that connects an event with its handler. To raise an event, two elements are needed:
A class that holds the event data.
This class must derive from the base class EventArgs.
A delegate that points to a method that provides the response to the event.
Example:public delegate void EventHandler(Object sender, EventArgs e);public event EventHandler DataChangedEventHandler;

Does anyone have an example of how to create an ArrayList of objects?
It's like adding any other item to an ArrayList, and then retrieving that. For example, if you wanted to add a Hashtable to an ArrayList, then you would do this: // An array list. ArrayList arrayList = new ArrayList();

// Add hashtables.
for (int index = 0; index < 10; index++)
{
// Add a hashtable.
arrayList.Add(new Hashtable());
}

How do I get type information on an object?
You can get type information using reflection-
Reflection is the process by which a program can read its own metadata. A program is said to reflect on itself, extracting metadata from its assembly and using that metadata either to inform the user or to modify its own behavior.
All .NET assemblies contain type information which describes the structure of every internal and external type. Many methods in the .NET base class libraries require you to pass in type information for a given item. When you need to obtain type information for a given method invocation, you have numerous approaches.
First, all types inherit the public System.Object.GetType() method: // obtain type information from a variable.MyType c = new MyType();Type t = c.GetType();GetTypeInfo(t);
Or simply use: // obtain type information via typeof.GetTypeInfo(typeof(MyType));
0

No comments: