Tuesday, February 8, 2011

.Net Interview Question - 2


    1. What is garbage collection?
      Garbage collection is a CLR feature which automatically manages memory.
      CLR automatically releases objects when they are no longer in use and referenced. CLR runs on non-deterministic to see the unused objects and cleans them. One side effect of this non-deterministic feature is that we cannot assume an object is destroyed when it goes out of the scope of a function. We should avoid using destructors because before GC destroys the object it first executes destructor in that case it will have to wait for code to release the umanaged resource. resulting in additional delays in GC. So its recommended to implement IDisposable interface and write cleaup code in Dispose method and call GC.SuppressFinalize method so instructing GC not to call your constructor.
      Assemblies which are critical to your application’s start up time should either be precompiled synchronously or asynchronously with priority 1. Priority 1 and 2 assemblies are precompiled aggressively while Priority 3 assemblies are only precompiled during machine idle-time. Synchronously precompiling your critical assemblies guarantees that the native images will be available prior to the first time your end user launches the application but increases the time taken to run your application’s set up program.
      You can uninstall an assembly and its dependencies (if no other assemblies are dependent on them) from the native image cache by running the following command.
      ngen.exe uninstall
      Native images created using Ngen.exe cannot be deployed; instead they need to be created on the end user’s machine. These commands therefore need to be issued as part of the application’s setup program. Visual Studio .NET can be used to implement this behavior by defining custom actions in a Microsoft Installer (MSI) package.
    2. Can we force garbage collector to run ?
      System.GC.Collect()forces garbage collector to run. This is not recommended but can be used if situations arises.
    3. What is reflection?
      All .NET assemblies have metadata information stored about the types defined in modules. This metadata information can be accessed by mechanism called as “Reflection”. Reflection can be used to browse through the metadata information.
      Using reflection you can also dynamically invoke methods usingSystem.Type.Invokemember.
    4. What are different types of JIT?
      JIT compiler is a part of the runtime execution environment. In Microsoft .NET there are three types of JIT compilers:
      • Pre -JIT: Pre-JIT compiles complete source code into native code in a single compilation cycle. This is done at the time of deployment of the application.
      • Econo -JIT : Econo-JIT compiles only those methods that are called at runtime. However, these compiled methods are removed when they are not required.
      • Normal -JIT : Normal-JIT compiles only those methods that are called at runtime. These methods are compiled the first time they are called, and then they are stored in cache. When the same methods are called again, the compiled code from cache is used for execution.
    5. 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 types, pointer types, 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 derive from the System.Object base type.
    6. What is boxing and unboxing?
      Boxing permits any value type to be implicitly converted to type object or to any interface type implemented by value type. Boxing is a process in which object instances are created and copy values in to that instance.
      Unboxing is vice versa of boxing operation where the value is copied from the instance in to appropriate storage location.

      Dim x As Integer
      Dim y As Object
      x = 10
      ‘ boxing process
      y = x
      ‘ unboxing process
      x = y
    7. What are main differences between VB.NET and C#?
      Its a subjective matter which language is best. Some like VB.NET’s natural style and some like professional and terse C# syntaxes. Both use the same framework and speed is also very much equivalents. But still let’s list down some major differences between them :
      Advantages of VB.NET:
      • Has support for optional parameters which makes COM interoperability much easy.
      • With Option Strict off late binding is supported.Legacy VB functionalities can be used by using Microsoft.VisualBasic namespace.
      • Has the WITH construct which is not in C#.
      • The VB.NET part of Visual Studio .NET compiles code in the background. While this is considered an advantage for small projects, people creating very large projects have found that the IDE slows down considerably as the project gets larger.
      Advantages of C#:
      • XML documentation is generated from source code but this is now been incorporated in VS 2005 for both languages.
      • Operator overloading which is not in current VB.NET but is been introduced in VS 2005.
      • Use of this statement makes unmanaged resource disposal simple.
      • Access to Unsafe code. This allows pointer arithmetic etc, and can improve performance in some situations. However, it is not to be used lightly, as a lot of the normal safety of C# is lost (as the name implies).This is the major difference that you can access unmanaged code in C# and not in VB.NET.
    8. What is the difference between System exceptions and Application exceptions?
      All exception derives from Exception Base class. Exceptions can be generated programmatically or can be generated by system. Application Exception serves as the base class for all application-specific exception classes. It derives from Exception but does not provide any extended functionality. You should derive your custom application exceptions from Application Exception.
    9. What is code access security?
      Code access security (CAS) is part of .NET security model that determines whether or not a piece of code is allowed to run and what resources it can use while running. Example CAS will allow an application to read but not to write and delete a file or a resource from a folder.
    10. What is a satellite assembly?
      Satellite assemblies are assemblies which do not contain source code. They only contain resource files. You can create a satellite assembly using utilities like rsgen.exe (Resource Generator) and al.exe (Assmbly Linker). They are in binary DLL format which makes it easier to ship it during deployment. So finally during deployment you do not need to ship the resx files but only the compiled satellite DLL.
      Once you made your resx file or text file you should first convert in to a “.resource” files. This is done by using the resgen.exe. Below is the command snippet for resgen.exe where LoginScreen.aspx.el.resx is the resx file and output is Greek.resources file. If you do not provide the output file name it will generate “LoginScreen.resources”.
      resgen LoginScreen.aspx.el.resx Greek.resources
      Now once the resource file is generated its time make the compiled assembly of the same so that it can be shipped during deployment. This is accomplished by using the assembly linker tool al.exe provided by Microsoft.
      al.exe /out:el.dll /c:ge /embed:greek.resources
      In the /out switch you need to provide the output DLL name. /c you need to specify the culture of the resource file. /embed you need to specify all the resources which are present in the resource file.
    11. I)What is ResourceManager class?
      ResourceManager class helps us to read the resource files and get the values using key. First you need to create the object of resource manager. You need to specify the resource name and the assembly in the constructor.
      private ResourceManager objResourceManager = new ResourceManager ("Globalization.resource",System.Reflection.Assembly.GetExecutingAssembly());
      Once the resource manager is populated with details you can then use the GetString function to get by key. For instance in the below code snippet we are using the “cmdAddNew” key to get the value for button “cmdAddNew””.
      cmdAddNew.Text = objResourceManager.GetString("cmdAddNew");
    12. A)How do we deploy satellite assemblies?
      When we deploy the assembly, the folder structure has to very organized. MainFolder is the main application folder. All satellite assemblies should be deployed in the Main application folder with in there own respective folder. The respective folder is denoted by the culture code (/en-ca, /hi, /el etc.).
      If the program does not find resource file for a culture it uses the invariant culture satellite assembly. The above folder structure is a strict requirement when we deploy the satellite assembly. Any mismatch in the folder structure will lead to in appropriate results.
    13. How to prevent my .NET DLL to be decompiled?
      By design .NET embeds rich Meta data inside the executable code using MSIL. Anyone can easily decompile your DLL back using tools like ILDASM (owned by Microsoft) or third-party tools like Reflector for .NET. So any one can easily look in to your assemblies and reverse engineer them back in to actual source code and understand some real good logic which can make it easy to crack your application.
      The process by which you can stop this reverse engineering is using “obfuscation”. It’s a technique which will foil the decompilers. There are many third parties (XenoCode, Demeanor for .NET) which provide .NET obfuscation solution. Microsoft includes one that is Dotfuscator Community Edition with Visual Studio.NET.
    14. What is the difference between Convert.ToString and .ToString() method ?
      We can convert, say, the integer “i” using “i.ToString()” or “Convert.ToString” so what’s the difference. The basic difference between them is “Convert” function handles NULLS while “i.ToString()” does not it will throw a NULL reference exception error. So as good coding practice using “convert” is always safe.
    15. What is Native Image Generator (Ngen.exe)?
      The Native Image Generator utility (Ngen.exe) allows you to run the JIT compiler on your assembly’s MSIL and generate native machine code which is cached to disk. After the image is created .NET runtime will use the image to run the code rather than from the hard disk. Running Ngen.exe on an assembly potentially allows the assembly to load and execute faster, because it restores code and data structures from the native image cache rather than generating them dynamically.
      Below are some points to be remembered for Native Image Generator:
      • Native images load faster than MSIL because JIT compilation and type-safety verification is eliminated.
      • Native images enable code sharing between processes.
      • If you are sharing code between process Ngen.exe improves the performance significantly. As Native image generated Windows PE file so a single DLL file can be shared across applications. By contrast JIT produced code are private to an assembly and can not be shared.
      • Native images require more storage space and more time to generate.
      • Startup time performance improves lot. We can get considerable gains when applications share component assemblies because after the first application has been started the shared components are already loaded for subsequent applications. If assemblies in an application must be loaded from the hard disk, does not benefit as much from native images because the hard disk access time shadows everything.
      • Assemblies in GAC do not benefit from Native image generator as the loader performs extra validation on the strong named assemblies thus shadowing the benefits of Native Image Generator.
      • If any of the assemblies change then Native image should also be updated.
      • Native images require more storage space and more time to generate.
      • You should have administrative privilege for running Ngen.exe.
      • While this can decrease your application startup times as the code is statically compiled but it can be somewhat slower than the code generated dynamically by the JIT compiler. So you need to compare how the whole application performance with Ngen.exe and with out it.
      To run Ngen.exe, use the following command line.
      ngen.exe install assemblyname
      This will synchronously precompile the specified assembly and all of its dependencies. The generated native images are stored in the native image cache.
      In .NET Framework 2.0 there is a service (.NET Runtime Optimization Service) which can precompile managed assemblies in the background. You can schedule your assemblies to be precompiled asynchronously by queueing them up with the NGEN Service. Use the following command line.
      ngen.exe install assemblyname /queue:priority
    16. If we have two version of same assembly in GAC how do we make a choice ?
      You need to specify “bindingRedirect” in your config file. For instance in the below case “ClassLibraryVersion” has two versions “1.1.1″ and “1.1.2″ from which “1.1.2 is the recent version. But using the bindingRedirect we can specify saying “1.1.1″ is the new version. So the client will not use “1.1.2″.





  • No comments: