Saturday, 16 March 2013

  Generic List and ArrayList difference in c#?


                1. By using both the ArrayList and Generic List we can store any type of objects.Consider that you are intended to store only the integer values.In this case when we use ArrayList , we can store any type of objects(i.e.,  Integer,String or some custom objects).

Example:
ArrayList objarList=new ArrayList();
objarList.add(123);
objarList.add("senthil");// will compile..but throw run time error.

 It won't throw any compile time error.It will throw only run time error when we iterate values in the  array list by using for each.

But when we use the Generic List ,we can restrict in the compile time itself.If you are intended to store only integer values, it will allow to store only integer values.If you try to store any other value other than integer, then it will throw compile time error.

List<int> objList=new List<int>();
objList.add(123);
objList.add("senthil") // Will result in compile time error.

2.Whenever you are adding the objects in the ArrayList  , at that time boxing will happen.Whenever you are accessing the objects from the ArrayList , at that time unboxing will happen.So it will hurt the performance. But incase of Generic List there will not be any Boxing or UnBoxing problem and also it will provide the better type safety.

Friday, 15 March 2013

Difference between IDENT_CURRENT ,@@IDENTITY and SCOPE_IDENTITY ?

 1.When we use the IDENT_CURRENT , it will return the last identity value generated for a specific table in any session and any scope.
2.When we use the @@IDENTITY , it will return the last identity value generated for any table in the current session, across all scopes.
3.When we use the SCOPE_IDENTITY, it will return the last identity value generated for any table in the current session and the current scope.

USE YourDBName
DROP TABLE Table1
DROP TABLE Table2
GO
CREATE TABLE Table1(id int IDENTITY)
CREATE TABLE Table2(id int IDENTITY(10,1))
GO
CREATE TRIGGER trTable1 ON Table1 FOR INSERT 
AS
BEGIN
   INSERT Table2 DEFAULT VALUES
END
GO
--end of 'trTable1'trigger definition

SELECT   * FROM Table1
--id is empty.

SELECT   * FROM Table2
--id is empty.

--Do the following in Session 1
INSERT Table1 DEFAULT VALUES
SELECT @@IDENTITY      
/*Returns the value 10, which was inserted by the trigger.*/

SELECT SCOPE_IDENTITY()   
// Returns the value 1, which was inserted by the 
INSERT statement 2 statements before this query.

SELECT IDENT_CURRENT('Table2')
// Returns value inserted into Table2, i.e. in the trigger.

SELECT IDENT_CURRENT('Table1')
// Returns value inserted into Table1, which was the INSERT statement 4 stmts before this query.

-- Do the following in Session 2
SELECT @@IDENTITY
//Returns NULL since there has been no INSERT action 
so far in this session.

SELECT SCOPE_IDENTITY()
// Returns NULL since there has been no INSERT action 
so far in this scope in this session.

SELECT IDENT_CURRENT('Table2')
// Returns the last value inserted into Table2.

Why jQuery?

jQuery is very compact and well written JavaScript code .It increases the productivity of the developer by enabling them to achieve critical UI functionality by writing very small amount of code.
  • Used to improve the performance of the application
  • Used to develop most browser compatible web page
  • Used to implement UI related critical functionality without writing hundreds of lines of codes
  • It i fast
  • It is extensible – jQuery can be extended to implement customized behavior

Difference between jQuery-x.x.x.js and jQuery.x.x.x min.js?

    In terms of functionality, there is no difference between the jQuery-x.x.x.js and jQuery-x.x.x-min.js .jQuery-x.x.x-min.js is also called as minified version of jQuery. 

   jQuery-1.4.4.js file size is 178 KB  and the  jQuery-1.4.4-min.js size is  76.7 KB. If you are not using minified version of jQuery,then it will load the 178 kb file( jQuery-1.4.4.js).So it will take the much time to load the page and will hurt the performance.
   So we can prefer using  jQuery-1.4.4.js during development and  jQuery-1.4.4-min.js when we move the code into  production environment.
    
    Use of  jquery-x.x.x-vsdoc.js
            Generally we will use jQuery-x.x.x-vsdoc.js to provide the  intellisense support. We can even delete this file. But the thing is that it won't provide the intellisense support if we delete that file.
     
       
  

Sunday, 10 March 2013

How Dynamic works in C#


     DLR (Dynamic language runtime) is set of services which add dynamic programming capability to CLR. DLR makes dynamic languages like LISP, Javascript, PHP,Ruby to run on .NET framework.

To know more about DLR, please visit the following link:-

         We are going to see how the Dynamics work in .Net now. For example consider the following statement.



          dynamic d=GetSomeData(15); When we execute this statement,Let's see what will happen behind the scenes.
         When the above statement executes,  the DLR  will pass the expression trees to the target object.If the dynamic data type is pointing in memory to a COM object, then the expression tree will be sent to the COM interface named IDispatch. This interface is COM's way of incorporating its own set of dynamic services.
         If the dynamic data is not pointing to a COM object, then the expression tree may be passed to the object implementing the IDynamicObject interface.This interface is used to map the expression tree to Ruby specifics in the dynamic languages like Iron Ruby.
    If suppose the dynamic data is not pointing to COM object and does not implement IDynamicObject, then the object will be considered as .Net object. In this case the expression tree is sent to the C# run time binder for processing.
   After the expression tree has been processed by the given binder, the dynamic data will be resolved to the real in-memory data type after which the correct method is called with any necessary parameters.

To know more about the expression tree basics , please read the following article.

http://www.codeproject.com/Articles/235860/Expression-Tree-Basics


 






 

Thursday, 7 March 2013

<b>Var DYNAMIC</b>

VAR - DYNAMIC DIFFERENCE C#

1. The Var(Implicit typed local variable) keyword is used to define local variables.In  case of  Var , the underlying data type is determined at compile time itself  based on the initial assignment.Once the initial assignment has been made with Var type , then it will become strongly typed.If you try to store any incompatible value with the Var  type it will result in compile time error.

Example:

Var strNameList=new List<string>(); By using this statement we can store list of names in the string format. 
strNameList.add("Senthil");
strNameList.add("Vignesh");

strNameList.add(45); // This statement will cause the compile time error.

    But in Dynamic type, the underlying type is determined only at run time.Dynamic data type is not checked at compile time and also it is not strongly typed.We can assign any initial value for dynamic type and then it can be reassigned to any new value during its life time.

Example:
dynamic test="Senthil";
Console.Writeline(test.GetType())  // System.String

test=1222;
Console.Writeline(test.GetType())  // System.Int32

test=new List<string>();
Console.Writeline(test.GetType())  //System.Collections.Generic.List'1[System.String]

    It doesn't provide IntelliSense  support also.It doesn't give better support when we give work with linq also.Because it doesn't support lambda expressions ,extension methods and anonymous methods.

  Use  Dynamic when: 
    It is better to go with dynamic type , only when our application depends heavily on Reflection services.By using Dynamic we can reduce the amount of code while we are working with reflection.

    If your application needs to communicate with COM components like Microsoft Office Products(i.e.MS Word,MS Excel), then we can prefer using Dynamic.

   If your application needs to communicate with some dynamic languages like Iron Ruby  or Iron Python, then we can prefer using Dynamic.

Use Var when:-
        
      We work with linq.Because many linq queries will return only enumeration of anonymous classes..
      
   
2. We can use the Var keyword only for the local variables.We can't use it as a class level variables,method's parameter or method's return type. But we can use the Dynamic type as a Class level variables, local variables,dynamic property ,dynamic parameter and dynamic return type.

Class VarDynamicClass
        {
        private  var MyVarField;//Not possible
        private dynamic MyDynField;// Possible
        public var MyVarProperty{get;set;}//Not possible
        public dynamic myDynProperty{get;set;}// Possible
        public var MyVarMethod(var param1) //Not possible
            {
           var dynInt=25;   // Possible
           if(param1 is int)
                {
               return param1+dynInt;
                }
            }
        public dynamic MyDynamicMethod(dynamic d1)  // Possible
           {
          if(d1 is int)
             {
               return d1;
             }
           }
  }

3.If you try to call non existing members(i.e variables or methods and so on) with dynamic type , it won't throw any compile time error. It will throw only run time error as it is not checked at compile time. But in case of var type, we can restrict in the compile time itself.
 Example:

  dynamic d=new MyObject();
  d.CallNonExistingMethod()// Won't throw compile time error..will throw run time error.

// Unhandled Exception: Microsoft.Csharp.RuntimeBinder.RuntimeBinderException
// MyObject doesn't contain a definition for 'CallNonExistingMethod'