Set - 8

Question 11 :

How do I create a Delegate/MulticastDelegate?

Answer :

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!


Question 12 :

How do destructors and garbage collection work in C#?

Answer :

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.


Question 13 :

My switch statement works differently! Why?

Answer :

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;
        }
	}
}

 


Question 14 :

How can I access the registry from C# code?

Answer :

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

 


Question 15 :

How can you sort the elements of the array in descending order?

Answer :

By calling Sort() and then Reverse() methods.