2nd lecture -- collections and streams
Slides
Source codes
- SparseListExample.zip
- ArraysExample.cs
- CollectionsExample.cs
- CompressionTool.cs
- Iteration.cs
- StreamWriterExample.cs
Homework assignment
Download SparseListExample.zip. Implement the two missing methods and indexer so that they behave as required by IList<T>
interface. (You can ignore all the other functions, but you are of course welcome to implement them as a nice programming exercise.). You must implement the inner implementation of the SparseList<T> class as it was described in the seminar (see the slides).
Then go to the Program
class and implement the static method WriteToTextFile
with two arguments, one of type IList<T>
, second of type string
. Return type of the method will be void
. The method will create a file of the name given by the string. The data stored in this files will be strings returned by ToString
method of each of the elements of the list, separated by a new line character. E.g. for a list of integers containing elements 2,31,12,43, the method will write the following text to the file:
2
31
12
43
Create a documentation-style comments for the WriteToTextFile
method using XML documentation comments we have seen in the first seminar. The documentation for the other methods is not mandatory, but you are of course still required to properly comment your code.
Note: Do not forget to treat the exceptions correctly: In SparseList<T>, make sure you throw the exceptions as described in the documentation (hint: for indexer documentation, look for Item
in IList<T>
documentation). In WriteToTextFile
method, catch the exceptions and document all thrown exceptions using XML documentation comments.
If you have any questions, if you find an error in the assignment or if you find the assignment unclear, use discussion forums.
Notes, corrections etc.
- Hashtable stores its elements based on the hash of the key, but it indeed allows more than one key with same hash to be stored (as someone of you correctly pointed out).
- Someone at the seminar asked me whether it is possible to "override" behaviour of
default
keyword. It is exactly as I claimed,default
always returns null for reference types. - I incorrectly claimed that arrays do not derive from
System.Array
class. They do inherit from this class, however, you cannot derive fromSystem.Array
yourself, only compiler and system can do this. - Remarks on the following piece of code:
1: class MyClass: IEnumerable<T>
2: {
3: public IEnumerator<T> GetEnumerator()
4: {
5: //some code
6: {
7:
8: IEnumerator IEnumerate.GetEnumerator()
9: {
10: //some code
11: }
12: }- Method at line 3 must be explicitly specified as
public
. Without any access modifier, it would be private, which is not possible as the methods implementing an interface must be public. - One can not use
public
keyword at line 8, see Compiler error CS0106 (C#) and Explicit interface implementation - One can not use
IEnumator<T> GetEnumerator()
instead of line 8. MethodGetEnumerator()
inIEnumerable<T>
hides (see Hiding through inheritance) the method of the same name fromIEnumerable
. However, when implementingIEnumerable<T>
, both methods must be implemnted. This is done using explicit interface implementation, which is what we did here.
- Method at line 3 must be explicitly specified as