Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Wednesday, July 16, 2008

C# Interview Questions and Answers 9

What is the wildcard character in SQL?
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%.

What is the role of the DataReader class in ADO.NET connections?
It returns a read-only dataset from the data source when the command is executed.

What does the This window show in the debugger?
It points to the object that is pointed to by this reference. Object’s instance data is shown.

Describe the accessibility modifier protected internal?
It is available to derived classes and classes within the same Assembly (and naturally from the base class it is declared in).

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

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

How does one compare strings in C#?
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

What does assert() do?
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.

How do I get deterministic finalization in C#?
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.

How can I get around scope problems in a try/catch?
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').

Why do I get an error (CS1006) when trying to declare a method without specifying a return type?
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)

No comments:

Archives