Set - 8

Question 1 :

How do you specify a custom attribute for the entire assembly (rather than for a class)?

Answer :

Global attributes must appear after any top-level using clauses and before the first type or namespace declarations. An example of this is as follows:

using System;
[assembly : MyAttributeClass]
class X {}

Note that in an IDE-created project, by convention, these attributes are placed in 
AssemblyInfo.cs.


Question 2 :

What is the difference between a struct and a class in C#?

Answer :

From language spec:
The list of similarities between classes and structs is as follows. Longstructs can implement interfaces and can have the same kinds of members as classes. Structs differ from classes in several important ways; however, structs are value types rather than reference types, and inheritance is not supported for structs. Struct values are stored on the stack or in-line. Careful programmers can sometimes enhance performance through judicious use of structs. For example, the use of a struct rather than a class for a Point can make a large difference in the number of memory allocations performed at runtime. The program below creates and initializes an array of 100 points. With Point implemented as a class, 101 separate objects are instantiated-one for the array and one each for the 100 elements.


Question 3 :

What is the difference between the Debug class and Trace class?

Answer :

Documentation looks the same. Use Debug class for debug builds, use Trace class for both debug and release builds.


Question 4 :

How can you overload a method?

Answer :

Different parameter data types, different number of parameters, different order of parameters.


Question 5 :

What debugging tools come with the .NET SDK?

Answer :

CorDBG - command-line debugger, and DbgCLR - graphic debugger. Visual Studio .NET uses the DbgCLR. To use CorDbg, you must compile the original C# file using the /debug switch.


Question 6 :

What does Dispose method do with the connection object?

Answer :

Deletes it from the memory.


Question 7 :

How do you generate documentation from the C# file commented properly with a command-line compiler?

Answer :

Compile it with a /doc switch.


Question 8 :

When you inherit a protected class-level variable, who is it available to?

Answer :

Classes in the same namespace.


Question 9 :

How can I get the ASCII code for a character in C#?

Answer :

Casting the char to an int will give you the ASCII value: char c = 'f'; System.Console.WriteLine((int)c); or for a character in a string: System.Console.WriteLine((int)s[3]); The base class libraries also offer ways to do this with the Convert class or Encoding classes if you need a particular encoding.


Question 10 :

Is there an equivalent to the instanceof operator in Visual J++?

Answer :

C# has the is operator:
expr is type


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.


Question 16 :

Can multiple catch blocks be executed for a single try statement?

Answer :

No. Once the proper catch block processed, control is transferred to the finally block (if there are any).


Question 17 :

Explain the three services model commonly know as a three-tier application.

Answer :

Presentation (UI), Business (logic and underlying code) and Data (from storage or other sources).


Question 18 :

What is the syntax to inherit from a class in C#?

Answer :

Place a colon and then the name of the base class.
Example: 

class MyNewClass : MyBaseClass

 


Question 19 :

Can you prevent your class from being inherited by another class?

Answer :

Yes. The keyword "sealed" will prevent the class from being inherited.


Question 20 :

Can you allow a class to be inherited, but prevent the method from being over-ridden?

Answer :

Yes. Just leave the class public and make the method sealed.