Set - 7

Question 6 :

What is an interface class?

Answer :

It is an abstract class with public abstract methods all of which must be implemented in the inherited classes.


Question 7 :

What is a multicast delegate?

Answer :

It is a delegate that points to and eventually fires off several methods.


Question 8 :

How does one compare strings in C#?

Answer :

In the past, you had to call .ToString() on the strings when using the == or != operators to compare the strings' values. That will still work, but the C# compiler now automatically compares the values instead of the references when the == or != operators are used on string types. If you actually do want to compare references, it can be done as follows: if ((object) str1 == (object) str2) { ... } Here's an example showing how string compares work:

using System; 
public class StringTest{
	public static void Main(string[] args){
		Object nullObj = null;
		Object realObj = new StringTest();
		int i = 10;
		Console.WriteLine("Null Object is [" + nullObj + "]n" + "Real Object is [" + realObj + "]n" + "i is [" + i + "]n");
		// Show string equality operators
		string str1 = "foo";
		string str2 = "bar";
		string str3 = "bar";
		Console.WriteLine("{0} == {1} ? {2}", str1, str2, str1 == str2 );
		Console.WriteLine("{0} == {1} ? {2}", str2, str3, str2 == str3 );
	}
}

Output: Null Object is []
Real Object is [StringTest]
i is [10]
foo == bar ? False
bar == bar ? True


Question 9 :

What does assert() do?

Answer :

In debug compilation, assert takes in a Boolean condition as a parameter, and shows the error dialog if the condition is false. The program proceeds without any interruption if the condition is true.


Question 10 :

How do I get deterministic finalization in C#?

Answer :

In a garbage collected environment, it's impossible to get true determinism. However, a design pattern that we recommend is implementing IDisposable on any class that contains a critical resource. Whenever this class is consumed, it may be placed in a using statement, as shown in the following example:

using(FileStream myFile = File.Open(@"c:temptest.txt",
FileMode.Open)){
	int fileOffset = 0;
	while(fileOffset < myFile.Length){
		Console.Write((char)myFile.ReadByte());
		fileOffset++;
	}
}

When myFile leaves the lexical scope of the using, its dispose method will be called.