Thursday, March 20, 2008

OOP Concepts

OOP Concepts:

1. POLYMORPHISM: In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms") is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

2.ENCAPSULATION: Conceptually, encapsulation means hiding the internal details of a class's operation.Goal of encapsulation: use an object without understanding or depending on its internal details.

3.INHERITANCE: Inheritance is used to establish hierarchical class relationships.In inheritance, one class (the subclass) adopts (inherits) the features of another class (the superclass).

4.ABSTRACTION:In object technology, determining the essential characteristics of an object. Abstraction is one of the basic principles of object-oriented design, which allows for creating user-defined data types, known as objects.

Wednesday, March 19, 2008

Dispose(), Destructor/Finalizer

Destructors are called by GC (non-deterministic) ~ClassName() in C# & Finalize() method in VB.NET when GC determines it is time for garbage collection.
Not the best place to put cleanup code.
Ex: open a database connection and put the cleanup code in the Destructor. That DB connection might stay open for the life of the entire application. Not good!

Dispose() method:
A better place to put cleanup code.
Mostly used to cleanup unmanaged resources.
using{} statement automatically calls the Dispose() before exiting.
Recommended to be called only if the object is resource intensive.

References:
http://www.dotnetspider.com/qa/Question1450.aspx

Access Modifiers (public, private, ...)

By default;

classes: internal by default. (can be internal or public, can NOT be private, protected or protected internal)
interfaces & enumerations: always public, no access modifier needed or allowed.
namespaces: always public
Struct members: private by default. (can be public, private, internal)

public: accessible from instance & derived classes
private: NOT accessible from instance nor derived classes
protected: NOT accessible from instance but accessible from derived classes
internal: accessible from instance & derived classes WITHIN THE SAME ASSEMBLY ONLY.
protected internal: NOT accessible from instance but accessible from derived classes of the same assembly.

So, if a member is marked as protected, then it is accessible from derived classes of the SAME assembly or other assemblies.

Tuesday, March 18, 2008

Reference & Value Types and Primitive Types

Value types:
All numeric data types
Boolean, Char, and Date
All structures, even if their members are reference types
Enumerations, since their underlying type is always Byte, Short, Integer, or Long

Reference types:
String
All arrays, even if their elements are value types
Class types, such as Form
Delegates

Primitive types:
Some of the data types are used very frequently during programming. To ease coding using these data types, compilers allow us to usesimplified syntax.
So, the data types supported directly by the compiler are called primitive types and these types map to the types in the Base Class Library (BCL).

For instance, instead of initializing an integer like this;
System.Int32 i = new System.Int32(10); //Throws compiler error

we can easily initialize it using below line of code:
int i =5;

So, the primitive type "int" maps to "System.Int32" type in the BCL.
string maps to System.String.
object maps to System.Object.
...

Boxing & Unboxing:
Boxing refers to the operation of converting a value type to a reference type.

Int32 v = 5; // Create an unboxed value type variable
Object o = v; // o refers to a boxed version of v
v = 123;

-----------------------------------------------------------
public static void Main() {
Int32 v = 5; // Create an unboxed value type variable

// When compiling the following line, v is boxed 3 times
// wasting heap memory and CPU time
Console.WriteLine(v + ", " + v + ", " + v);

// The lines below use less memory and run faster
Object o = v; // Manually box v (just once)

// No boxing occurs to compile the following line
Console.WriteLine(o + ", " + o + ", " + o);
}

default Keyword in Generic Code:
This is an issue with the generics concept in .NET 2.0.

public class GenericList
{
}

When we do not know in advance,
whether the type T is a value or a reference type
and if it is a value type whether is it a numeric value or a struct
we can use the default keyword.

T theTypeInstance= default(T);

This initializes theTypeInstance;
to null if T is a reference type,
to 0 if numeric,
the references types of the struct to null & the numeric types of the struct to 0.

References:
http://msdn2.microsoft.com/en-us/magazine/cc301569.aspx
http://msdn2.microsoft.com/en-us/magazine/bb984984.aspx
http://msdn2.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx

Monday, March 17, 2008

C# indexer

Declaration:
public this [ identifier]

Example:
public string this [int theIndex]



Sample C# Use:

using System;

//Indexer Class Declaration
class MyIntIndexer
{
private string[] theData;

public MyIntIndexer(int size)
{
theData= new string[size];

//Initialize
for (int i=0; i < size; i++)
{
theData[i] = "nothing";
}
}

public string this[int position]
{
get
{
return theData[position];
}
set
{
theData[position] = value;
}
}


static void Main(string[] args)
{
int size = 5;

MyIntIndexer myIndexer = new MyIntIndexer(size);

myIndexer[3] = "str3";
myIndexer[1] = "str1";
myIndexer[5] = "str5";

}
}

Monday, March 10, 2008

const vs readonly

a const member's value is defined at compile time.
a readonly member's value can be set at runtime as well as compile time.

static means the member is not tied to the instances of the class. There exists only one copy of the static member regardless of the number of instances.