Applying for the position of a developer is often a harrying experience that can leave you drained. Not only are the pressures of being interviewed brought to bear on the aspiring candidate, but the very nature of .net technology makes it open to several ambiguities that often stump beginners. Whether you are looking to enter this field or further your career, this set of .net interview questions compiled from the vast pool of expert resources available on the internet will be of great value.
If you need to brush up on C# and .NET, consider this 3 course series:
This .NET interview questions compilation is divided into four sections and forty nine questions in all, in order to make it easier to follow and study. Knowing these questions and their corresponding answers will make you better-prepared to handle the interview.
The sections are segregated as follows and do not, by any means, comprise a comprehensive compilation of knowledge about .NET and all its capabilities and avatars. Yet, they are a vital starting point in your quest for that dream developer job.
Take the The Art of Unit Testing & TDD Master Class in .NET class here!
I. The Basics
III. Remoting and Web Services
1. What is .NET?
.NET is a general-purpose software framework developed by Microsoft – a development platform that is similar to Java. At its core is a virtual machine that turns intermediate language (IL) into machine code. High-level language compilers for C#, VB.NET and C++ areprovided to turn source code into IL. C# (pronounced C-Sharp) is a new programming language and is very similar to Java. An extensive class library is included, featuring all the functionality one might expect from a contemporary development platform -windows GUI development (Windows Forms), database access (ADO.NET),web development (ASP.NET), web services, XML etc.
2. What are the Versions of .NET?
The final version of the 1.0 SDK and runtime was released on 15-Jan-2002. At the same time, the final version ofVisual Studio.NET was made available to MSDN subscribers..NET 1.1 was released in April 2003 – it’s mostly bug fixes for 1.0. In 2005, version 2.0 was released and was distributed with Windows Server 2003 R2. Subsequent versions were released from 2006 through 2013. The latest release – version 4.5.1, which was released in October 2013 – uses Visual Studio 2013 and is distributed with Windows 8.1 and Windows Server 2012 R2.
3. What operating systems does the .NET Framework run on?
Following is a table of all the versions of .NET and the operating systems they support.
1.x and 2.0 work all the way back to Win98 but may not work with Windows 8
2.0 Supported Operating Systems
· Microsoft Windows 98/ME
· Windows 2000
· Windows XP
· Windows Vista (included with OS)
· Windows Server 2003
· Windows Server 2008 (included with OS)
.NET Framework 3.0
· Microsoft Windows 2003 Server Service Pack 1 (SP1)
· Windows XP SP2
· Windows Vista (included with OS)
· Windows 7
· Windows 8
Note: Windows Vista comes with .NET Framework 3.0. There is no separate installation package required. The standalone .NET Framework 3.0 packages are not supported on Vista.
.NET Framework 3.5
· Microsoft Windows XP
· Microsoft Windows Server 2003
· Windows Vista (included with OS)
· Windows 7
· Windows 8
· Windows Server 2003/2008
.NET Framework 4.0
· Microsoft Windows XP (but not Starter, Media Center or Tablet editions)
· Microsoft Windows Server 2003
· Windows Vista
· Windows 7 (included with OS)
· Windows 8
· Windows Server 2008
.NET Framework 4.5
· Windows Vista SP2
· Windows 7
· Windows 8 (included with OS)
· Windows Server 2008 SP2/R2
· Windows Server 2012 (included with OS)
4. What tools are available for .NET app development?
.NET Framework SDK
ASP.NET Web Matrix
Microsoft Visual C#.NET
Microsoft Visual Studio
In addition to these, there are also OpenSource tools that many developers use for web and other applications.
5. What is an IL?
Alternatively: Explain CIL (or MSIL) and What is JIT?
(IL)Intermediate Language is also known as MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language). All .NET source code is compiled to IL. This IL is then converted to machine code at the point where the software is installed, or at run-time by a Just-In-Time (JIT) compiler.
6. What is a CLR?
The full form of CLR is Common Language Runtime, and it is at the core of the .NET framework. All languages have a runtime that manages the code execution of the program. For example, VC++ has MSCRT40.DLL,VB6 has MSVBVM60.DLL, Java has Java Virtual Machine etc.
Similarly .NET has CLR. Below are the responsibilities of the CLR:
· Garbage Collection: The CLR automatically manages memory thus eliminating memory leaks. When objects are not referred, the GC automatically releases those memories, thus providing efficient memory management.
· Code Access Security: CAS grants rights to program depending on the security configuration of the machine. For example, the program has rights to edit or create a new file but the security configuration of the machine does not allow the program to delete a file. CAS will ensure that the code runs under the environment of the machine’s security configuration.
· Code Verification: This ensures proper code execution and type safety while the code runs. It prevents the source code from performing illegal operations such as accessing invalid memory locations, etc.
· IL-to-native translators and optimizers: CLR uses JIT, compiles the IL code to machine code and then executes it. The CLR also determines, depending on the platform,the best way to run the IL code.
7. What is a CTS?
In order for two languages to be able to communicate smoothly, the CLR has a CTS (Common Type System). For example, in VB you have “Integer”, and in C++ you have “long”; these datatypes are not compatible, so the interfacing between them is very complicated. In order for two different languages to communicate, Microsoft introduced the Common Type System. Therefore, “Integer” datatype in VB6 and “int” datatype in C++ will be converted to System.int32, which is a datatype of CTS.
8. What is a CLS (Common Language Specification)?
This is a subset of the CTS which all .NET languages are expected to support. CLS is one step towards Microsoft’s goal of unifying various languages under one umbrella. It was always a dream of Microsoft to unite all different languages into one umbrella. CLS is a set of guidelines for a language to follow so that it can communicate with other .NET languages in a seamless manner.
9. What is Managed Code?
Managed code is that which runs inside the CLR environment. From that perspective, all ILs fall under managed code, but if you are using third party software such as a VB6 or VC++ component they are unmanaged code, as the .NET runtime (CLR) does not have control over the source code execution of the language.
10. What is an Assembly?
An assembly is a fully functional deployment unit such as a DLL or an EXE. It may be a group of such files that represent the resource, type definitions and implementations of those types. A single assembly can also contain references to other units such as itself, and these groups are known as a manifest – essentially, a block of data that describe resources, types and references.
11. What is the function of an assembly?
An assembly is completely self-describing, and contains metadata information that is used by the CLR for everything from type checking and security to actually invoking the components’ methods. Since all information is in the assembly itself, it is independent of the registry. This is an advantage over COM, where the versions are stored in the registry.
12. What is the difference between private and shared (shared) assemblies?
For private assembly deployment, the assembly is copied to the same directory as the client program that references it. No registration is needed, no registry clean-ups are required when such an assembly is removed and there is no need for an uninstall program. Deleting it from the hard drive is all that is required.In shared assembly deployment, an assembly is installed in the Global Assembly Cache (or GAC). The GAC contains shared assemblies that are globally accessible to all .NET applications on the machine.
An important aspect of assemblies is that they are part of the identity of a type. The identity of a type is the assembly that houses it combined with the type name. This means, for example, that if assembly A exports a type called T, and assembly B exports a type called T, the .NET runtime sees these as two completely different types. Furthermore, don’t get confused between assemblies and namespaces – namespaces are merely a hierarchical way of organizing type names. To the runtime, type names are type names,regardless of whether namespaces are used to organize the names. It’s the assembly plus the typename (regardless of whether the type name belongs to a namespace) that uniquely indentifies a type to the runtime.Assemblies are also important in .NET with respect to security – many of the security restrictions are enforced at the assembly boundary.Finally, assemblies are the unit of versioning in .NET
13. What is NameSpace and what are its functions?
Namespaces are merely a hierarchical way of organizing type names. Their functions are as follows:
· They logically group types. For example:System.Web.UI logically groups UI-related features.
· In the Object Oriented world, it is possible that programmers will use the same class names. By qualifying NameSpace with classname, this collision can be removed.
14. What are the differences between namespace and assembly?
An assembly is a physical grouping of logical units. A namespace, on the other hand, groups classes in a logical manner. In addition, a namespace can span multiple assemblies.
15. What is a Manifest?
A manifest is where all assembly metadata is stored. This metadata contains the following:
· Version of the assembly
· Scope of the assembly
· Security identity
A manifest resolves references to resources and classes, and can either be stored as a standalone PE file (.dll or .exe) or with MSIL code.
16. What is a GAC and when is it used?
GAC stands for Global Assembly Cache, and it is where shared .NET assemblies reside. The GAC is used when:
· the application has to be shared among several application, or
· the assembly has some special security requirements, such as only administrators being allowed to remove it. If the assembly is private then a simple delete will suffice.
17. What is garbage collection?
This is a feature of the CLR that helps manage memory.CLR runs on non-deterministic to see the unused objects and cleans them by automatically cleaning them when they are no longer in use or referenced. The garbage collector can be forced to run using System.GC.Collect(). However, it is not recommended unless the situation calls for it.
18. What is reflection?
All .NET assemblies have metadata information stored about the types defined in modules. This metadata information can be accessed by a mechanism called “Reflection”.System.Reflection can be used to browse through the metadata information. With reflection, you can also dynamically invoke methods using System.Type.Invokemember. Below is a sample source compiled using VB.NET 2005.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim PobjtypeAs Type
Dim PobjObjectAs Object
Dim PobjButtons As New Windows.Forms.Button()
Pobjtype = PobjButtons.GetType()
For Each PobjObjectInPobjtype.GetMembers
Figure: Sample reflection display
19. What are Value types and Reference types ?
Value types directly contain their data which are either allocated on the stack or allocated in-line in a structure. Reference types store a reference to the value’s memory address, and are allocated on the heap. Reference types can be self-describing, pointer, or interface types. Variables that are value types each have their own copy of the data and, therefore, operations on one variable do not affect other variables. Variables that are reference types can refer to the same object; therefore, operations on one variable can affect the same object referred to by another variable. All types are derived from the System.Object base type.
20. What are boxing and unboxing?
Boxing is a process in which object instances are created and copy values in to that instance.Boxing permits any value type to be implicitly converted to a type object or to any interface type implemented by value type. Unboxing is the opposite of boxing, where the value is copied from the instance into the appropriate storage location. Below is a sample code of boxing and unboxing, where an integer data type is converted into object, and then unboxed.
Dim x As Integer
Dim y As Object
x = 10
‘ boxing process
y = x
‘ unboxing process
x = y
21. What is Code Access Security?
CAS is part of the .NET security model that determines whether or not a piece of code is allowed to run, and what resources it can use while running. For example, CAS will allow an application to read but not to write and delete a file or a resource from a folder.
1. How can we use COM Components in .NET?
.NET components communicate with COM using RCW (Runtime Callable Wrapper). Following are some methods to generate an RCW:
· Adding reference in Visual Studio.net. Wrapper class is generated and placed in the “BIN” directory.
· Using Type library import tool. Tlbimp.exe yourname.dll.
· Using interopservices.System.runtime.Interopservices namespace contains class TypeLib Converter, which provides methods to convert COM classes and interface into assembly metadata.
· If your COM component does not have type library then the only way to communicate is writing custom wrappers. That means communicating directly with COM components.
2. How can we use .NET components in COM?
.NET components cannot be used in a straightforward way with COM. You will need to create CCWs in order for COM components to communicate with .NET assemblies. Following are different ways to implement this:
· Explicitly declare interfaces.
· By using InteropServices attributes. Here interfaces are created automatically. Following are different type of class attributes:
§ None:-No class interface is generated for the class. This is default setting when you do not specify anything.
§ AutoDispatch :- Interface that supports IDispatch is created for the class. However, no type information is produced.
§ AutoDual :- A dual interface is created for the class. Type information is produced and made available in the type library.
Public Class ClsCompliant
3. How can we make Windows API calls in .NET?
Windows API calls are not COM based and they are invoked through Platform Invoke Services.
Declare StringConversionType (Function | Sub) MethodName Lib “DllName” ([Args]) As Type
· StringConversionType is to specify the type of conversion that should take place. Either we can specify Unicode to convert all strings to Unicode values, or Auto to convert strings according to the .NET runtime rules.
· MethodName is the name of the API to call.
· DllName is the name of the DLL.
· Args are any arguments to the API call.
4. When we use Windows API in .NET is it managed or unmanaged code?
Windows API in .NET is unmanaged code.
5. What is COM?
Microsoft’s COM is a technology for component software development. It is a binary standard which is language-independent. DCOM is a distributed extension of COM.
6. What is reference counting in COM?
Reference counting is a memory management technique used to count how many times an object has a pointer referring to it. The first time it is created, the reference count is set to one. When the last reference to the object is nulled, the reference count is set to zero and the object is deleted. Care must be taken to prevent a context switch from changing the reference count at the time of deletion.
7. What is DCOM and how does it differ from COM?
DCOM differs from COM in that it allows for creating objects distributed across a network, a protocol for invoking that object’s methods, and secures access to the object. DCOM provides a wrapper around COM, hence it is a backwards compatible extension. DCOM uses Remote Procedural Calls (RPC) using Open Software Foundation’s Distributed Computing Environment.
These RPC are implemented over TCP/IP and named pipes. The protocol which is actually being used is registered just prior to use, as opposed to being registered at initialization time. The reason for this is that if a protocol is not being used, it will not be loaded.
In order to inform an object that the client is still alive, periodic pinging is used. Hence, when the client has died and no ping has been received (to refresh it) before the expiration time, the server object will perform some clean up tasks (including decrementing its reference count).
Since RPC across a network are typically slow (compared to processes residing on the same machine), DCOM sends multiple requests in the same call.
For example, in COM, the program performs a QueryInterface, one interface at a time. In DCOM, multiple QueryInterfaces are all clustered into one call. This clustering optimization trick is also used when creating an instance of the object and serializing it with data. Since these two operations usually occur together, DCOM allows one method which will perform both operations in one call without waiting for an acknowledgment from the first task before performing the second one.
Similarly, when a client pings its server object, he can do it in one call. Moreover, if there are multiple clients sending pings to multiple servers, an optimization is made where the multiple pings going to the same object are consolidated into just one ping. This is to cut down on the use of precious bandwidth used only for pinging.
The client has the control to set the computer which will be responsible for the lifetime of the object. That is to say, these objects are not created just somewhere where the system resources and access privileges allow for it.
Call security is implemented in all four ways: authentication (to prevent false clients from impersonating the true client), authorization (to insure that a client only does what it is authorized to do), data integrity (to insure that data was not tampered with during transit) and data privacy (to insure that only designated sources can read it). The security issues are handled as they are on operating systems. The client gives the server various access privileges to access memory or disk space.
8. How do we create a DCOM object in VB6?
Using the CreateObject method, you can create a DCOM object. You have to put the server name in the registry in order to do this.
9. How to implement DTC in .NET?
DTC is implemented using COM+. Following are the steps to implement COM + in .NET:
· “EnterpriseServices” namespace has all the classes by which we can implement DTC in .NET. You have to add the reference “EnterpriseServices” namespace.
· Your class must derive from “Serviced Component” object.
· You then have to define your class with the transaction attribute.
· After the class level transaction type is defined, it is time to define the AutoComplete attribute at the method level. The Autocomplete attribute says that if no exception is thrown, then mark that part of the transaction as okay. This helps cut down on the amount of code required. If the implementation sets AutoComplete to false or omits it all together, then we would need to manage the transaction manually. To manually control the transaction, you will need to use the ContextUtil class and its static members.
· The components derived from “ServicedComponent” should be strong-named as they run under COM+.
· Once the classes are compiled using the strong name, register the component in COM+ services with: regsvcs c:\DllPath\TransactionComponent.dll
10. How many types of transactions are there in COM + .NET?
There are 5 transactions types that can be used with COM+. Whenever an object is registered with COM+ it has to abide by one of these 5 transaction types –
Disabled: There is no transaction. COM+ does not provide transaction support for this component.
Not Supported:Component does not support transactions; therefore, even if the calling component in the hierarchy is transaction-enabled, this component will not participate in the transaction.
Supported: Components with transaction type support will be part of the transaction. This will be only if the calling component has an active transaction. If the calling component is not transaction-enabled this component will not start a new transaction.
Required:Components with this attribute require a transaction: i.e., the calling should have a transaction in place, else this component will start a new transaction.
Required New:Components enabled with this transaction type always require a new transaction. Components with required new transaction type instantiate a new transaction for themselves every time.
11. How do you do object pooling in .NET?
COM+ reduces overhead by creating objects from scratch. So in COM+ when an object is activated, it is activated from pool; and when it is deactivated it is pushed back to the pool. Object pooling is configured by using the “ObjectPoolingAttribute” to the class. When a class is marked withobjectpooling attribute it cannot be inherited.
Remoting and Web Services
1. What is an AppDomain?
Previously, processes were used as security boundaries. A process had its own virtual memory and did not overlap with another process’s virtual memory; by dint of this, one process could not crash another. Therefore, any problem or error in one process did not affect another process. With .NET, they went one step further, introducing application domains, or appdomains. In appdomains, multiple applications can run in the same process without influencing each other. If one of the application domains throws up an error, it does not affect the others. To invoke a method in an object running in a different appdomain, .NET remoting is used.
2. What is .NET Remoting?
.NET remoting is essentially a replacement for DCOM. Using .NET remoting, you can make remote object calls for objects that reside in different app domains. Because the remote objects run in a different process,the client cannot call the remote object directly. So, the client uses a proxy that looks like a real object. When the client wants to make a method call on the remote object, it uses a proxy for it. These method calls are called “messages.” Messages are serialized using “formatter” class and sent to a client “channel”. The client channel communicates with the server channel. The Server channel is used as a formatter to deserialize the message and send it to the remote object.
3. Which class does the remote object have to inherit?
All remote objects should inherit from System.MarshalbyRefObject.
4. What are the two different types of remote object creation modes in .NET?
There are two different ways in which object can be created using remoting:
oSAO (Server Activated Objects).also called “well-known call mode.” SAO has two modes:
§ “Single Call”: With single call, an object is created with every method call, thus making the object stateless.
§ “Singleton”: With Singleton, the object is created only once and is shared with all clients.
oCAO (Client Activated Objects)
§ CAO are stateful when compared to SAO. In CAO, the creation request is sent from the client side. The client holds a proxy to the server object created on server.
5. Is it good design practice to distribute the implementation to a remoting client?
It is never advisable to distribute complete implementation at client, because:
· Anyone can use ILDASM and decrypt your logic.
· It is a bad architecture move to have full implementation at client side, as any changes in implementation on server side have to be redistributed again. The best way is to have an interface or SOAPSUDS-generated meta-data DLL at the client side rather than having full implementation.
6. What is a Web Service?
Web Services are business logic components that provide functionality via the Internet using standard protocols such as HTTP. Web Services use Simple Object Access Protocol (SOAP) in order to expose business functionality.SOAP defines a standardized format in XML, which can be exchanged between two entities over standard protocols such as HTTP. SOAP is platform-independent, so the consumer of a web service is completely shielded from any implementation details about the platform exposing the web service. For the consumer, it is simply a black box of ‘send and receive’ XML over HTTP. Therefore, any web service hosted on Windows can also be consumed by UNIX and LINUX platforms.
7. What is UDDI?
UDDI stands for Universal Description, Discovery and Integration. It is a directory that can be used to publish and discover public web services. http://www.UDDI.org contains all the details regarding this.
8. What is DISCO?
DISCO is the abbreviated form of Discovery. It is basically used to club or group common services together on a server, and provides links to the schema documents of the services it describes.
9. What is WSDL?
Web Service Description Language (WSDL)is a W3C specification that defines XML grammar for describing web services.XML grammar describes details such as where to find the web service (its URI), the methods and properties that the service supports, the data type support and supported protocols. In short, it is a comprehensive description of what the webservice can do.Clients can consume this WSDL and build proxy objects that clients then use to communicate with the web services. Full WSDL specs are available at http://www.w3.org/TR/wsdl.
10. What are the different phases of/steps in acquiring a proxy object in web service?
The following are the different steps needed to get a proxy object of a web service at the client side:
· Client communicates to UDI node for web service either through browser or UDDI’s public web service.
· UDII responds with a list of web service
· Every service listed by web service has a URI pointing to a DISCO or WSDL document.
· After parsing the DISCO document, we follow the URI for the WSDL document related to the web service required.
· The client then parses the WSDL document and builds a proxy object which can communicate with web service.
1. Can you explain ADO.NET in brief?
ADO.NET is a very important feature of the .NET Framework, which is used to work with data that is stored in structured data sources such as databases and XML files. Theseare some of the important features of ADO.NET:
● Contains a number of classes that provide you with various methods and attributes to manage the communication between your application and data source.
● Enables you to access different data sources, such as Microsoft SQL Server, and XML, as per your requirements.
● Provides a rich set of features, such as connection and commands that can be used to develop robust and highly efficient data services in .NET applications.
● Provides various data providers that are specific to databases produced by various vendors. For example, ADO.NET has a separate provider to access data from Oracle databases; whereas, another provider is used to access data from SQL databases.
2. Can you explain the basic architecture of ADO.NET?
ADO.NET consists of two fundamental components:
● The DataSet, which is disconnected from the data source and does not need to know where the data that it holds is retrieved from.
● The .net data provider, which allows you to connect your application to the data source and execute the SQL commands against it.
The data provider contains the Connection, Command, DataReader, and DataAdapter objects. TheConnection object provides connectivity to the database. The Command object provides access to database commands to retrieve and manipulate data in a database. The DataReader object retrieves data from the database in the read-only and forward-only mode. The DataAdapter object uses Command objects to execute SQL commands. The DataAdapter object loads the DataSet object with data and also updates changes that you have made to the data in the DataSet object back to the database.
3. What are the benefits of using of ADO.NET in .NET 4.0?
Following are the benefits of using ADO.NET in version 4.0
Language-Integrated Query (LINQ) – Adds native data-querying capabilities to .NET languages by using a syntax similar to that of SQL. This means that LINQ simplifies querying by eliminating the need to use a separate query language. LINQ is an innovative technology that was introduced in .NET Framework 3.5.
LINQ to DataSet – Allows you to implement LINQ queries for disconnected data stored in a dataset. LINQ to DataSet enables you to query data that is cached in a DataSet object. DataSet objects allow you to use a copy of the data stored in the tables of a database, without actually getting connected to the database.
LINQ to SQL – Allows you to create queries for data stored in SQL server database in your .NET application. You can use the LINQ to SQL technology to translate a query into a SQL query and then use it to retrieve or manipulate data contained in tables of an SQL Server database. LINQ to SQL supports all the key functions that you like to perform while working with SQL, that is, you can insert, update, and delete information from a table.
SqlClient Support for SQL Server 2008 – Specifies that with the starting of .NET Framework version 3.5 Service Pack (SP) 1, .NET Framework Data Provider for SQL Server (System.Data.SqlClient namespace) includes all the new features that make it fully compatible with SQL Server 2008 Database Engine.
ADO.NET Data Platform – Specifies that with the release of .NET Framework 3.5 Service Pack (SP) 1, an Entity Framework 3.5 was introduced that provides a set of Entity Data Model (EDM) functions. These functions are supported by all the data providers; thereby, reducing the amount of coding and maintenance in your application. In .NET Framework 4.0, many new functions, such as string, aggregate, mathematical, and date/time functions have been added.
4. What is the role of the DataSet object in ADO.NET?
One of the major components of ADO.NET is the DataSet object, which always remains disconnected from the database and reduces the load on the database.
5. What is a DataReader object?
The DataReader object helps in retrieving the data from a database in a forward-only, read-only mode. The base class for all the DataReader objects is the DbDataReader class.
The DataReader object is returned as a result of calling the ExecuteReader() method of the Command object. The DataReader object enables faster retrieval of data from databases and enhances the performance of .NET applications by providing rapid data access speed. However, it is less preferred as compared to theDataAdapter object because the DataReader object needs an Open connection till it completes reading all the rows of the specified table.
6. Can you describe the disconnected architecture of ADO.NET’s data access model?
ADO.NET maintains a disconnected database access model. This means that the application never remains connected constantly to the data source. Any changes and operations done on the data are saved in a local copy (dataset) that acts as a data source. Whenever, the connection to the server is re-established, these changes are sent back to the server, in which these changes are saved in the actual database or data source.
7. What is connection pooling?
Connection pooling refers to the task of grouping database connections in cache to make them reusable, because opening new connections every time to a database is a time-consuming process. Therefore, connection pooling enables you to reuse already existing and active database connections whenever required, and increases the performance of your application.
You can enable or disable connection pooling in your application by setting the pooling property to either true or false in the connection string. By default, it is enabled in an application.
Take the The Art of Unit Testing & TDD Master Class in .NET class here!