Saturday, 14 February 2015

Download ALL Old Versions of iTunes


Found a really great website that let's you download ALL older versions of iTunes for iOS, Windows 32bit and Windows 64bit!

Website: http://appstudio.org/itunes/

Wednesday, 11 February 2015

C# - Serialize and Deserialize using BinaryFormatter with Strings (WITHOUT Files)


This code snippet will allow you to Serialize and DeSerialize any serializable object to a standard string and back. No need for binary files!

Usage:
Code Snippet
  1. // Inits
  2. CustomClass instance = new CustomClass();
  3.  
  4. // Serialize
  5. string data = SerializeObject<CustomClass>(instance);
  6.  
  7. // DeSerialize
  8. CustomClass instance = DeSerializeObject<CustomClass>(data);
End of Code Snippet


Code Snippet
  1. // NOTE: The result of serializing an object with BinaryFormatter is an octet stream, not a string.
  2. //       If we encode the serialized object with Base64, we will get our string.
  3. public static string SerializeObject<T>(T toSerialize)
  4. {
  5.     System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  6.  
  7.     if (!toSerialize.GetType().IsSerializable)
  8.     {
  9.         throw new Exception("Object cannot be serialized in it's current state.");
  10.     }
  11.  
  12.     using (MemoryStream ms = new MemoryStream())
  13.     {
  14.         new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(ms, toSerialize);
  15.         return Convert.ToBase64String(ms.ToArray());
  16.     }
  17. }
  18.  
  19. public static T DeSerializeObject<T>(string data)
  20. {
  21.     byte[] bytes = Convert.FromBase64String(data);
  22.  
  23.     using (MemoryStream ms = new MemoryStream(bytes))
  24.     {
  25.         return (T)new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Deserialize(ms);
  26.     }
  27. }
End of Code Snippet

Friday, 6 February 2015

Recursion and Recursive Methods for Dummies - Working C# Example


In Brief
What is Recursion: Very simply put... A method that calls itself.
Why use Recursion?: So we can repeat a methods logic any number of times with differing arguments.
What is an Example of Recursion?: Directories which contain directories which contain more directories and you would like to scan through all of the files within this directory chain. We can create ONE SINGLE method to do this by calling itself recursively. See below for full example.

Here is a working example of a recursive method in C#. It takes a file directory as an argument and recursively loops through all of the files which the WHOLE directory structure. It goes in deep, then works backwards. This is an example of backwards or tail recursion.

Code Snippet
  1. public void NameOfRecursiveMethod(string directoryToSearch)
  2. {
  3.     // Go in deep as far as possible, then work backwards.
  4.     // Find all the subdirectories under this directory.
  5.     foreach (string dir in Directory.GetDirectories(directoryToSearch))
  6.     {
  7.         // Recursive call for each subdirectory.
  8.         this.NameOfRecursiveMethod(dir);
  9.     }
  10.    
  11.     // Note: The First time we arrive here will be inside the deepest first directory
  12.     // Get all files in the directory
  13.     string[] files = Directory.GetFiles(directoryToSearch);
  14.  
  15.     foreach (string filename in files)
  16.     {
  17.         // Do something for each file here
  18.        
  19.     }
  20. }
End of Code Snippet

How to Deep Clone an Object by Value and not by Reference in C#


The following code snippet using serialization to deep clone an object in C#. Please note: ICloneable is a deprecated API. ICloneable is considered a poor API, since it does not specify whether the result is a deep or a shallow copy.

Code Snippet
  1. // Usage
  2. CLASSNAME clonedInstance = DeepClone<CLASSNAME>(instance);
  3.  
  4.  
  5. // Deep clones an object
  6. // Your class (and all sub-classes) MUST be marked as [Serializable] in order for this to work.
  7. public static T DeepClone<T>(T obj)
  8. {
  9.     using (var ms = new System.IO.MemoryStream())
  10.     {
  11.         var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  12.         formatter.Serialize(ms, obj);
  13.         ms.Position = 0;
  14.  
  15.         return (T)formatter.Deserialize(ms);
  16.     }
  17. }
End of Code Snippet