C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Using Conversion Operators


Posted Date: 11 Feb 2009    Resource Type: Articles    Category: .NET Framework
Author: Puja SharmaMember Level: Gold    
Rating: 1 out of 5Points: 20



Using Conversion Operators

A very common task is converting data from one form to another.
The new version of .NET has conversion operators ToArray, OfType, Cast, AsEnumerable, ToList, ToDictionary, and ToLookup.

We will discuss each one by one:

ToArray

Arrays are fast but not very convenient. It is tedious to write array management code when collections like List<T> make managing data easier.

The main reason, dot net framework supprots ToArray because many legacy code and API supprots uses ToArray till now.

OfType
The conversion operator OfType<T> returns an IEnumerable<T> collection of only the types defined by the parameter T.
For example, initializing a list of objects where some are integers, you can quickly extract just the integers


var numbers = new object[]{1, "two", null, "3", 4};
foreach(var anInt in numbers.OfType<int>())
Console.WriteLine(anInt);


Cast
To receive an exception if elements of the source type cannot be converted to the target type, use the Cast <T> conversion operator.

AsEnumerable

Many types implement IEnumerable.
AsEnumerable Forces an Object That Implements IEnumerable to Use theBehaviors
of the IEnumerable Interface

ToList
The ToList conversion operator forces an immediate query evaluation and stores the results in a List<T>.

ToDictionary
A dictionary is a collection of name and value pairs. ToDictionary converts an
IEnumerable<T> object—such as is returned from a LINQ query—into an
IDictionary<Key, Value> object.

ToLookup
ToLookup converts an IEnumerable<T> to a Lookup<Key, Element> type. Lookup is like a dictionary, but where a Dictionary uses a single key value, Lookup maps keys to a collection of values. Lookups have no public constructor and are immutable. You cannot add or remove elements or keys after they are created.

Summary
Compound type initialization makes it easier to initialize arrays and lists and is an essential capability for initializing anonymous types. Conversion operators take some of the tedium out of converting between convenient types and other types. More important, this chapter demonstrates the classic onion layering of complexity in .NET that makes it possible to do a lot of work with relatively few lines of code.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Using Conversion Operators  .  ToList  .  ToArray  .  Cast  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Introduction to LINQ-- Anonymous Types
Previous Resource: Composition and Aggregation in UML
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use