Friday, October 7, 2011

properties and indexers in c#

Properties:
Properties are extensions to fields. properties are called smart fields. There are two accessors get and set. get is used to retrieve the value where as set is used
to assign the value to that property. A property that contains get accessor only is called read only property, property that contains set accessor only is called write only property. get accessor doesn't accept any parameter, set accessor contains an implicit parameter called value.
Example:

/* Property Example */
using System;
namespace ProjectOne
{
public class PropertyExample
{
int testValue;
public int testProperty
{
get
{
return testValue;
}
set
{
testValue = value;
}
}
}
class Program
{
static void Main(string[] args)
{
PropertyExample PE = new PropertyExample();
PE.testProperty = 200;
Console.Write("{0}", PE.testProperty );
Console.Read();
}
}
}

Indexers:
Indexers treat objects as an array. indexers are called smart arrays.
Example:

/* Indexer Example */
using System;
namespace ProjectOne
{
public class IndexerExample
{
int[] testValue = new int[5];

public int this[int index]
{
get
{
return testValue[index];
}
set
{
testValue[index] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
IndexerExample IE = new IndexerExample();
IE[0] = 200;
Console.Write("{0}", IE[0]);
Console.Read();
}
}
}

Properties Vs Indexers:
static properties can be used without instantiating the class, A static property can
access static members of a class only. we cant define static Indexers.
properties identified by name where as indexers identified by signature.
Below are the difference between properties and indexers.




























Overloading

Inheritance

Overriding

abstract

can have Static

properties

NO

YES

YES

YES

YES

Indexers

YES

YES

YES

YES

NO

Wednesday, October 5, 2011

Difference between Abstraction and Encapsulation with example

Abstraction:
Abstraction refers to, providing only essential information to the outside world and hiding their background details.
Encapsulation :
The wrapping up of data members and member functions into a single unit is called encapsulation. Every class is encapsulation
and every method is also an encapsulation.
Example: Class is encapsulation where as object created for that class is abstraction.

Tuesday, October 4, 2011

Access Modifiers in C#

There are 5 types of access modifiers in c#
Public
public members can be accessed by any other code in the same assembly or any other assembly that references it.
private
private members can only be accessed by code in the same class or structure.
Protected
protected members can only be accessed by code in the same class or structure,
or in a derived class.(Classes that do not derived from the protected members class can't use these protected members)
Internal
Internal members can be accessed by any code in the same assembly, but not from another assembly.
Protected Internal
Protected Internal members can be accessed by any code in the same assembly, or by any derived class in another assembly. protected internal members are either protected or internal but not both. Inside the same assembly it behaves as internal, in other assemblies it behaves as protected members.



















































same assembly - same class

same assembly -derived class

same assembly - Object reference

diff assembly - Object reference

diff assembly - derived class

public

TRUE

TRUE

TRUE

TRUE

TRUE

private

TRUE

FALSE

FALSE

FALSE

FALSE

protected

TRUE

TRUE

FALSE

FALSE

TRUE

Internal

TRUE

TRUE

TRUE

FALSE

FALSE

protected internal

TRUE

TRUE

TRUE

FALSE

TRUE


Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectOne
{
/* same assembly - same class */
public class AccessModifiers
{
public int publicVariable = 10;
private int privateVariable = 20;
protected int protectedVariable = 30;
internal int internalVariable = 40;
protected internal int protectedInternalVariable = 50;

public void accessModifiersDisplay()
{
Console.WriteLine("{0}", publicVariable);
Console.WriteLine("{0}", privateVariable);
Console.WriteLine("{0}", protectedVariable);
Console.WriteLine("{0}", internalVariable);
Console.WriteLine("{0}", protectedInternalVariable);
Console.Read();
}
}

/* same assembly - derived class */
class DerivedFromAccessModifiers : AccessModifiers
{
public void derivedFromAccessModifiersDisplay()
{
Console.WriteLine("{0}", publicVariable);
// Console.WriteLine("{0}", privateVariable); /* Not Accessable */
Console.WriteLine("{0}", protectedVariable);
Console.WriteLine("{0}", internalVariable);
Console.WriteLine("{0}", protectedInternalVariable);
}
}

/* same assembly - Object reference */
class Program
{
static void Main(string[] args)
{
AccessModifiers AM = new AccessModifiers();

Console.WriteLine("{0}", AM.publicVariable);
// Console.WriteLine("{0}", AM.privateVariable); /* Not Accessable */
// Console.WriteLine("{0}", AM.protectedVariable); /* Not Accessable */
Console.WriteLine("{0}", AM.internalVariable);
Console.WriteLine("{0}", AM.protectedInternalVariable);

AM.accessModifiersDisplay();
}
}
}

/* Different Assembly */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectOne;

namespace ProjectTwo
{
/* different assembly - Object reference */
class AccessModifiers_2
{
AccessModifiers AM = new AccessModifiers();
public void accessModifiersDisplay_2()
{
Console.WriteLine("{0}", AM.publicVariable);
// Console.WriteLine("{0}", AM.privateVariable); /* Not Accessable */
// Console.WriteLine("{0}", AM.protectedVariable); /* Not Accessable */
// Console.WriteLine("{0}", AM.internalVariable); /* Not Accessable */
// Console.WriteLine("{0}", AM.protectedInternalVariable); /* Not Accessable */
}
}

/* different assembly - derived class */
class DerivedFromAccessModifiers_2 : AccessModifiers
{
public void derivedFromAccessModifiersDisplay()
{
Console.WriteLine("{0}", publicVariable);
// Console.WriteLine("{0}", privateVariable); /* Not Accessable */
Console.WriteLine("{0}", protectedVariable);
// Console.WriteLine("{0}", internalVariable); /* Not Accessable */
Console.WriteLine("{0}", protectedInternalVariable);
}
}
}