Thursday, April 24, 2014
Mobile App Development (using Xamarin)
Wednesday, April 23, 2014
Sorted Arrays and Search
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
I found my way over to this StackOverflow question and answer while trying to figure out the difference between a List
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:ListmyStringList = 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
What's the difference between the heap and stack in C# .NET?
- 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#
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
