C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Use Of Generics In Dot Net Frame


Posted Date: 11 Oct 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Sachin Arora       Member Level: Silver
Rating:     Points: 4



Use Of Generics In Dot Net Frame

The purpose of Generics is introduce the Compiler with the type of Collection., so that the compiler can verify at Compile Time that Type Constraints are not violated at runtime.


Advantages Of Using Generics

1. Using generics in your applications makes it Clearer and Safer.
2. It eliminates unsafe Cast and extra parantheses.


Take the example to make it more clear

I am showing the same functionality with and without generics.
In this problem we have to remove all the elements of length from the collection of elements

(1.)Without Generics.

Static void nongenerics( collection c)
{
for (Iterator i = c.iterator(); i.hasNext(); )
if (((String) i.next()).length() == 4)
i.remove();
}

(2.)Let us apply generics to the same above code

Static void genenerics( Collection c)
{
for (iterator i=c.iterator( );i.hasnext( );)
if(i.next( ).length==4)
i.remove();
}


The Collection in code 2. above is read as Collection Of Type c. Now, (Using Generics) the compiler can verify at compile time that constraints are not violated at runtime. Also, the variable c holds true whenever and wherever it is used and the compiler guarantees it. Cast on the other hand shows what the programmer thinks is true at the single point in the 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  
Use of generics in Dot net Framewrk  .  How to make code clearer using Generics  .  Generics  .  

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: Files used in ASP.NET Application
Previous Resource: Differrence between Array.CopyTo and Array.Clone
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

conference call

Contact Us    Privacy Policy    Terms Of Use