24 March 2014

difference between i++ and ++i

i++ means 'tell me the value of i, then increment'
++i means 'increment i, then tell me the value'
For the prefix form:
  • x is evaluated to produce the variable
  • the value of the variable is copied to a temporary location
  • the temporary value is incremented to produce a new value (not overwriting the temporary!)
  • the new value is stored in the variable
  • the result of the operation is the new value
For the post fix form:
  • x is evaluated to produce the variable
  • the value of the variable is copied to a temporary location
  • the temporary value is incremented to produce a new value (not overwriting the temporary!)
  • the new value is stored in the variable
  • the result of the operation is the temporary copy

++i is definitely as fast as i++ but it may be faster.
The reason is the implementation.
In order to implement i++ the implementation needs to generate a temporary copy of i unlike the implementation for ++i.
But smart compilers can optimize the generate of this temporary, they certainly do for POD types

Difference between event and delegate?

Event:

1) It is a data member of a type(class/structure)
2)It is declared inside a type(class/structure)
3) It is used to generate notifications which are then passed to methods though
delegates.

delegate:

1)It is a datatype(reference type) that holds references of methods with
some signatures.also called as function pointer.
2)It may or may not be declared inside a class.
3)It is used as the return type of an event and used in passing messages from event to methods.


event-->delegate-->method

example:

namespace dd
{
//delegate declaration
delegate void first();
class cc
{
//event declaration
public first myevent;
}
}

example 2:
button1.Click+=new EventHandler(this.button1_Click);
(Windows Applications)

Click is the event that returns an instance of the EventHandler delegate.
EventHandler delegate has the reference of button1_Click event and that
helps in the communication betwen the Click event and button1_Click method

Access Modifiers in c#.net

Access Modifiers         Access Modifiers are keywords used to specify the declared accessibility of a member of a type.
 
Public is visible to everyone. A public member can be accessed using an instance of a class, by a class's internal code, and by any descendants of a class.

Private is hidden and usable only by the class itself. No code using a class instance can access a private member directly and neither can a descendant class.



Protected members are similar to private ones in that they are accessible only by the containing class. However, protected members also may be used by a descendant class. So members that are likely to be needed by a descendant class should be marked protected.



Internal/Friend is public to the entire application but private to any outside applications. Internal is useful when you want to allow a class to be used by other applications but reserve special functionality for the application that contains the class. Internal is used by C# and Friend by VB .NET.



Protected Internal may be accessed only by a descendant class that's contained in the same application as its base class. You use protected internal in situations where you want to deny access to parts of a class functionality to any descendant classes found in other applications.


18 March 2014

How to get total number of tables in a database in SQL?

SELECT COUNT(*) as TotalTables FROM sys.tables WHERE type in ('u')

Differences Between WCF and ASP.NET Web Services

Important differences between WCF Services and ASP.NET Web Services:

WCF Interview Question



What is WCF?
Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows. WCF provides a runtime environment
 for services, enabling you to expose CLR types as services, and to consume other services as CLR types.

WCF is part of .NET 3.0 and requires .NET 2.0, so it can only run on systems that support it. WCF is Microsoft’s unified programming model for building service-oriented applications with managed code. It extends the .NET Framework to enable developers to build secure and reliable transacted Web services that integrate across
 platforms and interoperate with existing investments.

Windows Communication Foundation combines and extends the capabilities of
 existing Microsoft distributed systems technologies, including Enterprise Services, System.Messaging, Microsoft .NET Remoting, ASMX, and WSE to deliver a unified development experience across multiple axes, including distance (cross-process, cross-machine, cross-subnet, cross-intranet, cross-Internet), topologies (farms, fire-walled, content-routed, dynamic), hosts (ASP.NET, EXE, WindowsPresentation Foundation, Windows Forms, NT Service, COM+), protocols (TCP, HTTP, cross-process, custom), and security models (SAML, Kerberos, X509, username/password, custom).

What is service and client in perspective of
 data communication?
A service is a unit of
 functionality exposed to the world. The client of a service is merely the party consuming the service.

What is endpoint in WCF? or What is three major points in WCF?
Every service must have Address that defines where the service resides,
 Contract that defines what the service does and a Binding that defines how to communicate with the service.

In WCF the relationship between Address,
 Contract and Binding is called Endpoint. The Endpoint is the fusion of Address, Contract and Binding.

1. Address : Specifies the location of the service which will be like http://Myserver/MyService.Clients will use this location to communicate with our service.

2.
 Contract : Specifies the interface between client and the server.It’s a simple interface with some attribute.

3. Binding : Specifies how the two paries will communicate in term of transport and encoding and protocols.

What is binding and how many types of bindings are there in WCF?

A binding defines how an endpoint communicates to the world. A binding defines the transport (such as HTTP or TCP) and the encoding being used (such as text or binary).

A binding can contain binding elements that specify details like the security mechanisms used to secure messages, or the message pattern used by an endpoint.

WCF supports nine types of bindings.

1. Basic binding :
Offered by the BasicHttpBinding class, this is designed to expose a WCF service as a legacy ASMX web service, so that old clients can work with new services. When used by the client, this binding enables new WCF clients to work with old ASMX services.

2. TCP binding :

Offered by the NetTcpBinding class, this uses TCP for cross-machine communication on the intranet. It supports a variety of features, including reliability, transactions,
 and security, and is optimized for WCF-to-WCF communication. As a result, it requires both the client and the service to use WCF.

3. Peer network binding :
Offered by the NetPeerTcpBinding class, this uses peer networking as a transport. The peer network-enabled client and services all subscribe to the same grid and broadcast messages to it.

4. IPC binding :
Offered by the NetNamedPipeBinding class, this uses named pipes as a transport for same-machine communication. It is the most secure binding since it cannot accept calls from outside the machine and it supports a variety of features similar to the TCP binding.

5. Web Service (WS) binding :
Offered by the WSHttpBinding class, this uses HTTP or HTTPS for transport, and is designed to offer a variety of features such as reliability, transactions,
 and security over the Internet.

6. Federated WS binding :

Offered by the WSFederationHttpBinding class, this is a specialization of the WS binding, offering support for federated security.

7. Duplex WS binding :
Offered by the WSDualHttpBinding class, this is similar to the WS binding except it also supports bidirectional communication from the service to the client.

8. MSMQ binding :

Offered by the NetMsmqBinding class, this uses MSMQ for transport and is designed to offer support for disconnected queued calls.

9. MSMQ integration binding :

Offered by the MsmqIntegrationBinding class, this converts WCF messages to and from MSMQ messages, and is designed to interoperate with legacy MSMQ clients.

What is contracts in WCF?
In WCF, all services expose contracts. The
 contract is a platform-neutral and standard way of describing what the service does.

WCF defines four types of contracts.
1. Service contracts : Describe which operations the client can perform on the service.

2. Data contracts : Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but we can easily define explicit opt-in data contracts for custom types.

3. Fault contracts : Define which errors are raised by the service, and how the service handles and propagates errors to its clients.

4. Message contracts : Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an
 existingmessage format we have to comply with.

What is address in WCF and how many types of transport schemas are there in WCF?
Address is a way of letting client know that where a service is located. In WCF, every service is associated with a unique address. This contains the location of the service and transport schemas. 

WCF supports following transport schemas
1. HTTP
2. TCP
3. Peer network
4. IPC (Inter-Process Communication over named pipes)
5. MSMQ

The sample address for above transport schema may look like

http://localhost:81
http://localhost:81/MyService
net.tcp://localhost:82/MyService
net.pipe://localhost/MyPipeService
net.msmq://localhost/private/MyMsMqService
net.msmq://localhost/MyMsMqService

What is the difference WCF and Web services?
1. Web services can only be invoked by HTTP. While Service or a WCF component can be invoked by any protocol and any transport type.

2. Second web services are not flexible. But Services are flexible. If you make a new version of the service then you need to just expose a new end point. So services are agile and which is a very practical approach looking at the current business trends.

How can we host a service on two different protocols on a single server?
Let’s first understand what this question actually means. Let’s say we have made a service and we want to host this service using HTTP as well as TCP.

You must be wondering why to ever host services on two different types of protocol. When we host a service it’s consumed by multiple types of client and it’s very much possible that they have there own protocol of communication. A good service has the capability to downgrade or upgrade its protocol according the client who is consuming him.

Let’s do a small sample in which we will host the ServiceGetCost on TCP and HTTP protocol.

Once we are done
 the server side coding its time to see make a client by which we can switch between the protocols and see the results. Below is the code snippet of the client side for multi-protocol hosting

How does WCF work?
Follows the ‘software as a service’ model, where all units of
 functionality are defined as services.

A WCF Service is a program that exposes a collection of Endpoints. Each Endpoint is a portal (connection) for communication with either clients (applications) or
 other services.

Enables greater design flexibility and extensibility of distributed systems architectures.

A WCF application is represented as a collection of services with multiple entry points for communications.

What are the main components of WCF?
1.Service: The working logic or offering, implemented using any .Net Language©.

2.Host: The environment where the service is parked. E.g. exe, process, windows service


3.Endpoints: The way a service is exposed to outside world.

Explain transactions in WCF.
Transactions in WCF allow several components to concurrently participate in an operation. Transactions are a group of operations that are atomic, consistent, isolated and durable. WCF has features that allow distributed transactions. Application config file can be used for setting transaction timeouts.

What are different isolation levels provided in WCF?
The different isolation levels:

1. READ UNCOMMITTED: – An uncommitted transaction can be read. This transaction can be rolled back later.

2. READ COMMITTED :- Will not read data of a transaction that has not been committed yet


3. REPEATABLE READ: – Locks placed on all data and another transaction cannot read.

4. SERIALIZABLE:- Does not allow other transactions to insert or update data until the transaction is complete.

How do I serialize entities using WCF?
LINQ to SQL supports serialization as XML via WCF by generating WCF serialization attributes and special serialization specific logic during code-generation. You can turn on this feature in the designer by setting serialization mode to ‘Unidirectional’. Note this is not a general solution for serialization as unidirectional mode may be insufficient for many use cases.

What is End point ?
Every service must have Address that defines where the service resides, Contract that defines what the service does and a Binding that defines how to communicate with the service. In WCF the relationship between Address, Contract and Binding is called Endpoint.

The Endpoint is the fusion of Address, Contract and Binding.

15 March 2014

Client side validate checkboxlist using custom validator in Asp .Net



        In this article we will see how to validate checkboxlist at client side to make sure that at least one item is checked. To do this we will use custom validator control and javascript. 

Java script

<script type="text/javascript">
    function ValidatenumberList(source, args) {
        var chkListModules = document.getElementById('<%= cbl_number.ClientID %>');
        var chkListinputs = chkListModules.getElementsByTagName("input");
        for (var i = 0; i < chkListinputs.length; i++) {
            if (chkListinputs[i].checked) {
                args.IsValid = true;
                return;
            }
        }
        args.IsValid = false;
    }
   </script>


asp.net code:
<asp:checkboxlist id="cbl_number" runat="server">
<asp:listitem text="1" value="1"></asp:listitem>
<asp:listitem text="2" value="2"></asp:listitem>
<asp:listitem text="3" value="3"></asp:listitem>
<asp:listitem text="4" value="4"></asp:listitem>
<asp:listitem text="5" value="5"></asp:listitem>
<asp:listitem text="6" value="6"></asp:listitem>
<asp:listitem text="7" value="7"></asp:listitem>
</asp:checkboxlist>

<asp:customvalidator runat="server" forecolor="Red" id="Customvalidator1" clientvalidationfunction="ValidatenumberList" errormessage="Please select atleast one number."></asp:customvalidator>

<asp:button text="Select Number" id="btnTest" runat="server"></asp:button>