Pages

Tuesday, September 15, 2015

Value vs Reference Types in c#

VALUE types are stored in one location in stack memory.

Primitive types such as int, bool, float, char are value types.

Structs are value types. Structs are cut down classes. Must use Struct keyword instead of class. Structs do not support inheritance. They reside in stack memory which can run faster than things stored in heap.

Runtime deals with the actual data so it is more efficient.


Reference types are stored in heap memory. The object is created in memory and then handled through a reference, similar to a pointer.

Classes are reference types. They are less efficient. Classes support inheritance.


Example value types:

int i = 1;
int j = i;
i++;

what is the value of j?

when you assign the value of i to j, it makes an actual copy of the value so j = 1; so when i is incremented, it doesn't affect the value of j; therefore, j = 1 and j does not = 2.


Example reference types:

class Form()
{
   string Text;
}

Form myForm = new Form();   // this creates two stores of memory: 1) ref to object  2) the object itself  

Form myForm;              // this breaks down the above to see clearer  1) reference to object is created
myForm = new Form();    // object is created in memory

Test (myForm);

void Test (Form f)
{
    f.Text = "hello";      // myForm's caption changes because myForm and f point to the same object
    f = null;   // myForm is intact because only a copy of the reference has been nullified/erased; myForm still holds its original reference to the object
}



http://www.albahari.com/valuevsreftypes.aspx

Monday, September 7, 2015

Object Oriented Programming

Object Oriented Programming (OOP) is a software development methodology that breaks up code into modules for greater reuse and maintainability. Prior to OOP, code was written in procedural format that led to large, monolithic pieces of code that was difficult to maintain (update and make changes). OOP breaks up the code into chunks for better maintainability which allows the code base to scale.

Class allows you to create custom types by grouping together variables, methods, and events. The class acts a blueprint or a template.

There are 3 pillars of OOP.

1) Inheritance - this deals with code reuse. If there are multiple classes that share the same properties, fields, methods, and events, you can create a base class that contains all of the shared members. Any child class that inherits from the base class is automatically exposed to all the members of the base class. The child class can be extended to add its own members to make it unique. If a method in the base class is defined as virtual, then the child class can override the method creating a new method implementation.

An abstract class is a base class that can contain abstract methods. Abstract methods are only defined and do not have any code implementation. Abstract classes cannot be instantiated. They can only be inherited. When a class inherits from an abstract class, it must implement all the abstract methods. An interface is a special form of an abstract class where ALL the methods are abstract. A class can only inherit from one abstract class. A class can inherit from multiple interfaces.

2) Encapsulation - this describes hiding the internal state (data) of a class. The common technique is to use private fields that can only be accessed by public properties. The advantage is that you have more control over the behavior of the data you can protect the data. For example, if you define a public field as int, it is exposed (can be changed) by all the code in the application. Also, it has a valid range of +/- 2.1 billion. By using encapsulation, you can implement code in the SET part of the public property that limits the valid values from 0 - 10.

Encapsulation helps to reduce coupling between objects and increases code maintainability.

3) Polymorphism - allows an object of one type to behave like a different type.

Example is the use of interfaces. You can have multiple classes inheriting the same interface, however, each class can implement different methods, thereby achieving different behaviors.

Another example is the use of overrides. If a base class defines a method as virtual, then child classes that inherit from the base class can override/change the virtual method implementation. The result is that multiple classes can inherit from the same base class, but because of different code implementations (overrides), they can all behave differently.

Method overloading IS NOT polymorphism.

*****

Interface - collection of properties and public methods grouped together to encapsulate functionality. The methods are abstract meaning that they are only defined. Any class that inherits from an interface must implement code for all the methods. In this sense, an interface is like a contract that guarantees the availability of all the methods defined. An interface is a special form of an abstract class whereby, all the methods are abstract. As such, it cannot be instantiated and must be inherited.

Static member - are shared between instances of a class. Think of them as global variables. Don't have to instantiate to use. Example:  Console.WriteLine() is a static method.

Struct - is like a cut down version of a class. Use Struct keyword instead of class. Structs are value types and resides in one place in memory. Classes are reference types so it holds a reference to a location in memory where the content is stored. Struct resides in the stack memory and the actual object is affected so it runs faster than classes that reside in the heap. Structs do not support inheritance.