Monday 2 January 2012

What is the difference between primary key and unique key?

Primary key and unique are Entity integrity constraints

primary key doesnot accept the duplicate values and null values. the unique key doesnot accept the duplicate value but it accept the null values.
All Primary Key is Unique,but unique key is not a primary key.
Primary key is a column or set of columns which uniquely identifies a row in a table.There must be only one primary key for each table.It does not contains any null values.It also does contains any duplicate values.All the values in the primary keycolumn must be unique.It is a unique key.

Unique key is a constraint which is defined on a column in a table to avoid duplicatevalues in that column.But it allows one null value
In SQL Server, you can create a table with multiple unique key but you can't create a table with multiple primary key
Primary Key will create the clustered index in a table. Uniqe key will create the non-clustered index in a table
Primary Key automatically Create Cluster Index While Unqiue key doesn't create.

Difference between a sub and a function?

A Sub Procedure is a method will not return a value A sub procedure will be defined with a Sub keyword
Sub ShowName(ByVal myName As String)
Console.WriteLine (My name is: & myName)
End Sub
A function is a method that will return value(s). A function will be defined with a Function keyword

Function FindSum(ByVal num1 As Integer, ByVal num2 As  Integer)  As Integer 

Dim sum As Integer = num1 + num2 

Return sum 

End Function

What is "Polymorphism" and what are Polymorphism in VB.Net?

Polymorphism is one of the crucial features of OOP. It  means  "one name, multiple forms". It is also called as  Overloading which means the use of same thing for different  purposes. Using Polymorphism we can create as many  functions we want with one function name but with different  argument list. The function performs different operations  based on the argument list in the function call. The exact  function to be invoked will be determined by checking the  type and number of arguments in the function.
Polymorphism  is ability for redefining methods for derived classesin OOPs. its of two types: 1.Compiletime : Method overloading Methods with same name but diff signatures. Example: Public class Calculation public overloads sub Add(x as integer,y as integer)    return x+y End Sub  Public overloads sub Add(x along,y as long, z as long) return x+y+z End sub End class  2.Runtime :Method Overriding  Two or more methods with same name,same signatures but with different implementations.  Example: Public class DrwingObject Public Overridable function Draw() as String return "i am in generic object" End functin End class  Public class Line inherits DrawingObject Public overrides functin Draw() as string return " i am line" End Function End class

What is the Difference between Overriding and overloading?

Overloading 
All the method will share the same name but  it differes based on the parameter, type of parameter and  number of parameter  
overloading does not take return type to differentiate overloaded method.
ex: int add(int a, int b) int add(float a , float b) are overloaded methods
Overriding 
The method in the derived class the has the  same name in the base class and it changes the behaviour or  functionality of the method in the base class.
overriding comes with inheritance concept. here parent and child both contains the same method. and  when execution takes place; child's method is called.

also

 
overriding->     1) method should be public.     2)it need inheritance.     3)it need virtual keyword before it declartion.     4)it have same name with same parameter in diffrent   class.     5)it require non-static method.     6)method should have same datatype. Overloading->     1)method can be different access speicifier.     2)it doesn't need inheritacne.     3)all method should be in same class.     4)method can have diffrent datatypes
also

 
OVERLOADING:- 1) overloaded methods have the same name but  different parameter list. 2)a subclass method can overload a superclass method   eg:-int add(int a, int b) int add(float a , float b) are overloaded methods    OVERRIDING:- it just in inheritance  and the overriding method must hold the same name and the same signatures . the change maybe just in behavior .   The Cat class in the following example is the subclass and  the Animal class is the superclass. The Cat class overrides  eat() method inherited from Animal class.  public class Animal {      public void eat() {         System.out.println("Eat for Animal");     } }  public class Cat extends Animal {      public void eat() {         System.out.println("Eat for Cat");     } }

The difference between out and ref.

"The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed."
This means inside your method implementation which takes an out parameter you have to set a value before you can use it. At least you have to assign a value before you can return.
This also means you can not use the out paramter to pass a value to the method. (With ref you can)
This also means you can not have any logically includes comparison (e.g. if(param>0 ){ ...}) inside your method before you assign a value to your incoming parameter (I know this sounds weird).
Also nice to know:
"The ref and out keywords are treated differently at run-time, but they are treated the same at compile time. Therefore methods cannot be overloaded if one method takes a ref argument and the other takes an out argument."
To see the difference in IL, I used Reflector to see how the IL looks like for out/ref on Value Types (in my case int32):
out:
[out] int32& result
ref:
int32& result
Remarks:
- Most of the Framework functions (e.g. TryParse) use out and not ref (which was surprising to me). But inside the internal implementation the Framework mainly uses ref.
- Properties can not be passed via out or ref. (Under the hood they are function calls)
- It is very handy if you have several Value Types as return values and you don't want to create a class or struct.