What is Generic? explain clearly?

Answer Posted / mithun

By using a generic class, we can create classes that is type safe at compile time.

ArrayList is a highly convenient collection class that can be used without modification to store any reference or value type. Any reference or value type that is added to an ArrayList is implicitly upcast to Object. If the items are value types, they must be boxed when they are added to the list, and unboxed when they are retrieved. Both the casting and the boxing and unboxing operations decrease performance

ArrayList list = new ArrayList();
list.Add(3);
list.Add("It is raining in Redmond.");
int t = 0;
foreach (int x in list)
{
t += x;
}

This will cause a programming error, and this error will not be detected until runtime.


In the generic List<T> collection, the same operation of adding items to the collection
List<int> list1 = new List<int>();
list1.Add(3);
list1.Add("It is raining in Redmond."); // this gives Compile-time error.

Now you can create a list that is not only safer than ArrayList, but also significantly faster, especially when the list items are value types.

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How does http session work?

525


What are the new features added from ASP to ASP.NET?

573


Which adapter should you use, if you want to get the data from an access database?

544


When using the Pager object, inorder to know which page to go, which property you have to set to grid?

567


How does session id work?

494






Explain repository pattern in asp.net mvc? : asp.net mvc

532


What is the mvc framework?

568


How will create assesblies at run time?

558


Where is the view state data stored?

578


Explain security types in asp.net?

545


How we implement web farm and web garden concept in asp.net?

544


What is the use of global.asax file?

553


Is session stored in browser?

532


What is the difference between cache and cookies?

520


How would you get asp.net running in apache web servers?

546