Sunday, 29 January 2017

IQ

Find 2nd highest salary using sql
Select TOP 1 Salary
from (SELECT DISTINCT TOP 2 Salary from Employee ORDER BY Salary ASC)
a ORDER BY Salary DESC

Find 2nd highest salary using LINQ

var employee = Employees
                         .OrderByDescending(e => e.Salary)
                         .Skip(1)  //skip(N-1) N is 2
                         .First();

If multiple employees may have equal salary and you wish to return an IEnumerable of all the employees with the second-highest salary you could do

var employees = Employees
                           .GroupBy(e => e.Salary)
                          .OrderByDescending(g => g.Key)
                          .Skip(1)
                           .First();
==========================================================

Call Server Side Code using ASP.NET AJAX and jQuery AJAX
using System.Web.Services;
     try
     {
        //Do here server event
     }
     catch (Exception)
     {
       throw;
     }
}
function MyFunction(Param1, Param2) {                                              
    $.ajax({
        type: "POST",
        url: "MyPage.aspx/MyMethod",
        data: "{ Param1: '" + Param1+ "',Param2: '" + Param2 + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: "false",
        success: function (msg) {
            // On success                
        },
        Error: function (x, e) {
            // On Error
        }
    });

Thursday, 26 January 2017

C# QS

Value type:
A data type is a value type if it holds a data value within its own memory space. It means variables of these data types directly contain their values.
All the value types derive from System.ValueType, which in-turn, derives from System.Object.
For example, consider integer variable int i = 100;
The system stores 100 in the memory space allocated for the variable 'i'. The following image illustrates how 100 is stored at some hypothetical location in the memory (0x239110) for 'i':
Memory allocation for Value Type
Value types:
  • bool,byte,char,decimal,double,enum,float,int

Reference type:

Unlike value types, a reference type doesn't store its value directly. Instead, it stores the address where the value is being stored. In other words, a reference type contains a pointer to another memory location that holds the data.
For example, consider following string variable:
string s = "Hello World!!";
The following image shows how the system allocates the memory for the above string variable.
Referencetypes:String,All arrays, even if their elements are value types,Class,Delegates
Memory allocation for Reference type

Wednesday, 11 January 2017

Differences


  1. Difference between count(*) and count(1)
  • COUNT(*) returns the number of items in a group. This includes NULL values and duplicates.
  • COUNT(1) returns the number of items in a group. This includes NULL values and duplicates.
  • COUNT(ALL expression) evaluates expression for each row in a group and returns the number of nonnull values.
  • COUNT(DISTINCT expression) evaluates expression 

  1. Hashtable vs. Dictionary 
  • Dictionary:

    • It returns error if we try to find a key which does not exist.
    • It is faster than a Hashtable because there is no boxing and unboxing.
    • Only public static members are thread safe.
    • Dictionary is a generic type which means we can use it with any data type.

    Hashtable:

    • It returns null if we try to find a key which does not exist.
    • It is slower than dictionary because it requires boxing and unboxing.
    • All the members in a Hashtable are thread safe,
    • Hashtable is not a generic type.
  • Difference Between IS and AS
                          //  IS str1 variable type of string

                        ISkeyword is used to check the varaibles are same type or not
                        object str1=123;
                       if(str1 is string)
                            {
                                    c.w.l("This is of same type");
                             }

                      As keyword is helps to convert object one type to other type

                      string x=str1 as string
  • Difference Between Ref and Out
  • Difference Between Async and Await
  1. async and await are markers,which mark code positions from where control should resume after task(thread) completes
    EX: Public staticasync void method()
      {
        await Task.run(new Action(LongTask)
          C.W.L("New Thread");
            }
              Public staticvoidLongTAsk()
                {
                  Thread.sleep(20000);
                    }
                    • Difference Between Constant and readonly
                    • Difference Between var and Dynamic
                    • Private constructor
                    • Difference Between Throw and Throw ex

                                          Sunday, 1 January 2017

                                          Multithreading

                                          A Thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
                                          Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.
                                          So far we wrote the programs where a single thread runs as a single process which is the running instance of the application. However, this way the application can perform one job at a time. To make it execute more than one task at a time, it could be divided into smaller threads.
                                          Following are the various states in the life cycle of a thread:
                                          • The Unstarted State: It is the situation when the instance of the thread is created but the Start method is not called.
                                          • The Ready State: It is the situation when the thread is ready to run and waiting CPU cycle.
                                          • The Not Runnable State: A thread is not executable, when:
                                            • Sleep method has been called
                                            • Wait method has been called
                                            • Blocked by I/O operations
                                          • The Dead State: It is the situation when the thread completes execution or is aborted.
                                          • The following program demonstrates main thread execution:
                                          using System;
                                          using System.Threading;
                                          
                                          namespace MultithreadingApplication
                                          {
                                             class MainThreadProgram
                                             {
                                                static void Main(string[] args)
                                                {
                                                   Thread th = Thread.CurrentThread;
                                                   th.Name = "MainThread";
                                                   Console.WriteLine("This is {0}", th.Name);
                                                   Console.ReadKey();
                                                }
                                             }
                                          }