Recently Updated .Net Interview Questions Part – 3
1.What is .NET?
.NET is a general-purpose software development platform,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++ are provided to turn source code into IL. C# is a new programming language,very similar to Java.
An extensive class library is included,featuring all the functionality one might expect from a contempory development platform – windows GUI development (Windows Form s),database access (ADO.NET),web development (ASP.NET),web services,XML etc.
2.What is the CLI?Is it the same as the CLR?
The CLI (Common Language Infrastructure) is the definition of the fundamentals of the .NET framework – the Common Type System (CTS),metadata,the Virtual Execution Environment (VES) and its use of intermediate language (IL),and the support of multiple programming languages via the Common Language Specification (CLS). The CLR (Common Language Runtime) is Microsoft’s primary implementation of the CLI.
Microsoft also have a shared source implementation known as ROTOR,for educational purposes,as well as the .NET Compact Framework for mobile devices. Non-Microsoft CLI implementations include Mono and DotGNU Portable .NET.
3.What is the CTS,and how does it relate to the CLS?
CTS = Common Type System. This is the full range of types that the .NET runtime understands. Not all .NET languages support all the types in the CTS. CLS = Common Language Specification. This is a subset of the CTS which all .NET languages are expected to support. The idea is that any program which uses CLS-compliant types can interoperate with any .NET program written in any language. This interop is very fine-grained – for example a VB.NET class can inherit from a C# class.
4.What is IL?
IL = Intermediate Language. Also known as MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language). All .NET source code (of any language) is compiled to IL during development. The IL is then converted to machine code at the point where the software is installed,or (more commonly) at run-time by a Just-In-Time (JIT) compiler.
5.What is C#?
C# is a new language designed by Microsoft to work with the .NET framework. In their “Introduction to C#” whitepaper,Microsoft describe C# as follows: “C# is a simple,modern,object oriented,and type-safe programming language derived from C and C++. C# (pronounced “C sharp”) is firmly planted in the C and C++ family tree of languages,and will immediately be familiar to C and C++ programmers. C# aims to combine the high productivity of Visual Basic and the raw power of C++.”
6.What is reflection?
All .NET compilers produce metadata about the types defined in the modules they produce. This metadata is packaged along with the module (modules in turn are packaged together in assemblies),and can be accessed by a mechanism called reflection. The System.Reflection namespace contains classes that can be used to interrogate the types for a module/assembly.
Using reflection to access .NET metadata is very similar to using ITypeLib/ITypeInfo to access type library data in COM,and it is used for similar purposes – e.g. determining data type sizes for marshalling data across context/process/machine boundaries. Reflection can also be used to dynamically invoke methods (see System.Type.InvokeMember),or even create types dynamically at run-time (see System.Reflection.Emit.TypeBuilder).
7.What is an assembly?
An assembly is sometimes described as a logical .EXE or .DLL,and can be an application (with a main entry point) or a library. An assembly consists of one or more files (dlls,exes,html files etc),and represents a group of resources,type definitions,and implementations of those types.
An assembly may also contain references to other assemblies. These resources,types and references are described in a block of data called a manifest. The manifest is part of the assembly,thus making the assembly self-describing. 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 organising type names. To the runtime,type names are type names,regardless of whether namespaces are used to organise 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.
8.What is the difference between a private assembly and a shared assembly?
1. Location and visibility: A private assembly is normally used by a single application,and is stored in the application’s directory,or a subdirectory beneath. A shared assembly is normally stored in the global assembly cache,which is a repository of assemblies maintained by the .NET runtime. Shared assemblies are usually libraries of code which many applications will find useful,e.g. the .NET framework classes.
2. Versioning: The runtime enforces versioning constraints only on shared assemblies,not on private assemblies.
9.How do assemblies find each other?
By searching directory paths. There are several factors which can affect the path (such as the AppDomain host,and application configuration files),but for private assemblies the search path is normally the application’s directory and its sub-directories. For shared assemblies,the search path is normally same as the private assembly path plus the shared assembly cache.
10.Can I write my own .NET host?
Yes. For an example of how to do this,take a look at the source for the dm.net moniker developed by Jason Whittington and Don Box. There is also a code sample in the .NET SDK called CorHost.
11.What is garbage collection?
Garbage collection is a heap-management strategy where a run-time component takes responsibility for managing the lifetime of the memory used by objects. This concept is not new to .NET – Java and many other languages/runtimes have used garbage collection for some time.
12.Is it true that objects don’t always get destroyed immediately when the last reference goes away?
Yes. The garbage collector offers no guarantees about the time when an object will be destroyed and its memory reclaimed. There was an interesting thread on the DOTNET list,started by Chris Sells,about the implications of non-deterministic destruction of objects in C#. In October 2000,Microsoft’s Brian Harry posted a lengthy analysis of the problem. Chris Sells’ response to Brian’s posting is here.
13.Why doesn’t the .NET runtime offer deterministic destruction?
Because of the garbage collection algorithm. The .NET garbage collector works by periodically running through a list of all the objects that are currently being referenced by an application. All the objects that it doesn’t find during this search are ready to be destroyed and the memory reclaimed.
The implication of this algorithm is that the runtime doesn’t get notified immediately when the final reference on an object goes away – it only finds out during the next ‘sweep’ of the heap. Futhermore,this type of algorithm works best by performing the garbage collection sweep as rarely as possible. Normally heap exhaustion is the trigger for a collection sweep.
14.Is the lack of deterministic destruction in .NET a problem?
It’s certainly an issue that affects component design. If you have objects that maintain expensive or scarce resources (e.g. database locks),you need to provide some way to tell the object to release the resource when it is done. Microsoft recommend that you provide a method called Dispose() for this purpose. However,this causes problems for distributed objects – in a distributed system who calls the
Dispose() method. Some form of reference counting or ownership-management mechanism is needed to handle distributed objects – unfortunately the runtime offers no help with this.
15.Do we have any control over the garbage collection algorithm?
A little. For example the System.GC class exposes a Collect method,which forces the garbage collector to collect all unreferenced objects immediately. Also there is a gcConcurrent setting that can be specified via the application configuration file. This specifies whether or not the garbage collector performs some of its collection activities on a separate thread. The setting only applies on multi-processor machines,and defaults to true.
16.What is the lapsed listener problem?
The lapsed listener problem is one of the primary causes of leaks in .NET applications. It occurs when a subscriber (or ‘listener’) signs up for a publisher’s event,but fails to unsubscribe. The failure to unsubscribe means that the publisher maintains a reference to the subscriber as long as the publisher is alive. For some publishers,this may be the duration of the application. This situation causes two problems.
The obvious problem is the leakage of the subscriber object. The other problem is the performance degredation due to the publisher sending redundant notifications to ‘zombie’ subscribers. There are at least a couple of solutions to the problem. The simplest is to make sure the subscriber is unsubscribed from the publisher,typically by adding an Unsubscribe() method to the subscriber. Another solution,documented here by Shawn Van Ness,is to change the publisher to use weak references in its subscriber list.
17.What is serialization?
Serialization is the process of converting an object into a stream of bytes. Deserialization is the opposite process,i.e. creating an object from a stream of bytes. Serialization/Deserialization is mostly used to transport objects (e.g. during remoting),or to persist objects (e.g. to a file or database).
18.Does the .NET Framework have in-built support for serialization?
There are two separate mechanisms provided by the .NET class library – XmlSerializer and SoapFormatter/BinaryFormatter. Microsoft uses XmlSerializer for Web Services,and SoapFormatter/BinaryFormatter for remoting. Both are available for use in your own code.
19.Why is XmlSerializer so slow?
There is a once-per-process-per-type overhead with XmlSerializer. So the first time you serialize or deserialize an object of a given type in an application,there is a significant delay. This normally doesn’t matter,but it may mean,for example,that XmlSerializer is a poor choice for loading configuration settings during startup of a GUI application.
20.Why do we get errors when we try to serialize a Hashtable?
XmlSerializer will refuse to serialize instances of any class that implements IDictionary,e.g. Hashtable. SoapFormatter and BinaryFormatter do not have this restriction.
21.XmlSerializer is throwing a generic “There was an error reflecting MyClass” error. How do I find out what the problem is?
Look at the InnerException property of the exception that is thrown to get a more specific error message.
22.What are attributes?
There are at least two types of .NET attribute. The first type I will refer to as a metadata attribute – it allows some data to be attached to a class or method. This data becomes part of the metadata for the class,and (like other class metadata) can be accessed via reflection.
An example of a metadata attribute is [serializable],which can be attached to a class and means that instances of the class can be serialized. [serializable] public class CTest {} The other type of attribute is a context attribute. Context attributes use a similar syntax to metadata attributes but they are fundamentally different. Context attributes provide an interception mechanism whereby instance activation and method calls can be pre- and/or post-processed. If you have encountered Keith Brown’s universal delegator you’ll be familiar with this idea.
23.What is Code Access Security (CAS)?
CAS is the part of the .NET security model that determines whether or not code is allowed to run,and what resources it can use when it is running. For example,it is CAS that will prevent a .NET web applet from formatting your hard disk.
24.How does CAS work?
The CAS security policy revolves around two key concepts – code groups and permissions. Each .NET assembly is a member of a particular code group,and each code group is granted the permissions specified in a named permission set. For example,using the default security policy,a control downloaded from a web site belongs to the ‘Zone – Internet’ code group,which adheres to the permissions defined by the ‘Internet’ named permission set. (Naturally the ‘Internet’ named permission set represents a very restrictive range of permissions.).
25.How to troubleshoot the CAS problems?
Caspol has a couple of options that might help. First,you can ask caspol to tell you what code group an assembly belongs to,using caspol -rsg. Similarly,you can ask what permissions are being applied to a particular assembly using caspol -rsp.
26.Can I look at the IL for an assembly?
Yes. MS supply a tool called Ildasm that can be used to view the metadata and IL for an assembly.
27.Can source code be reverse-engineered from IL?
Yes,it is often relatively straightforward to regenerate high-level source from IL. Lutz Roeder’s Reflector does a very good job of turning IL into C# or VB.NET.
28.How can I stop my code being reverse-engineered from IL?
You can buy an IL obfuscation tool. These tools work by ‘optimising’ the IL in such a way that reverse-engineering becomes much more difficult. Of course if you are writing web services then reverse-engineering is not a problem as clients do not have access to your IL.
29.What is JIT and how is works?
An acronym for “just-in-time,” a phrase that describes an action that is taken only when it becomes necessary,such as just-in-time compilation or just-intime object activation
30.What is strong name?
A name that consists of an assembly’s identity—its simple text name,version number,and culture information (if provided)—strengthened by a public key and a digital signature generated over the assembly.
Because the assembly manifest contains file hashes for all the files that constitute the assembly implementation,it is sufficient to generate the digital signature over just the one file in the assembly that contains the assembly manifest. Assemblies with the same strong name are expected to be identical.