Allen Jones


Add, Substract, and Compare Dates and Times

Problem: You need to perform basic arithmetic operations or comparisons using dates and times.

Solution: Use DateTime and TimeSpan classes, which support standart arithmetic and comparison operators. A DateTime instace represents a specific time (such as 3:40 AM July 20 1999), whereas a TimeSpan instance represents a period of time (such as 3 hours 45 minutes). You will often want to add, substract and compare TimeSpan and DateTime instances. Internally, both DateTime and TimeSpan use ticks to represent time (a tick is equal to 100 nanoseconds). TimeSpan stores its time internal as the number of ticks equal to that interval, and DateTime stores time as the number of ticks since 00:00:00 January 1 0001. This approach and the use of operator overloading makes its easy for DateTime and TimeSpace to support basic arithmetic and comparison operations.

Operator TimeSpan DateTime
Assignment (=) Because TimeSpan is a structure, assignment returns a copy and not a reference Because DateTime is a structure, assignment returns a copy and not a reference
Addition (+) Adds two TimeSpan instances Adds a TimeSpan to a DateTime
Subtraction (-) Subtracts one TimeSpan instance from another Subtracts a TimeSpan or a DateTime from a DateTime
Equality (==) Compares two TimeSpan instances and returns true if they are equal Compares two DateTime instances and returns true if they are equal
Inequality (!=) Compares two TimeSpan instances and returns true if they aren’t equal Compares two DateTime instances and returns true if they aren’t equal
Greater Than (>) Determines if one TimeSpan is greater than another TimeSpan Determines if one DateTime is greater than another DateTime
Greater Than or Equal(>=) Determines if one TimeSpan is greater than or equal to another TimeSpan Determines if one DateTime is greater than or equal to another DateTime
Less Than (<) Determines if one TimeSpan is less than another TimeSpan Determines if one DateTime is less than another DateTime
Less Than or Equal (<=) Determines if one TimeSpan is less than or equal to another TimeSpan Determines if one DateTime is less than or equal to another DateTime
Unary Negation (-) Returns a TimeSpan with a negated value of the specified TimeSpan Not Supported/td>
Unary Plus (+) Returns the TimeSpan specified Not Supported

Sort an Array or an ArrayList

Problem: You need to sort the elements contained in an array or an ArrayList.

Solution: Use the ArrayList.Sort method to sort ArrayList objects and the static Array.Sort method to sort arrays.


Copy a Collection to an Array

Problem: You need to copy the contents of a collection to an array.

Solution: Use the ICollection.CopyTo method implemented by all collection classes, or use the ToArray method implemented by the ArrayList, Stack, and Queue collections.


Create a Strongly Typed Collection

Problem: You need to create a collection that can hold elements only of a specific type.

Solution: Create a class that derives from the System.Collections.CollectionBase or System.Collections.DictionaryBase classes, and implement type-safe methods for the manipulation of the collection.


Store a Serializable Object to a File

Problem: You need to store a serializable object and its state to a file and then deserialize it later.

Solution: Use a formatter to serialize the object and write it to a System.IO.FileStream. When you need to retrieve the object, use the same type of formatter to read the serialized data from the file and deserialize the object. The .NET Framework class library includes the following formatter implementations for serializing objects to binary or SOAP format:

  • System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
  • System.Runtime.Serialization.Formatters.Soap.SoapFormatter