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/
Technical blog discussing various programming languages, frameworks and paradigms. Code snippets and projects are also provided.
Saturday, 14 February 2015
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:
Usage:
Code Snippet
- // Inits
- // Serialize
- string data = SerializeObject<CustomClass>(instance);
- // DeSerialize
- CustomClass instance = DeSerializeObject<CustomClass>(data);
End of Code Snippet
Code Snippet
- // NOTE: The result of serializing an object with BinaryFormatter is an octet stream, not a string.
- // If we encode the serialized object with Base64, we will get our string.
- public static string SerializeObject<T>(T toSerialize)
- {
- System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
- if (!toSerialize.GetType().IsSerializable)
- {
- }
- {
- return Convert.ToBase64String(ms.ToArray());
- }
- }
- public static T DeSerializeObject<T>(string data)
- {
- byte[] bytes = Convert.FromBase64String(data);
- {
- }
- }
End of Code Snippet
Labels:
C#
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.
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
- public void NameOfRecursiveMethod(string directoryToSearch)
- {
- // Go in deep as far as possible, then work backwards.
- // Find all the subdirectories under this directory.
- foreach (string dir in Directory.GetDirectories(directoryToSearch))
- {
- // Recursive call for each subdirectory.
- this.NameOfRecursiveMethod(dir);
- }
- // Note: The First time we arrive here will be inside the deepest first directory
- // Get all files in the directory
- string[] files = Directory.GetFiles(directoryToSearch);
- foreach (string filename in files)
- {
- // Do something for each file here
- }
- }
End of Code Snippet
Labels:
C#
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
- // Usage
- CLASSNAME clonedInstance = DeepClone<CLASSNAME>(instance);
- // Deep clones an object
- // Your class (and all sub-classes) MUST be marked as [Serializable] in order for this to work.
- public static T DeepClone<T>(T obj)
- {
- {
- formatter.Serialize(ms, obj);
- ms.Position = 0;
- return (T)formatter.Deserialize(ms);
- }
- }
End of Code Snippet
Labels:
C#
Subscribe to:
Posts (Atom)