Archive for the ‘OOPS’ Category

How to use IS and AS keywords in C#

Using as keyword

Dynamic polymorphism which is evaluated at run time not at compile time so we are sure that errors are not known before execution. Like e.g Explicit casting is evaluated at run time.

object test = new Manager();
//You cant cast test to a Department!
Department dept= (Department) test;

So how can we safely use such casting and handle exception

try
{ 
Department dept = (Department) test;
}
catch(InvalidCastException ex)
{ 
...
}

Above code is about handling exception but how about doing some more logical coding in the failed case, coding into catch not good example.

We can determine whether the given type is compatible with another using ‘as’. Compatibility check will return null value.

Using is Keyword
Unlike as keyword is keyword returns false if compatibility between objects or casting failed.

void(Employee emp)
{
if(emp is Department)
     Console.WriteLine("Sorry, emp is not Department..");
}

Here we are checking runtime what incoming base class reference is actually pointing in memory. So we are not using try catch as well here to wrap the casting.

Happy programming..

Number of View :1277

No Comments


Interface Programming tool A Concept

Interface is technique or concept which is used to add set of behavior or functionality to be mandatory. When we derive into class then class has to implement each thing that is in Interface. Something like making skeleton and then giving different look and behavior using that skeleton. Decoupling of object and  its implementation is possible because of interface.

Lets say you create interface of Cycle

Interface ICycle
{
void Handle(); 
void Wheels(); 
}

Inheriting class

class AtlasCycle: ICycle
{
void ResizableHandle()
{...
}
void fourDiaWheels()
{....
}
}

 

Above cycle skeleton can be used to derive any brand of bicycle with different feature values.

  • Interface has by default all fields, properties and methods Public. But you can have internally private and use it appropriately inside interface.
  • One which is public has to be implemented in inheriting class So it better than having base class.
  • Class can inherit as many as interfaces So structures can also use interfaces.
  • Interface does not have implementation only declarations.
  • It is highly used to design architecture. So it is architecture job to define what to be exposed and what not to client.

Happy Programming..

Number of View :1266

No Comments


C# Language Features OOPS .NET

Quick walkthrough on C# features.

DATA TYPES

Object Types: It is parent type of all other known types in .Net. If used as an argument in Method call then passing any type to method will cause late binding assignment to that argument.
Value and Reference Type: This type categorized on the storage of value in memory. Value Type are stored in Stack. e.g type INT, DOUBLE.
Reference types are stored in heap. e.g Type String and Object.
Converting value type into reference type called as Boxing and reverse is UnBoxing.

DECLARATION
C# allows simple and component variable declaration.

ENUMRATION
It is declared as set which are mapped to integers. This type allows us to have series which can have their mapping to unique integer value inside the set.

public enum EmployeeRole{ Trainee=0, Programmer=1, Sr Programmer=2}

 

Arrays: This type holds list.
e.g

int [] intArray;

 

OPERATORS Representing symbols for operations.
e.g Arithmetic[+, _, +], Logical[||, &&], String Concatenate[&], Increment and Decrement[+=], Comparison[<=, =>], Cast[()] etc..

FLOW CONTROL Controls the flow of your programme. e.g conditional statement [if ..else, switch], loops[while, do..while, for, foreach], functions.

CLASSES Class is type which includes all types, members, methods etc. It has visibility, its lifetime, relation, inheritance, method overloading..

REFLECTION Dynamically gather information of underlined type in late bound fashion. e.g Attributes allows to attach metadata to class and its members.

INTEROPERABILITY This feature facilitates to work with other programming language and API.
COM INTEROPERABILITY: This allows to integrate legacy components with .Net classes.
INVOKING WINDOWS API: This is same as VB6 feature to invoke and use windows API. Reference dll and create wrapper functions.

[sysimport(dll="user32.dll")]
private static extern int MessageBoxA(int hwind, string Message, string caption, int Type);

 

STRUCTURED ERROR HANDLING This is way of handling error by blocking code. e.g

Try
{}
Catch(){}
Finally{}

 

C# PREPROCESSOR This feature allows programmer to do better programming like expanding code, collapsing code…

Happy programming..

Number of View :1320

Tags:

No Comments



SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache