Set - 7

Question 1 :

Why does my Windows application pop up a console window every time I run it?

Answer :

Make sure that the target type set in the project properties setting is set to Windows Application, and not Console Application. If you're using the command line, compile with /target:winexe & not target:exe.


Question 2 :

What is the wildcard character in SQL?

Answer :

Let us say you want to query database with LIKE for all employees whose name starts with La. The wildcard character is %, the proper query with LIKE would involve La%.


Question 3 :

What is the role of the DataReader class in ADO.NET connections?

Answer :

It returns a read-only dataset from the data source when the command is executed.


Question 4 :

What does the This window show in the debugger?

Answer :

It points to the object that is pointed to by this reference. Object's instance data is shown.


Question 5 :

Describe the accessibility modifier protected internal?

Answer :

It is available to derived classes and classes within the same Assembly (and naturally from the base class it is declared in).


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.


Question 11 :

How can I get around scope problems in a try/catch?

Answer :

If you try to instantiate the class inside the try, it'll be out of scope when you try to access it from the catch block. A way to get around this is to do the following:

Connection conn = null;
try{
	conn = new Connection();
	conn.Open();
}
finally{
	if (conn != null) conn.Close();
}

By setting it to null before the try block, you avoid getting the CS0165 error (Use of possibly unassigned local variable 'conn').


Question 12 :

Why do I get an error (CS1006) when trying to declare a method without specifying a return type? 

Answer :

If you leave off the return type on a method declaration, the compiler thinks you are trying to declare a constructor. So if you are trying to declare a method that returns nothing, use void. The following is an example: // This results in a CS1006 error public static staticMethod (mainStatic obj) // This will work as wanted public static void staticMethod (mainStatic obj)


Question 13 :

How do I convert a string to an int in C#?

Answer :

Here's an example: 

using System;
class StringToInt{
	public static void Main(){
		String s = "105";
		int x = Convert.ToInt32(s);
		Console.WriteLine(x);
	}
}

 


Question 14 :

How do you directly call a native function exported from a DLL?

Answer :

Here's a quick example of the DllImport attribute in action:

using System.Runtime.InteropServices;
class C{
	[DllImport("user32.dll")]
	public static extern int MessageBoxA(int h, string m, string c, int type);
	public static int Main(){
		return MessageBoxA(0, "Hello World!", "Caption", 0);
	}
}

This example shows the minimum requirements for declaring a C# method that is implemented in a native DLL. The method C.MessageBoxA() is declared with the static and external modifiers, and has the DllImport attribute, which tells the compiler that the implementation comes from the user32.dll, using the default name of MessageBoxA. For more information, look at the Platform Invoke tutorial in the documentation.


Question 15 :

What is the .NET datatype that allows the retrieval of data by a unique key?

Answer :

HashTable.


Question 16 :

What's an abstract class? 

Answer :

A class that cannot be instantiated. An abstract class is a class that must be inherited and have the methods overridden. An abstract class is essentially a blueprint for a class without any implementation.


Question 17 :

When do you absolutely have to declare a class as abstract?

Answer :

1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
2. When at least one of the methods in the class is abstract.


Question 18 :

What is an interface class?

Answer :

Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes.


Question 19 :

Why can't you specify the accessibility modifier for methods inside the interface?

Answer :

They all must be public, and are therefore public by default.


Question 20 :

What happens if you inherit multiple interfaces and they have conflicting method names?

Answer :

It's up to you to implement the method inside your own class, so implementation is left entirely up to you. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data, but as far as compiler cares you're okay. 
To Do: Investigate


Question 21 :

Can you store multiple data types in System.Array?

Answer :

No.


Question 22 :

What's the difference between the System.Array.CopyTo() and System.Array.Clone()?

Answer :

The Clone() method returns a new array (a shallow copy) object containing all the elements in the original array. The CopyTo() method copies the elements into another existing array. Both perform a shallow copy. A shallow copy means the contents (each array element) contains references to the same object as the elements in the original array. A deep copy (which neither of these methods performs) would create a new instance of each element's object, resulting in a different, yet identical object.


Question 23 :

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

Answer :

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