C#.NET Questions and Answers on Assemblies, DLL

6/21/2009 No Comment

C#.NET Assemblies and DLL Questions asked in top companies.

Describe with an example how do you directly call a native function exported from a DLL in C#?
Answer : Let us look at a quick example of the DllImport attribute in action.
using System.Runtime.InteropServices;
class C
public static extern int MessageBoxA(int h, string m, string c, int type);
public static int Main()
{return MessageBoxA(0, "Hello World!", "Caption", 0);}
The above example shows what is the minimum requirement for declaring a C# method that is implemented in a native DLL. 

The method in the example C.MessageBoxA() is declared with the static and external modifiers, and it has the DllImport attribute, which will tell the compiler that the implementation is coming from the user32.dll, using the default name of MessageBoxA. 

Differentiate between private and shared assembly in C#.NET? 
Answer : 
A private assembly is normally used inside an application, and is stored in the application's directory, or a sub-directory beneath. Private assembly 
does not have to be identified by a strong name

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 and has to have a strong name.

  • In private assembly all the referred DLL's will be copied into global project debug folder but in shared assembly will not. 
  • Private assemblies are faster in accessing,when compare with shared assemblies.
  • The private assembly is used when only one project need to be developed for a client but shared assembly is used when more then one project Need to be developed.
  • Memory wise shared assembly are recommended. 
What do you mean by a strong name in C#? 
Answer : A strong name includes the name of the assembly, version number, culture identity, and a public key token.

Describe in brief who you will simulate optional parameters to COM calls?
Answer : You must use the Missing class and pass Missing.Value (in System.Reflection) for any values that have optional parameters.

What are .NET assemblies. Can you please explain?

Answer : Assemblies are the smallest units of versioning and deployment in the .NET application. Assemblies are also the building blocks for programs such as Web services, Windows services, serviced components, and .NET remote applications.

Describe the way how you can tell the .NET application to look for assemblies at the locations other than its own install path?

Answer : Use the directive in the XML .config file for a given application.
< privatepath="c:\mylibs;">
should do the trick. Or you can add additional search paths in the Properties box of the deployed application.

In brief describe how can you debug failed assembly binds?
Answer : Use the Assembly Binding Log Viewer (fuslogvw.exe) to find out the paths searched.

Where are the shared assemblies in .NET stored?
Answer : Global assembly cache.

Can you create a strong name for a .NET assembly in C#?
Answer : Yes, we can create with the help of Strong Name tool (sn.exe).

Where’s global assembly cache located on the system?
Answer : Usually C:\winnt\assembly or C:\windows\assembly.
Can you have two files with the same file name in GAC?
Yes, remember that GAC is a very special folder, and while normally you would not be able to place two files with the same name into a Windows folder, GAC differentiates by version number as well, so it’s possible for MyApp.dll and MyApp.dll to co-exist in GAC if the first one is version and the second one is
So let’s say I have an application that uses MyApp.dll assembly, version There is a security bug in that assembly, and I publish the patch, issuing it under name MyApp.dll

How do I tell the client applications that are already installed to start using this new MyApp.dll?
Answer : Use publisher policy. To configure a publisher policy, use the publisher policy configuration file, which uses a format similar app .config file. But unlike the app .config file, a publisher policy file needs to be compiled into an assembly and placed in the GAC.

Is there an equivalent of exit() for quitting a C# .NET application? 
Answer : Yes, you can use System.Environment.Exit(int exitCode) to exit the application or Application.Exit() if it's a Windows Forms app.

What is delay signing?
Answer : Delay signing allows you to place a shared assembly in the GAC by signing the assembly with just the public key. This allows the assembly to be signed with the private key at a later stage, when the development process is complete and the component or assembly is ready to be deployed. This process enables developers to work with shared assemblies as if they were strongly named, and it secures the private key of the signature from being accessed at different stages of development.

Can you prevent your class from being inherited and becoming a base class for some other classes?

Answer : Yes, that is what keyword sealed in the class definition is for. The developer trying to derive from your class will get a message: cannot inherit from Sealed class WhateverBaseClassName. It is the same concept as final class in Java.

How do I make a DLL in C#? 
Answer : You need to use the /target:library compiler option.

I was trying to use an "out int" parameter in one of my functions. How should I declare the variable that I am passing to it? 
Answer : You should declare the variable as an int, but when you pass it in you must specify it as 'out', like the following:
int i;
foo(out i);
where foo is declared as follows:
[return-type] foo(out int o) { } 

Can you tell us who to simulate optional parameters to COM calls in C#.NET? 
Answer : You must use the Missing class and pass Missing.Value (in System.Reflection) for any values that have optional parameters.

Is XML a case-sensitive language?

Answer : Yes, so and are different elements.

No comments :


Aired | The content is copyrighted and may not be reproduced on other websites. | Copyright © 2009-2015 | All Rights Reserved 2015

Contact Us | About Us | Privacy Policy and Disclaimer