Thursday, May 15, 2014

C# FirstOrDefault vs SingleOrDefault

Semantical Difference:
  • FirstOrDefault returns a first item of potentially multiple (or default if none exists).
  • SingleOrDefault assumes that there is a single item and returns it (or default if none exists). Multiple items are a violation of contract, an exception is thrown.
Performance Difference
  • FirstOrDefault is usually faster, it iterates until it finds the element and only has to iterate the whole enumerable when it doesn't find it. In many cases, there is a high probability to find an item.
  • SingleOrDefault needs to check if there is only one element and therefore always iterates the whole enumerable. To be precise, it iterates until it finds a second element and throws an exception. But in most cases, there is no second element.
Conclusion
  • Use FirstOrDefault if you don't care how many items there are or when you can't afford checking uniqueness (e.g. in a very large collection). When you check uniqueness on adding the items to the collection, it might be too expensive to check it again when searching for those items.
  • Use SingleOrDefault if you don't have to care about performance too much and want to make sure that the assumption of a single item is clear to the reader and checked at runtime.
  • If you result set returns 1 record:
    • SingleOrDefault returns that record
    • FirstOrDefault returns that record
    If your result set returns many records:
    • SingleOrDefault throws an exception
    • FirstOrDefault returns the first record

Wednesday, September 18, 2013

Difference Between Forms Authentication and Windows authentication


Forms Authentication

·         Forms Authentication is a process to allow only authenticated user with valid credential to view a particular page or group of pages/folders and stop unauthenticated or anonymous user outside the secure boundary.

·         It’s mainly to protect the particular folder or page from the group of user or particular user. Form authentication stores all the user details inside a cookie.

·         User details consist of the User roles, User name. However the page is disable cookies function, then it works by passing all the values in Query string.

Windows authentication

·         Windows authentication is the default authentication method of the ASP.NET application. Windows authentication it will use local windows user and groups to do authentication and authorization for your ASP.NET pages. Web application can use the same security that applies to your windows application which will authenticate to your window like user names, passwords, and permissions. Mainly used for intranet application.

·         In order to use windows authentication, you need to make sure your web application's hosting and client user's machine meet the requirements. Generally, both the client and server machines should be in the same local network environment, and they should be part of the same Windows domain (or trusted domain).

Difference Between Authentication and Authorization


Wednesday, April 17, 2013

Difference between C and C++

C++, as the name suggests, is a superset of C. As a matter of fact, C++ can run most of C code while C cannot run C++ code.

1. C follows the procedural programming paradigm while C++ is a multi-paradigm language(procedural as well as object oriented)
In case of C, importance is given to the steps or procedure of the program while C++ focuses on the data rather than the process.
Also, it is easier to implement/edit the code in case of C++ for the same reason.
2. In case of C, the data is not secured while the data is secured(hidden) in C++
This difference is due to specific OOP features like Data Hiding which are not present in C.
3. C is a low-level language while C++ is a middle-level language (Relatively, Please see the discussion at the end of the post)
C is regarded as a low-level language(difficult interpretation & less user friendly) while C++ has features of both low-level(concentration on whats going on in the machine hardware) & high-level languages(concentration on the program itself) & hence is regarded as a middle-level language.
4. C uses the top-down approach while C++ uses the bottom-up approach
In case of C, the program is formulated step by step, each step is processed into detail while in C++, the base elements are first formulated which then are linked together to give rise to larger systems.
5. C is function-driven while C++ is object-driven
Functions are the building blocks of a C program while objects are building blocks of a C++ program.
6. C++ supports function overloading while C does not
Overloading means two functions having the same name in the same program. This can be done only in C++ with the help of Polymorphism(an OOP feature)
7. We can use functions inside structures in C++ but not in C.
In case of C++, functions can be used inside a structure while structures cannot contain functions in C.
8. The NAMESPACE feature in C++ is absent in case of C
C++ uses NAMESPACE which avoid name collisions. For instance, two students enrolled in the same university cannot have the same roll number while two students in different universities might have the same roll number. The universities are two different namespace & hence contain the same roll number(identifier) but the same university(one namespace) cannot have two students with the same roll number(identifier)
9. The standard input & output functions differ in the two languages
C uses scanf & printf while C++ uses cin>> & cout<< as their respective input & output functions
10. C++ allows the use of reference variables while C does not
Reference variables allow two variable names to point to the same memory location. We cannot use these variables in C programming.
11. C++ supports Exception Handling while C does not.
C does not support it “formally” but it can always be implemented by other methods. Though you don’t have the framework to throw & catch exceptions as in C++.

Difference Between Namespace and Assembly


Assembly will contain Namespaces, Classes, Data types it's a small unit of code for deployment. Assembly defines the name of the .dll file.

Namespace is used in order to avoid conflict of user defined classes.
Namespace:
1) it is a Collection of names wherein each name is Unique.
2) They form the logical boundary for a Group of classes.
3) Namespace must be specified in Project-Properties.
Assembly:
1) It is an Output Unit.
2) It is a unit of Deployment & a unit of versioning.
3) Assemblies contain MSIL code.
4) Assemblies are Self-Describing. [e.g. metadata,manifest]
5)An assembly is the primary building block of a .NET Framework application.
6) It is a collection of functionality that is built, versioned, and deployed as a single implementation unit (as one or more files).
7) All managed types and resources are marked either as accessible only within their implementation unit, or by code outside that unit.

Differences between method overloading and method overriding

Method Overloading
Method Overriding
Definition
In Method Overloading, Methods of the same class shares the same name but each method must have different number of parameters or parameters having different types and order. In Method Overriding, sub class have the same method with same name and exactly the same number and type of parameters and same return type as a super class.
Meaning
Method Overloading means more than one method shares the same name in the class but having different signature. Method Overriding means method of base class is re-defined in the derived class having same signature.
Behavior
Method Overloading is to “add” or “extend” more to method’s behavior. Method Overriding is to “Change” existing behavior of method.
Overloading and Overriding is a kind of polymorphism.Polymorphism means “one name, many forms”.
Polymorphism
It is a compile time polymorphism. It is a run time polymorphism.
Inheritance It may or may not need inheritance in Method Overloading. It always requires inheritance in Method Overriding.
Signature In Method Overloading, methods must have different signature. In Method Overriding, methods must have same signature.
Relationship of Methods In Method Overloading, relationship is there between methods of same class. In Method Overriding, relationship is there between methods of super class and sub class.
Criteria
In Method Overloading, methods have same name different signatures but in the same class. In Method Overriding, methods have same name and same signature but in the different class.
No. of Classes
Method Overloading does not require more than one class for overloading. Method Overriding requires at least two classes for overriding.
Example
Class Add
{
   int sum(int a, int b)
   {
     return a + b;
   }
  int sum(int a)
  {
    return a + 10;
   }
}
Class A  // Super Class
{
  void display(int num)
  {
     print num ;
   }
}
//Class B inherits Class A
Class B //Sub Class
{
  void display(int num)
  {
     print num ;
   }
}

Difference between response.redirect and server.transfer

Respose.redirect
---------------------------------------------------
  • Respose.redirect can use another appliaction url.
  • It has round trip.
  • Its much slower than server.transfer.
  • Here also the data can pass through server variables and query string. It simply redirect the page from one page to another.
  • Eg: Response.Redirect("Default.aspx")
Server.transfer
---------------------------------------------------
  • In server.transfer we can use with in that application only.
  • It has not round trip.
  • it has faster than response.redirect
  • The main advantage of this transfer the first page to second page with better performance. The data can pass through variables, query string and also can retrive from the previous page control value.
        Eg: Server.Transfer("Default.aspx");

NOTE :By the way according to the requirement we can transfer one page to another page.

Tuesday, April 16, 2013

Difference between a Label and Literal control in asp.net

Lable
System.Object
  System.Web.UI.Control
    System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.Label
<asp:Label ID="lblFirstName" runat="server" CssClass="myCssClass"Text="First Name" />
Literal
System.Object
  System.Web.UI.Control
    System.Web.UI.WebControls.Literal 
<asp:Literal id="litFirstName" runat="server" Text="First Name" />
Now, bind your controls with some text :-
lblFirstName.Text = "text";
litFirstName.Text = "text";

Output will be show html of page run-time : 

<span id="lblFirstName">text</span>
<br />
text
------------------------------------------------------------------------------
we see that the label text is wrapped around a span tag and the literal text is simply putting a text in it. 
label control has much more properties than the literal control, so choose wisely. 

Sunday, September 9, 2012

Difference between Web Site and Web application in ASP.NET

• Development Wise Difference

Web Site
1. We can code one page in C# and one page in vb.net .
2. We cannot call (access) public functions from one page to other page.
3. Utility classes / functions must be placed in a special ASP.NET solder(the App_Code folder) 
4. Web Sites do not have a .csproj/.vbproj file that manages the project (the folder that contains the site becomes the project root). 
5. In the Web Site project, each file that you exclude is renamed with an exclude
keyword in the filename.
6. Explicit namespaces are not added to pages, controls, and classes by default, but you can add them manually.

Web Applications
1. Only one programming language allowed per project (Need to decide on programming language when starting project). 
2. We can access public functions from one page to other page.
3. Utility classes / function can be placed anywhere in the  Applications  folder structure. 
4. Web Applications are treated like other .NET Projects and are managed by a project file (.csproj or .vbproj) . 
5. One nice feature of the Web Application Project is it's much easier to exclude files from the project view.
6. Explicit namespaces are added to pages, controls, and classes by default.

• Deployment wise difference

Web Site
1. It has its code in a special App_Code directory and it's compiled into several DLLs (assemblies) at runtime.
2. No need to recompile the site before deployment. 
3. We need to deploy both .aspx file and code behind file. 
4. Small changes to the site do not require a full re-deployment. (We can upload the code file that was changed) 

Web Application
1. web application is precompiled into one single DLL.
2. Site has to be pre-compiled before deployment .
3. Deploy only .aspx page, but not associated code file (the pre-compiled dll will be uploaded) .
4. Even small changes require a full re-compile of the entire site(i.e. if the code for a single page changes the whole site must be compiled) (It requires careful planning to ensure new bugs aren't introduced to the production site when uploading bug fixes or other changes.) 

Thursday, September 6, 2012

Difference between Primary Key and Foreign Key

A primary key is a column which uniquely identifies the records in a table. In a broad sense, a primary key is the mixture of a unique key and an index: A collumn with a primary key is indexed to deliver a faster query, and doesn't allow duplicate values to ensure specific data.
Most programmers recommend all tables having a primary key (and only one) to enhance the speed of queries and overall database performance.
An example of a primary key may be found in a table named "departments," which might have a collumn named "department_number" that uniquely identifies each department in the table with a number.
A foreign key is a column (the child column) in a table which has a corresponding relationship and a dependency on another column (the parent collumn) that is usually in a different table. Parent columns can have multiple child collumns, but a child collumn can only have one parent column.
The child collumn is the collumn with the foreign key; the parent collumn does not have the foreign key "set" on it, but most databases require the parent collumn to be indexed.
Foreign keys are made to link data across multiple tables. A child collumn cannot have a record that its parent collumn does not have.
Say a table named "employees" has 20 employees (rows) in it. There are 4 departments in the "departments" table. All 20 employees must belong to a department, so a collumn in the "employees" table named "department" would point to the primary key in the "departments" table using a foreign key. Now all employees must belong to a department as specified by the "departments" table. If a department isn't specified in the "departments" table, the employee cannot be assigned to it.

Difference between DDL, DML and DCL commands


DDL – Data Definition Language: statements used to define the database structure or schema. Some examples:
  • CREATE – to create objects in the database
  • ALTER – alters the structure of the database
  • DROP – delete objects from the database
  • TRUNCATE – remove all records from a table, including all spaces allocated for the records are removed
  • COMMENT – add comments to the data dictionary
  • RENAME – rename an object
They are called Data Definition since they are used for defining the data. That is the structure of the data is known through these DDL commands.
DML – Data Manipulation Language: statements used for managing data within schema objects. Some examples:
  • SELECT – retrieve data from the a database
  • INSERT – insert data into a table
  • UPDATE – updates existing data within a table
  • DELETE – deletes all records from a table, the space for the records remain
  • MERGE – UPSERT operation (insert or update)
  • CALL – call a PL/SQL or Java subprogram
  • EXPLAIN PLAN – explain access path to data
  • LOCK TABLE – control concurrency
DML commands are used for data manipulation. Some of the DML commands
insert,select,update,delete etc. Even though select is not exactly a DML language command oracle still recommends you to consider SELECT as an DML command.
DML are not auto-commit. i.e. you can roll-back the operations, but DDL are auto-commit
DCL – Data Control Language. Some examples:
  • GRANT – gives user’s access privileges to database
  • REVOKE – withdraw access privileges given with the GRANT command
Data Control Language is used for the control of data. That is a user can access any data based on the priveleges given to him. This is done through DATA CONTROL
It is used to create roles, permissions, and referential integrity as well it is used to control access to database by securing it.
TCL – Transaction Control: statements used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.
  • COMMIT – save work done
  • SAVEPOINT – identify a point in a transaction to which you can later roll back
  • ROLLBACK – restore database to original since the last COMMIT
  • SET TRANSACTION – Change transaction options like isolation level and what rollback segment to use
For revoking the transactions and to make the data commit to the database we use TCL. 

Once we commit we cannot rollback. Once we rollback we cannot commit.
Commit and Rollback are generally used to commit or revoke the transactions that are with regard to DML commands.

Difference between Boxing And Unboxing


Tuesday, September 4, 2012

Difference between Public, Private, Protected .....


Public : Allows access to the class member from any other class.
Private : Allows access to the class member only in the same class.
Protected : Allows access to the class member only within the same class and from inherited classes.
Internal : Allows access to the class member only in the same assembly.
Protected internal : Allows access to the class member only within the same class, from inherited classes, and other classes in the same assembly.
Static : Indicates that the member can be called without first instantiating the class.

Difference between an Interface and an Abstract class


There are quite a big difference between an interface and an abstract class, even though both look similar.
    • Interface definition begins with a keyword interface so it is of type interface
    • Abstract classes are declared with the abstract keyword so it is of type class
    • Interface has no implementation, but they have to be implemented.
    • Abstract class’s methods can have implementations and they have to be extended.
    • Interfaces can only have method declaration (implicitly public and abstract) and fields (implicitly public static)
    • Abstract class’s methods can’t have implementation only when declared abstract.
    • Interface can inherit more than one interfaces
    • Abstract class can implement more than one interfaces, but can inherit only one class
    • Abstract class must override all abstract method and may override virtual methods
    • Interface can be used when the implementation is changing
    • Abstract class can be used to provide some default behavior for a base class.
    • Interface makes implementation interchangeable
    • Interface increase security by hiding the implementation
    • Abstract class can be used when implementing framework
    • Abstract classes are an excellent way to create planned inheritance hierarchies and also to use as non-leaf classes in class hierarchies. 

Difference between .EXE and .DLL files


EXE
  1. It is an executable file, which can be run independently.
  2. EXE is an out-process component, which means that it runs in a separate process.
  3. It cannot be reused in an application.
  4. It has a main function.

DLL
  1. It is Dynamic Link Library that is used as a part of EXE or other DLLs. It cannot be run independently.
  2. It runs in the application process memory, so it is called as in-process component.
  3. It can be reused in an application.
  4. It does not have a main function. 

Difference between Web.config and Machine.config


  • The settings made in the Web.config file are applied to that particular web application only whereas the settings of Machine.config file are applied to the whole asp.net application.
  • Web.config file Setting of asp.net all project Machine.config are setting of server setting and when the web side are implemented time it work all project but Web.config file set all projects
  • Web config will be for that particular aplln whereas the Machine .config will for the whole machine
  • Every ASP.NET application that you has a Web.config file . The settings specified in this will imply only to that application.Whereas Your System will have a Machine.config file in Microsoft.NET\Framework\v1.1.4322\CONFIG Folder which contains specifications and settings at a system level.
  • Web.config file is to override the settings from the Machine.config file. Machine.config file settings are applied to all the webapplications residing on the server while Web.config settings are application specific.
  • Machine. config is configuration file for all the application in the IIS. but Web.config is a configuration file for a application or folder.
  • Machine.config for machine level configuration. but Web.config for a application/folder level configuration

Friday, August 31, 2012

Difference between Data table Copy and the Clone method


Data table Copy and the Clone method:
"Both the Copy and the Clone methods create a new Data Table with the same structure as the original Data Table. The new Data Table created by the Copy method has the same set of Data Rows as the original table, but the new Data Table created by the Clone method does not contain any Data Rows."

In summary, Clone method creates a new Data Table with same structure and constraints but without data and Copy method does same but includes data too.

Copy 

datatable newTableCopy;
newTableCopy= existingTable.Copy();

datatable.copy : 
Creates a new datatable along with same structure - (schemas and constraints applied if any) including data in the existing table.

Clone

datatable newTableClone;
newTableClone= existingTable.Clone();.

datatable.Clone():

Creates a new datatable along with same structure with all schemas and constraints excluding data

Differences Between Authentication and Authorization


Authorization is process that comes after authentication and means that application is checking if user have rights to access to some part of web application. Very common example is administration area of site. Depending of user rights, application will allow or deny access to specific pages on site.

An authentication system is how you identify yourself to the computer. The goal behind an authentication system is to verify that the user is actually who they say they are. 
Authorization 
Once the system knows who the user is through authentication, authorization is how the system decides what the user can do. 


There are three major ways to authenticate and authorize users within an ASP.NET Web application:

(1) Windows integrated authentication.
      Identifies and authorizes users based on the server' s users list

(2) Forms authentication.  
     Directs users to a logon Web form that collects user name and password information, and    then authenticates the user against a user list or database that the application maintains.

(3) Passport authentication.
     Directs new users to a site hosted by Microsoft so that they can register a single user name and password that will authorize their access to multiple Web sites.

Wednesday, August 29, 2012

Difference between ASPX and ASCX

S.No
ASPX
ASCX
1
ASP.NET Page uses the extension .aspx
For eg: Default.aspx
User Control uses the extension .ascx
For eg: WebUserControl.ascx.
2
ASP.NET Page begin with a Page Directive.
For eg:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default" %>

User Control begin with a Control Directive.
For eg:
<%@ Control Language="C#"
AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs"
Inherits="WebUserControl" %>

3
Usercontrol code can be used in webforms
We can not use webforms in usercontrol.

4
ASP.NET Page can be viewed directly in the Browser.

User Control cannot be viewed directly in the browser. User Controls are added to WebPages and we view them by requesting a web page in our browser
5
ASP.NET page has HTML, Body or Form element.
User Control does not have a HTML, Body or Form element.

Difference between IN and EXISTS


S.NoINEXISTS
1Returns true if specified value matches any value in the sub query or a list.Return true if sub query contain any rows.
2The sub query will run first and then only outer query.The Outer query will ran first and then only sub query.
3IN is slower than EXISTS. The IN is used in the widely For Static variables for eg: select name from table where ID in (Select ID from table2).Exists is faster than IN.The Outer query will run first and then only inner query.So it will reduce the over head. The Exists is useful mostly in IF conditional statements.
4
Example:

SELECT id,
[Name]
FROM dbo.tablea
WHERE id IN (SELECT id
FROM dbo.tableb)
Example:

SELECT id,
[Name]
FROM dbo.tablea AS a
WHERE EXISTS (SELECT id2
FROM dbo.tableb
WHERE id2 = a.id)