Friday, June 17, 2016

Difference Between var and dynamic

1. Var is introduced in 3.0 while dynamic introduced in 4.0.
2. Var is statically typed means type is decided at compile time. Dynamic is dynamically typed means type is decided at runtime.
3. Error caught at compile time in case of var while with dynamic error caught at runtime.
4.Need to initialize at compile time when you are using var. In case of dynamic no need to initialize at compile time.
var obj;
var obj1=null;
Above statement give compile error. While below statement will not give any error.
dynamic obj;
dynamic obj1=null;
5. Intellisense work for var but not for dynamic.
6. Below statement will not compile
var obj =12;
obj = "Hello"
While these statements perfectly fine with dynamic.
dynamic obj =12;
obj = "Hello"
7. var can not be passed as an arguments and function can't return var type. While dynamic can be passed as an argument and function also can return dynamic.
7. var is type safe , dynamic is not.
8. var can't used as a property while dynamic can.
9.var can be use as a local variable only. Member var type not allowed. Dynamic can be use as a member variable as well as local.

Tuesday, June 14, 2016

Difference Between WPF User Control and Custom Control

User Control: When you need to put some controls together than you use user control. Example is RGB color panel. It is same as window because it has code behind file as well xaml file.
It does not support theming for consumers.It is better to stay with consumer application.

Custom Control: When you wan to extent the funtionality of a given control than use Custom Control Like Numeric Drop down is an extended custom control , Which is build by customizing text box.
It support theming for consumers. It is perfect to be 3rd party conrol.


1. Explain design pattern
Design Pattern

2. Difference between var and dynamic
Click Here For Diff

3. Can we have static constructor in abstract class?
Ans : Yes

4. Can we have both in same class static constructor and instance constructor?
Ans : Yes

5. Difference between override and new
Click Here

6. Difference between value type and reference type
7. Is it possible int? i=null
Ans Yes

8. Difference between factory and Abstract Factory
9. Define singleton pattern
10. Where singleton pattern used in .net?
Ans Dispatcher

11. What is the use of using statement?
Ans. Call the Dispose method if object is created using using statement.

12. If any error occur during execution of using block, did dispose method would call?
Ans Yes

13. Is it possible to define two catch statement where super exception class is above sub class?
Ans : Not possible, will give compile time error

14. Implement a queue with the help of two stack
15. Can we assign base class object to child class object?
Ans : No

16. Is static variable allowed inside function C#?
Ans : No, static variable are not allowed inside function in C# while they are allowed in C and C++.

There are two reasons C# doesn't have this feature.

First, it is possible to get nearly the same effect by having a class-level static, and adding method statics would require increased complexity.

Second, method level statics are somewhat notorious for causing problems when code is called repeatedly or from multiple threads, and since the definitions are in the methods, it's harder to find the definitions.

Saturday, June 11, 2016

Table Variable

SQL Serve 2000 introduces a new variable named table. You can say table variable is an alternative to temporary table. Which is better in both it depends on scenario somewhere table variable is good and somewhere temporary table.
You can save record in table variable
  Basic int,
  PF int,
  EmpId varchar

Fill the table

Insert into @SalaryDetail (Basic,PF,EmpId) Select Basic,PF,EmpId from Salary.

Table variables can be used in batches, stored procedures, and user defined functions. It also can be update and delete as well.

Update @SalaryDetail Set PF=PF*8/100 where EmpIt=’2’

Delete from @SalaryDetail where EmpId=’1’
Select Top 10 from @SalaryDetail OrderBy Basic Desc
Scope: Table variable cannot use as input and output parameter in procedure or function. Just like other variable table variable also no longer exist after the procedure exits. No need to drop the table (table variable).
A user defined function can return a table variable.
Performance: A table variable will generally use fewer resources in comparison of temporary variable cause is its well-defined scope. Transactions lock the table variable at last moment so less locking time.
There is no need to recompile the stored procedure in case of table variable unlike temporary table.

Other Features:

Using a temporary table inside of a stored procedure may result in additional re-compilations of the stored procedure. Table variables can often avoid this recompilation hit. For more information on why stored procedures may recompile, look at Microsoft knowledge base article 243586 (INF: Troubleshooting Stored Procedure Recompilation).
Other Features
Constraints are an excellent way to ensure the data in a table meets specific requirements, and you can use constraints with table variables. The following example ensures ProductID values in the table will be unique, and all prices are less then 10.0.
  ProductID int UNIQUE,
  Price money CHECK(Price < 10.0)
You can also declare primary keys. identity columns, and default values.
UPDATE @ProductTotals
  ProductID int IDENTITY(1,1) PRIMARY KEY,
  Name varchar(10) NOT NULL DEFAULT('Unknown')
So far it seems that table variables can do anything temporary tables can do within the scope of a stored procedure, batch, or UDF), but there are some drawbacks.

What is Web API?

WCF was too complicated to create REST services,  that’s why Microsoft introduced ASP.NET Web API to create RESTful services that are capable of providing fully resource oriented services for a broad range of clients including mobiles, browsers and tablets. ASP.NET Web API is a framework for building REST services easily and in a rather simple way. 

A Web API is an API over the web (HTTP) and ASP .Net Web API is a framework that is use to build HTTP Services. Those services can be used by various client including mobile, Tab, or Browser.  ASP .Net Web API is an ideal platform to build RESTful services.

What is REST?

REST stands for Representational State Transfer.
When WCF is developed using REST architectural style they are called RESTful Sevices.
REST introduced by Roy Fielding in 2000 in ‘‘Architectural Styles and the Design of Network-based Software Architectures’.
REST is an architectural pattern that is based on HTTP it uses HTTP requests, HTTP responses, verbs and status codes to communicate. Because they use HTTP so they can be use by any ‘online’ device.
REST does not belong to any specific platform, but is based on Web with HTTP.
In REST Architecture everything is a resource. RESTful web services are highly scalable, light weight  and maintainable and are very commonly used to create APIs for web based applications.

REST is often used with social networking, mobile applications, Web sites, mashup tools, and automated business processes.

The REST style emphasizes that interactions between clients and services is enhanced by having a limited number of operations (verbs).

Flexibility is provided by assigning resources (nouns) their own unique Universal Resource Identifiers (URIs). Because each verb has a specific meaning (GET, POST, PUT and DELETE), REST avoids ambiguity.

REST Starter Kit?

The WCF REST Starter Kit is a set of .NET Framework classes and Visual Studio features and templates that enable users to create and access REST-style Windows Communication Foundation (WCF) services.
RESTful service are easy to use by most of tools.
RESTful services easy to scale in comparison of SOAP.
REST uses a smaller message format than SOAP. So it is faster than SOAP.
Better choice for Cloud computing.
RESTful webservices are fast in comparison of SOAP, No strict specification like SOAP. It take less bandwidth and resources.
Language and Platform independent.
It can use SOAP web services as implementation.
Permit different data format like Plain Text, HTML, XML and JSON.

How to Designing RESTful Services

The very first step in designing RESTful based services is to identify the objects (resources) which will be exposed to the outside world and second step is, to map these resources to a URI. In RESTful based services one should not focus on designing the methods for an application but focus on designing the resources that implement uniform interfaces using standard HTTP verbs (GET, POST, PUT, and DELETE) and their URI (by which URI they can be located). There are several data format which can be used by REST but XML and JSON are mostly used.

Wednesday, June 8, 2016

Why do we use interlocked classes?

Interlocked classes used to achieve synchronization in a multithreaded environment.
They are used for increament,decreament, compareExchange and Exchange values.

Actually increament of a number seems single step operation but it involve three steps.

1. Load value in register
2. Increament it
3. Assign it in variable.

Interlocked classes promised atomocity of all these three steps.
Interlocked classes are fast in comparison of lock statement.

Monday, June 6, 2016

What is open close generic type?

If type is specified at declaration time than it is called close type. And if type is not specified at declaration time than it is called open type.
Ex List<int>, List<string> are close type
Dictionary<T,string>, Dictionary<T,M>,are open type.

What is the use of volatile keyword?

Volatile keyword indicate that a variable may be modified by multiple thread. It always contain the most updated value. There is no need to use lock statement with volatile variable.

Volatile keyword states that a variable may be changed by multiple threads simultaneously. Variable contains most updated value by any thread. Value will be same for all thread.
There is no need to use lock with volatile field.
For nonvolatile variable, complier ensures that a variable accessed by one thread at a time.
Volatile keyword can be used with following types.
1.       Reference type.
2.       Type like sbyte,  byte, int, uint, short, ushort, float, bool,char
3.       Pointer Type, but pointer to volatile not possible.
4.       An enum type
Life without volatile.
class TestNonVolatile
    private bool _loop = true;

    public static void Main()
        Test test1 = new Test();

        // Set _loop to false on another thread
        new Thread(() => { test1._loop = false;}).Start();

        // Poll the _loop field until it is set to false
        while (test1._loop == true) ;

        // The loop above will never terminate!

Loop will never terminate.

There are two possible ways to get the while loop to terminate:
  1. Use a lock to protect all accesses (reads and writes) to the _loop field
  2. Mark the _loop field as volatile

When variable is declared volatile it essentially means that threads should not cache such a variable or in other words threads should not trust the values of these variables instead they are directly read from the main memory.

The volatile keyword states that a field may be modified by multiple threads which are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.

Difference between Aggregation and Composition

Aggeragation implies a relationship where child can exist independently without parent like class and student relationship. But in composition child can not exist without parent like human body and body parts(hands,eyes,ears etc)