Solved

C# Strongly typed list

Posted on 2009-05-19
4
580 Views
Last Modified: 2013-12-17
Can anybody tell me what a strongly typed list is.

I am using C#

Thanks
0
Comment
Question by:Mr_Shaw
  • 2
4 Comments
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 150 total points
ID: 24421001
A strongly typed list is a list where the items have a specific type. As an example, List<string> is a list of string items.
0
 
LVL 39

Accepted Solution

by:
abel earned 350 total points
ID: 24421104
I second the answer provided by Shaun_Kline.

In older .NET enumerators and lists that needed to grab every possible type (a list for string, integers, boats or holidayhomes)  the only option was to use the object datatype as the base type, as that's the guaranteed base type for every class. The problem with this approach is that you would need to cast the elements back and forth and you loose your type safety (which is where the compiler warns you when you try to use a string as if it is an integer, more generic, whenever you assign on object to one of the wrong type).

Since .NET 2.0 we have generics. The syntax takes a little of getting used to. But once you understand it, it will make a lot of coding a lot easier and, more importantly, a lot safer to write and maintain. Many errors will be caught early and IntelliSense can help you with using the correct methods.
0
 
LVL 39

Assisted Solution

by:abel
abel earned 350 total points
ID: 24421167
Initializing and using a list is a breeze. For instance, to compare old and new:

List<string> listOfStrings = new List<string>();
listOfString.Add("new string to add");     // IntelliSense will tell you what to add here
 
List<DateTime> listOfDates = new List<DateTime>();
listOfDates.Add(DateTime.Now);
 
listOfDates.Add("10-24-2008");    // strong typing: this will raise an error
 
// old way:
IList listOfSomething = new ArrayList();
listOfSomething.Add("some sttring");    // no IntelliSense help
listOfSomething.Add(DateTime.Now);      // no IntelliSense help
 
// error after running code "Cannot convert DateTime to string"
// will not be visible during write/compiletime
string someString = listOfSomething[1]; 

Open in new window

0
 

Author Closing Comment

by:Mr_Shaw
ID: 31582950
Thanks
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where did System.Data.Objects go? 2 23
JQuery JavaScripot loop though RadioButtonList 4 25
ASP/VB email question 4 31
C# winforms programmitically move panels 6 28
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question