Category Archives: C#


Difference between Boxing and Unboxing in C#


  • The first two lines of code declare and initialize value type variable i and reference type variable oi.
  • In the third line of code, you want to assign the value of variable i to oi. But oi is a reference type variable and must be assigned a reference to an object in the heap.
  • Variable i, however, is a value type and doesn’t have a reference to an object in the heap.
  • The system therefore box the value of i by doing the following:

    • Creating an object of type int in the heap
    • Copying the value of i to the int object
    • Returning the reference of the int object to oi to store as its reference

    Memory is allocated on the heap that will contain the value type’s data and the other overhead necessary to make the object look like
    every other instance of a managed object of reference type .
    The value of the value type is copied from its current storage location into the newly allocated location on the heap.
    The result of the conversion is a reference to the new storage location on the heap.


    What is UnBoxing Conversion?

    Unboxing is the process of converting a boxed object back to its value type.Unboxing is an explicit conversion.

    The system performs the following steps when unboxing a value to ValueTypeT:

    It checks that the object being unboxed is actually a boxed value of type ValueTypeT.
    It copies the value of the object to the variable.


Search for string in string array







Determine if any ints are larger or less



less than or equal to 3: 3
more than or equal to 7: 8
less than or equal to 3: 1
more than or equal to 7: 7
more than or equal to 7: 9
less than or equal to 3: 2
more than or equal to 7: 8

Find age in Array of Structs


Delegates for c#

Doubly Linked List

Situations for using a Doubly Linked list would be

  • deck of cards
  • browser cache
  • Metro

A new node’s previous points left, and next points right. When we add a new node, we pass in the linked list’s tail into the new Node’s constructor.

The new Node will re-wire it so that its previous points to the last node of the linked list (via the tail).

Then have the next of the last node point to the new Node.

Then, the linked list will proceed to re-assign the tail to the new node. Thus, completing the push.

Also, each node is connected with each by 2 traits:

  • The first node’s next points to the second node. The first node’s previous points to the node on its left side. (or bottom, if you are looking at it vertically)
  • The second node’s previous points to the first node. And its next points to the node on its right side. (or top, if you are looking at it vertically)


Getter setter is done by first declaring a name that matches your property but with a capital first letter. Then, you use keywords get and write a function for when you retrieve the property.

Use keyword set to write a function where you assign another value to your property. The incoming value is designated as keyword “value”. The value keyword is a contextual keyword, that is, it has a different meaning based on its context.

Inside a set block, it simply means the value that the programmer has set it to.

Outside of a set block, you can use value as a variable identifier, as such:
int value = 5;

You’d use it like this:

myList.Count = 222;

Doubly Linked List source


For queues, we append new node at the tail end, and the tail take on its next.

The head pointer stays in place at the first node. When we execute something, we pop from the queue, and the head moves to next.


Stack appends (or pushes) at the end. And pops at the end also. hence, all we need is one pointer to do that.



Abstraction is the tactic of stripping an idea or object of its unnecessary accompaniments until you are left with its essential, minimal form. A good abstraction clears away unimportant details and allows you to focus and concentrate on the important details.

  • Declare the abstract class, then use keyword abstract to define your abstraction methods
  • Create a class, and conform to that abstraction
  • Once conformed, use keyword override to implement for that abstraction

Classes and Structs


Struct is a blueprint for a value – no identity, no accessible state, no added behavior (methods).

Variables of struct type are strictly called values.

Value types are the types found at the lowest level of a program. They are the elements used to build larger software entities. Value types can be freely copied and exist on the stack as local variables or as attributes inside the objects they describe.

Consider defining a structure instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

Do not define a structure unless the type has all of the following characteristics:

It logically represents a single value, similar to primitive types (integer, double, and so on).
It has an instance size smaller than 16 bytes.
It is immutable.
It will not have to be boxed frequently.

notes –

In addition, realize that when a struct implements an interface – as Enumerator does – and is cast to that implemented type, the struct becomes a reference type and is moved to the heap.

Whenever you don’t need polymorphism, want value semantics, and want to avoid heap allocation and the associated garbage collection overhead. The caveat, however, is that structs (arbitrarily large) are more expensive to pass around than class references (usually one machine word), so classes could end up being faster in practice.


A class is a blueprint for an object.

It has identity. Identity is an instantiated object with its own state.
It has accessible state (private variables), added behavior (methods).

Types represented by classes are called reference types.
Reference types are the types found at the higher levels of a program. They are built from smaller software entities. Reference types generally cannot be copied, and they exist on the heap.

If you copy a struct, C# creates a new copy of the object and assigns the copy of the object to a separate struct instance. Thus, we’re dealing with the object itself.

However, if you copy a class, C# creates a new copy of the reference to the object and assigns the copy of the reference to the separate class instance. We’re dealing with the address here.