The Collections class is supposed to, among other things, return
type safe collections from existing collections with static methods
such as .checkedList().
My question is: What is so special about these static methods since
we can do the same thing by declaring the original list generically
with a type like <String>? I mean, once you create the list with
<String the compiler will no longer allow you to add, say,
Integer to the list. So what purpose does .checkedList() provide
in that context?
Maybe it's all about backward compatibility? 3 2549
jupiter wrote:
The Collections class is supposed to, among other things, return
type safe collections from existing collections with static methods
such as .checkedList().
My question is: What is so special about these static methods since
we can do the same thing by declaring the original list generically
with a type like <String>? I mean, once you create the list with
<String the compiler will no longer allow you to add, say,
Integer to the list. So what purpose does .checkedList() provide
in that context?
Maybe it's all about backward compatibility?
For the most part (where "most part" includes all of the collections
interface), all of the type constraints of generics are only checked at
compile time. The type-safe methods, e.g., checkedList, are guarantees
at /runtime/, something which generics can't do. Observe:
ArrayList<Strin gfoo = new ArrayList();
List bar = foo;
bar.add(5);
String s = foo.get(0).subs tring(4);
This passes the compiler (although the compiler does give a warning), so
the code will be compiled to bytecode where it promptly emits a
ClassCastExcept ion. Passing the list through to checkedList still gives
an error, but it is emitted at the point of modification as opposed to
the point of access (which, in some cases, might not even give an error!).
In short, it is mostly a backwards-compatible feature, but it is
desirable in circumstances, so it is in no way an anachronism.
"Joshua Cranmer" <Pi*******@epen guin.zzn.comwro te in message
news:F517i.6680 $3B1.1920@trndd c08...
jupiter wrote:
>The Collections class is supposed to, among other things, return type safe collections from existing collections with static methods such as .checkedList().
My question is: What is so special about these static methods since we can do the same thing by declaring the original list generically with a type like <String>? I mean, once you create the list with <String the compiler will no longer allow you to add, say, Integer to the list. So what purpose does .checkedList () provide in that context?
Maybe it's all about backward compatibility?
For the most part (where "most part" includes all of the
collections interface), all of the type constraints of generics
are only checked at compile time. The type-safe methods, e.g.,
checkedList, are guarantees at /runtime/, something which
generics can't do. Observe:
ArrayList<Strin gfoo = new ArrayList();
List bar = foo;
bar.add(5);
String s = foo.get(0).subs tring(4);
This passes the compiler (although the compiler does give a
warning), so the code will be compiled to bytecode where it
promptly emits a ClassCastExcept ion. Passing the list through to
checkedList still gives an error, but it is emitted at the point
of modification as opposed to the point of access (which, in some
cases, might not even give an error!).
In short, it is mostly a backwards-compatible feature, but it is
desirable in circumstances, so it is in no way an anachronism.
Great example, Joshua. Thanks.
I guess I was stuck thinking "compiler means everything now" but
when using references to do implicit casting, anything can happen
at runtime as you've clearly shown.
jupiter wrote:
>This passes the compiler (although the compiler does give a warning), so the code will be compiled to bytecode where it promptly emits a ClassCastExcept ion. Passing the list through to checkedList still gives an error, but it is emitted at the point of modification as opposed to the point of access (which, in some cases, might not even give an error!).
I guess I was stuck thinking "compiler means everything now" but
when using references to do implicit casting, anything can happen
at runtime as you've clearly shown.
This is true, but don't underestimate compiler warnings.
im This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mart |
last post by:
What class does everyone out there use if they want to store a set of values
efficiently?
In java I use a HashSet, but there is no equivalent in C#.
Even worse, the lowest level interface to define the Add, Contains and
Remove methods is an IList, and we all should know that a set isn't a list.
ICollection only defines Count and CopyTo,...
|
by: Howard Swope |
last post by:
Could someone help explain thread safety issues in the System.Collections
classes? The documentation states:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thread Safety
Public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Instance...
|
by: Ian Gore |
last post by:
Hi,
I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here...
1) Creating a strongly typed collection by inheriting CollectionBase.
This is covered in most of the books I've read. Basically you inherit CollectionBase and then add methods (say Add) that takes your types as a parameter and...
|
by: Paul |
last post by:
Hello,
I'm upgrading a small single user app to VB.NET and have a few questions
about loading classes & collections classes with the data from a one to many
dataset structure.
I'm serializing the data to disk as a dataset and was wondering if it was an
accepted practice to load/add/edit/delete the data in the dataset from
methods within...
|
by: ljlevend |
last post by:
I've noticed that in VS.NET 2.0 Beta 1 that none of the methods in
System.Collections.Generic.List are overridable. In my app I currently have
over 50 strongly typed ArrayLists that inherit from
System.Collections.ArrayList. Each of these types raise strongly typed
events for when items are added and removed (e.g., AddingItem, AddedItem,...
| |
by: Arthur Dent |
last post by:
How do you sort a generic collection derived from
System.Collections.ObjectModel.Collection?
Thanks in advance,
- Arthur Dent
|
by: Lars |
last post by:
Hi,
I have a base class holding a generic list that needs
to be accessed by both the base class and its subclasses.
What is the best solution to this?
I am fairly new to generics, but I am aware of that fact
that if you have a class B, that inherits from A, then
List<Bdoes NOT inherit from List<A>. So I understand
why the example below...
|
by: =?Utf-8?B?QW5kcmV3IEhheWVz?= |
last post by:
Maybe I'm missing a code snippet or something, but there doesn't seem to be a
way in VS2005 to create a class that is a collection of another class.
Something that was very easy to do in the old VB6 Class Builder.
I've tried using the Class Designer but while it handles many of the
refactoring needed for properties and methods, it doesn't...
|
by: Scott Stark |
last post by:
Hello,
I'm trying to get a better handle on OOP programming principles in VB.NET.
Forgive me if this question is sort of basic, but here's what I want to do.
I have a collection of Employee objects that I can iterate through
relatively easily. I've included code at the bottom of this message.
I can pretty easily iterate through my...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |