Saturday, March 27, 2010

Basics in C#

C# is a multi paradigm programming language which has,

  • strong typing (error if argument pass to a function does not match expected type. See strong typing vs static typing)
  • imperative (statements which changes the state in contrast to functional programming- see diff)
  • declarative
  • functional
  • generic
  • object oriented and
  • component oriented 
program disciplines.

Data Types

Value type
Directly contain data in its own memory location
Each instance has its own copy of data
Value type can be divided into Simple, Enum, Struct and Nulllable types
http://stackoverflow.com/questions/1769306/why-are-net-value-types-sealed

Reference type
Stores a reference o the data (object)
Can be divided into Class, Interface, Array and Delegate types

It is not exactly true that value types goes on stack and reference types goes on the heap


Notes
A variable is an association between a name and a slot of memory. Variable has a value which the content of memory slot is associated with. The size depends on the type of the variable.

Value of a reference type is always a reference or null. If it's a reference it should be compatible with type of the variable. The slot of memory associated with the variable is size of the reference (Int32 : 4 bytes).

Value of value type is always data for an instance of the type itself. The slot of memory is large enough to hold its value (struct with 2 integers). Value type cannot have null. Null means variable of a reference type does not point to anything. 

local variables are stored on stack, including reference type variables. 

http://jonskeet.uk/csharp/memory.html

Mutable vs. Immutable 

  • Mutable : can change, Immutable : Cannot change
  • A mutable string can be changed, immutable string cannot
  • Immutable type is a class or struct written so that it cannot be changed once it is created. 
    • Once a string is constructed, it cannot be changed. All the functions you can call on a string return a new string, instead of manipulating the string they are called on
  • Instances of immutable types are inherently thread-safe (No thread can modify it)
  • source , mutable structs are evil
  • Always make value types Mutable (Eric Lappert)

Tuesday, March 16, 2010

Structural Design Patterns

Decorator

Allows behavior to be added to an individual object either statically or dynamically, without affecting the behavior of other objects from the same class. FileReader in C# is a good example for this. Also check this article from codeproject for C#. Explore the Decorator pattern in JavaScript

Source
Example

Facade

Provides simplified interface to larger body of code such as a class library. Checkout this article by dofactory for C# and addyosmani section for JavaScript.


Source

Composite

Describes that group of objects to be treated same way. The objective is to "compose" objects into tree structures to represent part-whole hierarchies.


Proxy

It's a class acting as an interface to something else. This can be a network connecting, web service or some other resource which is expensive or impossible to duplicate


Module pattern

See (http://en.wikipedia.org/wiki/Module_pattern)


Resources

http://en.wikipedia.org/wiki/Software_design_pattern
Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook