Thursday, April 24, 2014

Mobile App Development (using Xamarin)

Here is an excellent set of guidelines to consider when building any mobile app. The approach is dictated primarily by Xamarin (a mobile development framework that allows cross-platform building and deploying of an app), but the approach is widely applicable to any development environment whether it be XCode and the iOS SDK or Eclipse and the Android SDK.

Wednesday, April 23, 2014

Sorted Arrays and Search

I came across this article yesterday: Efficiently Searching a Sorted Array. If you haven't heard about Four Guys from Rolla yet, you haven't been programming long enough. I come across them every now and then. Good stuff.

What I learned:

In order to search a sorted array efficiently, the array has to be sorted first (Duh!). Sorting an array is a linear operation which isn't ideal; it works out to be O(n). I may be wrong on this as I can't find my source any more. Ugh.


Difference between List and Collection

The tutorials I posted in the post titled Microsoft C# .NET Data Structures Lessons are based on .NET 2.0. As you are hopefully aware, the .NET CLR (Common Language Runtime) is currently sitting at version 5.0 while the programming language C# .NET is currently at version 4.5 as of June 2013 (according to this Wikipedia article on C#).

I found my way over to this StackOverflow question and answer while trying to figure out the difference between a List and a Collection. If the post is correct, a Collection implements IList but is different from a List in that it is extensible (it can be modified, enhanced, and added to). Pretty cool. The reason I mentioned the tutorials and their versioning is that Collections as of C# .NET 3.5 are no longer part of the System.Collections.Generic namespace. Good thing I looked this up or it would be back to Google when I tried to implement Collection and couldn't find it.

Difference between List and ArrayList() in C# .NET


A List is typesafe whereas an ArrayList can contain anything that inherits from Object() and is open to runtime errors if an object of unexpected type is retrieved from the ArrayList and cast to the wrong type.

Example:
List myStringList = new List();
myStringList.Add("I like cheese.");
string cheesy = myStringList[0];

ArrayList myArrayList = new ArrayList();
myArrayList.Add("I like cheese.");
int cheesy = (int) myArrayList[0];

Both examples are perfectly legal code and will not cause a compile time error. The ArrayList example will, however, cause a run time error as you cannot cast a string to an int.

More can be found at this StackOverflow post.
 
 

Microsoft C# .NET Data Structures Lessons

These are invaluable! Great series of posts (six in total) from Microsoft on implementing and using data structures in C# .NET.


  1. Introduction
  2. The Queue, Stack, and Hashtable
  3. Binary Trees and BSTs
  4. Building a Better Binary Search Tree
  5. From Trees to Graphs
  6. Efficiently Representing Sets

What's the difference between the heap and stack in C# .NET?

This question has popped up several times while I have been studying data structures for an upcoming interview. The best answer I've found so far is this blog post by Eric Lippert at Microsoft. A short summary:

  • There are three kinds of storage locations: stack locations, heap locations, and registers.
  • Long-lived storage locations are always heap locations.
  • Short-lived storage locations are always stack locations or registers.
  • There are some situations in which it is difficult for the compiler or runtime to determine whether a particular storage location is short-lived or long-lived. In those cases, the prudent decision is to treat them as long-lived. In particular, the storage locations of instances of reference types are always treated as though they are long-lived, even if they are provably short-lived. Therefore they always go on the heap.

Always keep in mind that "The type system has nothing whatsoever to do with the storage allocation strategy." (my own paraphrase)

Monday, April 21, 2014

Abstract vs. Static Classes in C#

What is the difference between an abstract and a static class in C#?

For starters, this is a poor question. Let me start with the similarity that I think trips people up and causes them to ask such a question in the first place:

Neither a static class nor an abstract class can be instantiated.

Now, concerning their differences, here is an answer from a StackOverflow question that I think hits the nail on the head.

What is the most important difference between abstract classes and static classes in C#?
That's more like it.
An abstract class is usually intended to model something in a type hierarchy. For example, a truck is a kind of vehicle, and an airplane is a kind of vehicle, so you might have a base class Vehicle and derived classes Truck and Airplane. But "Vehicle" is abstract; there are no vehicles which are just vehicles without being some more specific kind of thing. You represent that concept with an abstract class.
A static class by contrast is not intended to model anything at all. It's just a convenient way of storing a bunch of code. Really it shouldn't be a class at all; VB made a better choice by calling such things "modules" rather than "classes". Though technically they inherit from object, static classes are logically not really in a type hierarchy at all. They're just a bucket for holding static members.
Static classes are often used as containers of extension methods.
When do I use what and why?
Use an abstract class when you want to build a model of the form "an X is a kind of Y". Like "a Car is a kind of Vehicle" or "a Square is a kind of Shape" or "a Magazine is a kind of Publication", where the "Y" is an abstract concept. Don't use it for things like "an Employee is a kind of Person" -- Person should be concrete. Person is not an abstract concept; there are people who are just people, but there are no vehicles that are not something else.
Use a static class when you want to make extension methods, or when you have a bunch of code that fits logically together but does not associate with any object. For example, if you have a bunch of related math routines, that's a good candidate for a static class.

Original StackOverflow Question

Saturday, April 19, 2014

Difference Between Heap and Stack

While studying for an upcoming interview, I found the following definitions of the heap and stack specifically in regards to C# .NET:

"The stack" (or more precisely the call stack) is automatically managed memory (even in "unmanaged languages" like C): Local variables are stored on the stack in stack frames that also contain the procedures or functions arguments and the return address and maybe some machine-specific state that needs to be restored upon return.

Heap memory is that part of RAM (or rather: virtual address space) used to satisfy dynamic memory allocations (malloc in C).

Yet, in C# heap and stack usage is an implementation detail. In practice though, objects of reference type are heap-allocated; value type data can both be stored on the stack and on the heap, depending on the context (e.g. if it's part of an reference-type object).


You can read the original question and answer on StackOverflow here: http://stackoverflow.com/questions/6531481/stack-and-heap-in-c-sharp