Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Wednesday, July 16, 2008

C# Interview Questions and Answers 11

How do I create a Delegate/MulticastDelegate?
C# requires only a single parameter for delegates: the method address. Unlike other languages, where the programmer must specify an object reference and the method to invoke, C# can infer both pieces of information by just specifying the method's name. For example, let's use System.Threading.ThreadStart: Foo MyFoo = new Foo(); ThreadStart del = new ThreadStart(MyFoo.Baz); This means that delegates can invoke static class methods and instance methods with the exact same syntax!

How do destructors and garbage collection work in C#?
C# has finalizers (similar to destructors except that the runtime doesn't guarantee they'll be called), and they are specified as follows:
class C
{
~C()
{
// your code
}
public static void Main() {}
}
Currently, they override object.Finalize(), which is called during the GC process.

My switch statement works differently! Why?
C# does not support an explicit fall through for case blocks.
The following code is not legal and will not compile in C#: switch(x)
{
case 0:
// do something
case 1:
// do something in common with 0
default:
// do something in common with
//0, 1 and everything else
break;
}
To achieve the same effect in C#, the code must be modified
as shown below (notice how the control flows are explicit): class Test
{
public static void Main()
{
int x = 3;
switch(x)
{
case 0:
// do something
goto case 1;
case 1:
// do something in common with 0
goto default;
default:
// do something in common with 0, 1, and anything else
break;
}
}
}

How can I access the registry from C# code?
By using the Registry and RegistryKey classes in Microsoft.Win32, you can easily access the registry. The following is a sample that reads a key and displays its value:
using System;using Microsoft.Win32;
class regTest
{
public static void Main(String[] args)
{
RegistryKey regKey;
Object value;
regKey = Registry.LocalMachine;
regKey =
regKey.OpenSubKey("HARDWAREDESCRIPTIONSystemCentralProcessor ");
value = regKey.GetValue("VendorIdentifier");
Console.WriteLine("The central processor of this machine is: {0}.", value);
}
}

How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.

How do you debug an ASP.NET Web application?
Attach the aspnet_wp.exe process to the DbgClr debugger.

How do you mark a method obsolete?
Assuming you've done a "using System;": [Obsolete]
public int Foo() {...}
or [Obsolete("This is a message describing why this method is obsolete")]
public int Foo() {...}
Note: The O in Obsolete is capitalized.

How is the DLL Hell problem solved in .NET?
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

What are the ways to deploy an assembly?
An MSI installer, a CAB archive, and XCOPY command.

No comments:

Archives