Archive | September, 2010

best .net training : Importance of Finally in C#

Consider the below example:

public class Mathematics
{
public int DivisionResult(int x, int y)
{
try
{
return x / y;
}
catch (Exception)
{
return -1;
}
Console.WriteLine(“Will this line be executed or not?”);
}
}
Question: In the above sample the code in Console.WriteLine will be executed or not?
Ans: No it wont since because of the return statement in the try and catch blocks.
If you want to execute some line of code always or at any point of time even if you have return codes. Put your code in finally block. This ensures that your code in finally block is always executed irrespective of written codes.

Posted in C# FAQ6 Comments

Order of Exceptions in C#

Order of Catch blocks is very importent while implementing the Catch Blocks.For Example if my code is expected to throw DivideByZeroException , If you know the exception type properly you have to write the specifice expected exception then write generic exception. See below example

Ex:

public class Mathematics
{
public int DivisionResult(int x, int y)
{
try
{
return x / y;
}
catch (DivideByZeroException de)
{
return -0;
}
catch (Exception)
{
return -1;
}
}
}
Note1: In the above example if you change the order of Catch you will get compile time error “A previous catch clause already catches all exceptions of this or of a super type (‘System.Exception’)”. This because of all the exception can be caught by the generic catchblock.
Note2: How many catch blocks can i have for a signgle try block . Answer is no limit.
Note3 : Can i have a try without catch. Ans: a try should always either have a catch or finally. try will never exist alone ( try cannot be a bachelor ).

Posted in C# FAQ5 Comments

Writing abstract properties and Implementing

public abstract class Surgeon
{
public abstract int Sal{get;set;}
}
public class SeniorSurgeon : Surgeon
{
private int _sal;
public override int Sal
{
get { return _sal; }
set { _sal = value; }
}
}

Posted in C# FAQ0 Comments

Properties Indepth

Q:Can i have access modifier for the get or set?

Ans: Yes you can but only for either for get or for set. You can define access modifiers for both get and set in a given property.

Ex:

public class Employer
{
private string _employerName;
public string EmployerName
{
private get { return _employerName; }
set { _employerName = value; }
}
}
Note : When we develop the project for this batch you will see the Properties usage  more frequently.

Posted in C# FAQ0 Comments

Why and When to use Properties

Properties in C# is a faster mechanism of reading and updating the private data. Properties generally considered as faster compared to methods . This is because for every instance method an entry will be created in the method table. Usually for CLR searching in method table is cumbersome and time consuming , hence Properties are faster.
GuideLine: See the article in msdn

http://msdn.microsoft.com/en-us/library/bzwdh01d(VS.71).aspx

Posted in C# FAQ2 Comments

How to implement interface in Struct

public interface Iphysics
{
int getsalary( int Empid);
}
public struct Campus : Iphysics
{
public string CampusName()
{
return “Palle Technologies Campus”;
}
public int getsalary(int Empid)
{
return 50000;
}
}

Posted in C# FAQ1 Comment

Ref and Out Parameters

Ref has to be initialized before calling the method. Out should be initialized after calling method i.e in the called method.See below Example.

Public Class Employee {

public double AddBonus(out int sal)
{
int result = 2000;
sal =10000;
return sal + (sal * 0.2);
}
public double IncrementSalary(ref int sal)
{
return sal + (sal * .1);
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Employee objemp = new Employee();
int sal; int baseSal = 5400;
Response.Write( objemp.AddBonus(out sal));
Response.Write(objemp.IncrementSalary(ref baseSal));
}
}

Posted in C# FAQ3 Comments

C Training – Pointers (Part 2)

Void pointers

===========

1. What is the use of void pointer ?

A. Void pointer for compiler means, compiler is not sure of to which data it is point to..

For example, int *i ==> means i is an object holds a pointer to a memory location which stores integer.

Lets take an example of a function where you are manipulating a memory location that may points

to any kind of data ( it may be int,float,struct etc .. ) . But you are not sure of which kind of pointer

user may pass with that function, then how you will code this generic function ?

Here comes the usage of void * .. Void pointer has the capability of storing any kind of pointer and retransmitting

it into corresponding pointer .. that means you can use this void pointer to take from a <specific pointer> and reassign

it to same <specific kind of pointer > ..

So your function prototype looks like this

void func( void *vPtr) { ……

…………

}

2. Can I perform arithmetic operation on void pointer ?

A. No You can’t perform arithmetic operation on void pointer.

3. Why I can’t perform arithmetic operation on void * ?

A. Because , since you can store any kind of pointer address into a void *, your compiler will not be sure

what kind of pointer you are going to store at run time. Which means to perform some operations on

pointer your compiler should know what kind of data type you are going to store in that memory location.

4. So can’t I perform arithmetic operations on void * ?

A. Yes you can perform if you know what kind of address you have initialized to that void *.

eg : int *i = Malloc(4 * sizeof(int));

void *vptr = i;

(int *)vptr + 1; // ==> will make sure that vptr will now points to second integer address.

5. Do I need to explicitly typecast while assigning void * to other pointers and vice versa ?

A. Not required , if you are not going to use it for c++ project also. Because C++ is very strict in typecasting.

But in C, it will do typecast for you automatically to other * type.

More on void pointer in later sections to follow…. Throw any kinds of doubts if you have..

Source of this article is Palle Technologies C training

Posted in C - Fresher questions, C Tutorials0 Comments

Best .net Training:Nested classes in c#

Nested classes are the classes  where the class declaration is fully implemented within the declaration of  another class.

These are used mainly for developing object models in our component.

A nested class and the enclosing class might look like the following example:

public class Enclosingclass//this is enclosing class whose class declaration contains the nested class

{

public class Nested class

{

//implement the code of nested class

}

}

How to instantiate the nested class?

To instantiate the nested class in the previous example we can use the following syntax.

Enclosingclass.Nestedclass  objNestedclass=new Enclosingclass.Nestedclass();

I hope this article  will surely helps  u.

Posted in C# FAQ2 Comments

best .net training : Interfaces

Interfaces in C# only contains only abstract members.and does not contain any fields or implementations.Consider the below scenario understand the Interfaces better.
Ex: Palle technologies having a .net team of 10 developers and got an order to develop the software for a multi bed hospital.
Here the classes involved in this applicatiosn is Doctor,Nurse,Compounder,Surgeon,Physician. All these classes has to give the salary details .So Palle technologies asked the developers to implement the classes.
Developer 1: implementing class Doctor
Public Class Doctor  { //writing functionality for getting salary . public int returnSalary(string doctorid) {return sal;}}
Developer 2: Implementing Class Nurse
Public Class Nurse { //implement salary logic. public int getSalary(string nurseId){ return sal;}}
like this all the developers are trying to write the methods which is having similar functionality but with different names. This kills the program or code readability and maintainability. to get rid of this. Implement the Interface called “IEmployee”
Ex: Public interface IEmployee
{int getsal(string empid);}
Classes: Public Class Doctor:IEmployee {//implement the getsal }
Public Class Nurse:IEmployee{//Implement the getSal;}

Interfaces in C# only contains only abstract members.and does not contain any fields or implementations.Consider the below scenario understand the Interfaces better.
Ex: Palle technologies having a .net team of 10 developers and got an order to develop the software for a multi bed hospital.
Here the classes involved in this applicatiosn is Doctor,Nurse,Compounder,Surgeon,Physician. All these classes has to give the salary details .So Palle technologies asked the developers to implement the classes.
Developer 1: implementing class Doctor
Public Class Doctor  { //writing functionality for getting salary . public int returnSalary(string doctorid) {return sal;}}
Developer 2: Implementing Class Nurse
Public Class Nurse { //implement salary logic. public int getSalary(string nurseId){ return sal;}}

like this all the developers are trying to write the methods which is having similar functionality but with different names. This kills the program or code readability and maintainability. to get rid of this. Implement the Interface called “IEmployee”
Ex: Public interface IEmployee
{int getsal(string empid);}
Classes: Public Class Doctor:IEmployee {//implement the getsal }
Public Class Nurse:IEmployee{//Implement the getSal;}

Posted in C# FAQ1 Comment