B
beginwithl
hi
1) Do we consider a collection to be strongly typed only if the
insertion of an item ( that is of incompatible type ) can be
determined at compile time?
For example: we could build a non generic custom collection that
allows only items of type A to be inserted by the user.
But what if this custom collection would only check at run time
( perhaps using try-catch clause ) if inserted item is of specified
type?
Do we still call this collection strongly typed?
2) Assume class B is derived from A
If a collection implements IEnumerator<T> interface, the ‘Current’
property returns an item of type T ( or type derived from T ), and
thus casting to actual type is not needed. That is considered strongly
typed code.
a) But explicit casting is still needed if T is of type A, but foreach
variable Variab is of type B!
foreach( B Variab in our_Collection ) {…}
b) and if it happens that an item in this collection, which foreach
will try to cast to B ( in order to assign it to Variab ), is actually
of type A, an exception will occur!
Thus, I don’t see how we can claim that enumerator is strongly-typed?!
2) Next question is basically the same as previous one, but now I’m
asking about generics and strong typing in general:
Say we have a generic collection of type Some_Colection<A>, which only
stores items of type A or those derived from A. If we have classes of
type B and C ( both being derived from A ), then we may store items of
both types into some_collection<A>.
But again, chances are a user may try to do some sort of operations on
item stored in this collection, thinking it is of type B but is really
of type C and as a result an exception will be thrown. In other
words, we did manage to ensure type safety when dealing with types
not_of_type_A/not_derived_from_A, but we are still dealing with unsafe
code when it comes to operating on items ( stored inside this
collection ) derived from A?!
3)
a) Say we have a collection that implemented IEnumerable<string>. How
does foreach statement figure out that this particular collection
implemented IEnumerable<string> and not, for example, IEnumerable<int>
or whatever?
b) If a collection implements IEnumerable<string>, then object
returned by GetEnumerator() will implement two versions of a ‘Current’
member. Will in that case foreach statement always call the
IEnumerator<string> version of ‘Current’ property?
c) Since both IEnumerator<T> and IEnumerator have a member named
Current, my book suggests that we should explicitly implement
IEnumerator<T> , while IEnumerator version should be implemented in
the class itself, but I found some other sources that suggest just the
opposite. So which is correct?
thank you
1) Do we consider a collection to be strongly typed only if the
insertion of an item ( that is of incompatible type ) can be
determined at compile time?
For example: we could build a non generic custom collection that
allows only items of type A to be inserted by the user.
But what if this custom collection would only check at run time
( perhaps using try-catch clause ) if inserted item is of specified
type?
Do we still call this collection strongly typed?
2) Assume class B is derived from A
If a collection implements IEnumerator<T> interface, the ‘Current’
property returns an item of type T ( or type derived from T ), and
thus casting to actual type is not needed. That is considered strongly
typed code.
a) But explicit casting is still needed if T is of type A, but foreach
variable Variab is of type B!
foreach( B Variab in our_Collection ) {…}
b) and if it happens that an item in this collection, which foreach
will try to cast to B ( in order to assign it to Variab ), is actually
of type A, an exception will occur!
Thus, I don’t see how we can claim that enumerator is strongly-typed?!
2) Next question is basically the same as previous one, but now I’m
asking about generics and strong typing in general:
Say we have a generic collection of type Some_Colection<A>, which only
stores items of type A or those derived from A. If we have classes of
type B and C ( both being derived from A ), then we may store items of
both types into some_collection<A>.
But again, chances are a user may try to do some sort of operations on
item stored in this collection, thinking it is of type B but is really
of type C and as a result an exception will be thrown. In other
words, we did manage to ensure type safety when dealing with types
not_of_type_A/not_derived_from_A, but we are still dealing with unsafe
code when it comes to operating on items ( stored inside this
collection ) derived from A?!
3)
a) Say we have a collection that implemented IEnumerable<string>. How
does foreach statement figure out that this particular collection
implemented IEnumerable<string> and not, for example, IEnumerable<int>
or whatever?
b) If a collection implements IEnumerable<string>, then object
returned by GetEnumerator() will implement two versions of a ‘Current’
member. Will in that case foreach statement always call the
IEnumerator<string> version of ‘Current’ property?
c) Since both IEnumerator<T> and IEnumerator have a member named
Current, my book suggests that we should explicitly implement
IEnumerator<T> , while IEnumerator version should be implemented in
the class itself, but I found some other sources that suggest just the
opposite. So which is correct?
thank you