Set - 2

Question 1 :

Can you have two files with the same file name in GAC?

Answer :

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

Question 2 :

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.

Question 3 :

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.

Question 4 :

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.

Question 5 :

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.

Question 6 :

Is XML case-sensitive?

Answer :

Yes, so and are different elements.

Question 7 :

If a base class has a bunch of overloaded constructors, and an inherited class has another bunch of overloaded constructors, can you enforce a call from an inherited constructor to an arbitrary base constructor?

Answer :

Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.

Question 8 :

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) { }

Question 9 :

How do I make a DLL in C#?

Answer :

You need to use the /target:library compiler option.

Question 10 :

How do I 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.

Question 11 :

Will finally block get executed if the exception had not occurred?

Answer :


Question 12 :

What is the C# equivalent of C++ catch (…), which was a catch-all statement for any possible exception? Does C# support try-catch-finally blocks?

Answer :

Yes. Try-catch-finally blocks are supported by the C# compiler. 
Here's an example of a try-catch-finally block:

using System; 

public class TryTest {
	static void Main() {
		try {
			Console.WriteLine("In Try block");
			throw new ArgumentException();
		catch(ArgumentException n1) {
			Console.WriteLine("Catch Block");
			Console.WriteLine("Finally Block");

Output: In Try Block
Catch Block
Finally Block

If I return out of a try/finally in C#, does the code in the finally-clause run? Yes. The code in the finally always runs. If you return out of the try block, or even if you do a "goto" out of the try, the finally block always runs, as shown in the following 

using System;
class main {
	public static void Main() {
			Console.WriteLine("In Try block");
		finally {
			Console.WriteLine("In Finally block");

Both "In Try block" and "In Finally block" will be displayed. Whether the return is in the try block or after the try-finally block, performance is not affected either way. The compiler treats it as if the return were outside the try block anyway. If it's a return without an expression (as it is above), the IL emitted is identical whether the return is inside or outside of the try. If the return has an expression, there's an extra store/load of the value of the expression (since it has to be computed within the try block).

Question 13 :

Is there regular expression (regex) support available to C# developers?

Answer :

Yes. The .NET class libraries provide support for regular expressions. Look at the documentation for the System.Text.RegularExpressions namespace.

Question 14 :

Is there a way to force garbage collection?

Answer :

Yes. Set all references to null and then call System.GC.Collect(). If you need to have some objects destructed, and System.GC.Collect() doesn't seem to be doing it for you, you can force finalizers to be run by setting all the references to the object to null and then calling System.GC.RunFinalizers().

Question 15 :

Does C# support properties of array types?

Answer :

Yes. Here's a simple example:

using System;
class Class1{
	private string[] MyField;
	public string[] MyProperty{
		get { return MyField; }
		set { MyField = value; }
class MainClass{
	public static int Main(string[] args){
		Class1 c = new Class1();
		string[] arr = new string[] {"apple", "banana"};
		c.MyProperty = arr;
		Console.WriteLine(c.MyProperty[0]); // "apple"
		return 0;


Question 16 :

Between Windows Authentication and SQL Server Authentication, which one is trusted and which one is untrusted? 

Answer :

Windows Authentication is trusted because the username and password are checked with the Active Directory, the SQL Server authentication is untrusted, since SQL Server is the only verifier participating in the transaction.

Question 17 :

What does the Initial Catalog parameter define in the connection string?

Answer :

The database name to connect to.

Question 18 :

What does the Dispose method do with the connection object?

Answer :

Deletes it from the memory.

Question 19 :

What is a pre-requisite for connection pooling?

Answer :

Multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings. The connection string must be identical.

Question 20 :

How is the DLL Hell problem solved in .NET?

Answer :

Assembly versioning allows the application to specify not only the library it needs to run (which was available under Win32), but also the version of the assembly.

Question 21 :

How do you convert a value-type to a reference-type? 

Answer :

Use Boxing.

Question 22 :

What happens in memory when you Box and Unbox a value-type?

Answer :

Boxing converts a value-type to a reference-type, thus storing the object on the heap. Unboxing converts a reference-type to a value-type, thus storing the value on the stack.Difference between directcast and ctype. 

DirectCast requires the run-time type of an object variable to bethe same as the specified type.The run-time performance ofDirectCast is better than that of CType, if the specified type and the run-time typeof the expression are the same. Ctype works fine if there is a valid conversion defined between the expression and the type. 

The difference between the two keywords is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType. 

An example of a ctype and directcast. 

In the preceding example, the run-time type of Q is Double. CType succeeds because Double can be converted to Integer, but DirectCast fails because the run-time type of Q is not already Integer

Question 23 :

ctype(123.34,integer) - should it throw an error? Why or why not?

Answer :

It would work fine. As the runtime type of 123.34 would be double, and Double can be converted to Integer. 

the ctype(123.34,integer) will work fine no errors

Question 24 :

directcast(123.34,integer) - should it throw an error? Why or why not?

Answer :

It would throw an InvalidCast exception as the runtime type of 123.34 (double) doesnt match with Integer.

Question 25 :

Difference between a sub and a function.

Answer :

A Sub does not return anything whereas a Function returns something. 

-A Sub Procedure is a method will not return a value
-A sub procedure will be defined with a "Sub" keyword

Sub ShowName(ByVal myName As String)
Console.WriteLine("My name is: " & myName)
End Sub

-A function is a method that will return value(s).
-A function will be defined with a "Function" keyword

Function FindSum(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Dim sum As Integer = num1 + num2
Return sum
End Function