Thursday, February 10, 2011

DbUtils.cs

     //sql connections
        /// Returns the results of a SQL Query in the form of a DataTable
        public static DataTable SQLSelect(SqlCommand cmdSQLQuery)
        {
            //Get connection string
            //string conConnectionString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            string conConnectionString=null;
            SqlConnection SQLDatabaseConnection = new SqlConnection(conConnectionString);
            //Perform Command
            cmdSQLQuery.Connection = SQLDatabaseConnection;
            DataSet dsPageInfo = new DataSet();
            SqlDataAdapter daPageInfo = new SqlDataAdapter(cmdSQLQuery);
            SQLDatabaseConnection.Open();
            daPageInfo.Fill(dsPageInfo);
            SQLDatabaseConnection.Close();
            return dsPageInfo.Tables[0];
        }
        /// Executes a SQL Command
        public static void ExecuteSQLCommand(SqlCommand CommandToExecute)
        {
            //get connection sring
           
            SqlConnection SQLDatabaseConnection = new SqlConnection(conConnectionString);
            //execute command
            CommandToExecute.Connection = SQLDatabaseConnection;
            SQLDatabaseConnection.Open();
            CommandToExecute.ExecuteNonQuery();
            SQLDatabaseConnection.Close();
        }


        //ole db connection
        /// Returns the results of a SQL Query in the form of a DataTable
        public static DataTable SQLSelect(OleDbCommand cmdOleDbCommand)
        {
            OleDbConnection OleDbDatabaseConnection = new OleDbConnection(conConnectionString);
            //Perform Command
            cmdOleDbCommand.Connection = OleDbDatabaseConnection;
            DataSet dsPageInfo = new DataSet();
            OleDbDataAdapter daPageInfo = new OleDbDataAdapter(cmdOleDbCommand);
            OleDbDatabaseConnection.Open();
            daPageInfo.Fill(dsPageInfo);
            OleDbDatabaseConnection.Close();
            return dsPageInfo.Tables[0];
        }

     public static void ExecuteSQLCommand(OleDbCommand CommandToExecute)
        {

          
            OleDbConnection OleDbDatabaseConnection = new OleDbConnection(conConnectionString);
            //execute command
            CommandToExecute.Connection = OleDbDatabaseConnection;
            OleDbDatabaseConnection.Open();
            CommandToExecute.ExecuteNonQuery();
            OleDbDatabaseConnection.Close();
        }

Enumerations

Enums are used to create and group together a list of user defined named constants. Although on the surface the enumerated types in C# and Java seem quite similar there are some significant differences in the implementation of enumerated types in both languages. In Java, enumerated types are a full fledged class which means they are typesafe and can be extended by adding methods, fields or even implementing interfaces. Whereas in C#, an enumerated type is simply syntactic sugar around an integral type (typically an int) meaning they cannot be extended and are not typesafe.
The following code sample highlights the differences between enums in both languages.
C# Code
using System;

public enum DaysOfWeek{
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}

public class Test{

public static bool isWeekDay(DaysOfWeek day){
return !isWeekEnd(day);
}

public static bool isWeekEnd(DaysOfWeek day){
return (day == DaysOfWeek.SUNDAY || day == DaysOfWeek.SATURDAY);
}

public static void Main(String[] args){

DaysOfWeek sun = DaysOfWeek.SUNDAY;
Console.WriteLine("Is " + sun + " a weekend? " + isWeekEnd(sun));
Console.WriteLine("Is " + sun + " a week day? " + isWeekDay(sun));

/* Example of how C# enums are not type safe */
sun = (DaysOfWeek) 1999;
Console.WriteLine(sun);

}
}

Java Code
enum DaysOfWeek{
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;

public boolean isWeekDay(){
return !isWeekEnd();
}

public boolean isWeekEnd(){
return (this == SUNDAY || this == SATURDAY);
}

}

public class Test{

public static void main(String[] args) throws Exception{

DaysOfWeek sun = DaysOfWeek.SUNDAY;
System.out.println("Is " + sun + " a weekend? " + sun.isWeekEnd());
System.out.println("Is " + sun + " a week day? " + sun.isWeekDay());
}
}

Generics

Both C# and Java provide a mechanism for creating strongly typed data structures without knowing the specific types at compile time. Prior to the existence of the Generics feature set, this capability was achieved by specifying the type of the objects within the data structure as Object then casting to specific types at runtime. This technique had several drawbacks including lack of type safety, poor performance and code bloat.
The following code sample shows how one would calculate the sum of all the integers in a collection using generics and using a collection of Objects so that both approaches can be compared.

C# Code
using System;
using System.Collections;
using System.Collections.Generic;

class Test{

public static Stack GetStackB4Generics(){
Stack s = new Stack();
s.Push(2);
s.Push(4);
s.Push(5);

return s;
}

public static Stack<int> GetStackAfterGenerics(){
Stack<int> s = new Stack<int>();
s.Push(12);
s.Push(14);
s.Push(50);

return s;
}

public static void Main(String[] args){

Stack s1 = GetStackB4Generics();
int sum1 = 0;

while(s1.Count != 0){
sum1 += (int) s1.Pop(); //cast
}

Console.WriteLine("Sum of stack 1 is " + sum1);

Stack<int> s2 = GetStackAfterGenerics();
int sum2 = 0;

while(s2.Count != 0){
sum2 += s2.Pop(); //no cast
}

Console.WriteLine("Sum of stack 2 is " + sum2);
}

}
Java Code
import java.util.*;

class Test{

public static Stack GetStackB4Generics(){
Stack s = new Stack();
s.push(2);
s.push(4);
s.push(5);

return s;
}

public static Stack<Integer> GetStackAfterGenerics(){
Stack<Integer> s = new Stack<Integer>();
s.push(12);
s.push(14);
s.push(50);

return s;
}

public static void main(String[] args){

Stack s1 = GetStackB4Generics();
int sum1 = 0;

while(!s1.empty()){
sum1 += (Integer) s1.pop(); //cast
}

System.out.println("Sum of stack 1 is " + sum1);

Stack<Integer> s2 = GetStackAfterGenerics();
int sum2 = 0;

while(!s2.empty()){
sum2 += s2.pop(); //no cast
}

System.out.println("Sum of stack 2 is " + sum2);
}

}
Although similar in concept to templates in C++, the Generics feature in C# and Java is not implemented similarly. In Java, the generic functionality is implemented using type erasure. Specifically the generic type information is present only at compile time, after which it is erased by the compiler and all the type declarations are replaced with Object. The compiler then automatically inserts casts in the right places. The reason for this approach is that it provides total interoperability between generic code and legacy code that doesn't support generics. The main problem with type erasure is that the generic type information is not available at run time via reflection or run time type identification. Another consequence of this approach is that generic data structures types must always be declared using objects and not primitive types. Thus one must create Stack<Integer> instead of Stack<int> when working integers.
In C#, there is explicit support for generics in the .NET runtime's instruction language (IL). When the generic type is compiled, the generated IL contains place holders for specific types. At runtime, when an initial reference is made to a generic type (e.g. List<int>) the system looks to see if anyone already asked for the type or not. If the type has been previously requested, then the previously generated specific type is returned. If not, the JIT compiler instantiates a new type by replacing the generic type parameters in the IL with the specific type (e.g. replacing List<T> with List<int>). It should be noted that if the requested type is a reference type as opposed to a value type then the generic type parameter is replaced with Object. However there is no casting done internally by the .NET runtime when accessing the type.
In certain cases, one may need create a method that can operate on data structures containing any type as opposed to those that contain a specific type (e.g. a method to print all the objects in a data structure) while still taking advantage of the benefits of strong typing in generics. The mechanism for specifying this in C# is via a feature called generic type inferencing while in Java this is done using wildcard types. The following code samples show how both approaches lead to the same result.
C# Code
using System;
using System.Collections;
using System.Collections.Generic;

class Test{

//Prints the contents of any generic Stack by
//using generic type inference
public static void PrintStackContents<T>(Stack<T> s){
while(s.Count != 0){
Console.WriteLine(s.Pop());
}
}

public static void Main(String[] args){

Stack<int> s2 = new Stack<int>();
s2.Push(4);
s2.Push(5);
s2.Push(6);

PrintStackContents(s2);

Stack<string> s1 = new Stack<string>();
s1.Push("One");
s1.Push("Two");
s1.Push("Three");

PrintStackContents(s1);
}

}
Java Code
import java.util.*;

class Test{

//Prints the contents of any generic Stack by
//specifying wildcard type
public static void PrintStackContents(Stack<?> s){
while(!s.empty()){
System.out.println(s.pop());
}
}

public static void main(String[] args){

Stack <Integer> s2 = new Stack <Integer>();
s2.push(4);
s2.push(5);
s2.push(6);

PrintStackContents(s2);

Stack<String> s1 = new Stack<String>();
s1.push("One");
s1.push("Two");
s1.push("Three");

PrintStackContents(s1);
}

}
Both C# and Java provide mechanisms for specifying constraints on generic types. In C# there are three types of constraints that can be applied to generic types
  1. A derivation constraint indicates to the compiler that the generic type parameter derives from a base type such an interface or a particular base class
  2. A default constructor constraint indicates to the compiler that the generic type parameter exposes a public default constructor
  3. A reference/value type constraint constrains the generic type parameter to be a reference or a value type.
In Java, only the derivation constraint is supported. The following code sample shows how constraints are used in practice.
C# Code
using System;
using System.Collections;
using System.Collections.Generic;

public class Mammal {
public Mammal(){;}

public virtual void Speak(){;}
}

public class Cat : Mammal{
public Cat(){;}

public override void Speak(){
Console.WriteLine("Meow");
}
}

public class Dog : Mammal{
public Dog(){;}

public override void Speak(){
Console.WriteLine("Woof");
}
}


public class MammalHelper<T> where T: Mammal /* derivation constraint */,
new() /* default constructor constraint */{

public static T CreatePet(){
return new T();
}


public static void AnnoyNeighbors(Stack<T> pets){
while(pets.Count != 0){
Mammal m = pets.Pop();
m.Speak();
}
}
}

public class Test{

public static void Main(String[] args){

Stack<Mammal> s2 = new Stack<Mammal>();
s2.Push(MammalHelper<Dog>.CreatePet());
s2.Push(MammalHelper<Cat>.CreatePet());

MammalHelper<Mammal>.AnnoyNeighbors(s2);
}
}

Java Code
import java.util.*;

abstract class Mammal {
public abstract void speak();
}

class Cat extends Mammal{
public void speak(){
System.out.println("Meow");
}
}

class Dog extends Mammal{
public void speak(){
System.out.println("Woof");
}
}

public class Test{

//derivation constraint applied to pets parameter
public static void AnnoyNeighbors(Stack<? extends Mammal> pets){
while(!pets.empty()){
Mammal m = pets.pop();
m.speak();
}
}

public static void main(String[] args){

Stack<Mammal> s2 = new Stack<Mammal>();
s2.push(new Dog());
s2.push(new Cat());

AnnoyNeighbors(s2);
}
}
C# also includes the default operator which returns the default value for a type. The default value for reference types is null, and the default value for value types (such as integers, enum, and structures) is a zero whitewash (filling the structure with zeros). This operator is very useful when combined with generics. The following code sample excercises the functionality of this operator.
C# Code
using System;

public class Test{

public static T GetDefaultForType(){
return default(T); //return default value of type T
}

public static void Main(String[] args){

Console.WriteLine(GetDefaultForType<int>());
Console.WriteLine(GetDefaultForType<string>());
Console.WriteLine(GetDefaultForType<float>());

}
}

for-each Loop

The for-each loop is an iteration construct that is popular in a number of scripting languages (e.g. Perl, PHP, Tcl/Tk), build tools (GNU Make) and function libraries (e.g. for_each in <algorithm> in C++). The for-each loop is a less verbose way to iterate through arrays or classes that implement the the System.Collections.IEnumerable interface in C# or the java.lang.Iterable interface in Java.
In C#, the keywords foreach and in are used when creating the for-each loop while in Java the keyword for and the operator : are used.
C# Code
string[] greek_alphabet = {"alpha", "beta", "gamma", "delta", "epsilon"};

foreach(string str in greek_alphabet)
Console.WriteLine(str + " is a letter of the greek alphabet");

Java Code
String[] greek_alphabet = {"alpha", "beta", "gamma", "delta", "epsilon"};

for(String str : greek_alphabet)
System.out.println(str + " is a letter of the greek alphabet");

Metadata Annotations

Metadata annotations provide a powerful way to extend the capabilities of a programming language and the language runtime. These annotations can be directives that request the runtime to perform certain additional tasks, provide extra information about an item or extend the abilities of a type. Metadata annotations are common in a number of programming environments including Microsoft's COM and the Linux kernel.
C# attributes provide a way to add annotations (i.e. metadata) to a module, type, method, parameter or member variable. Below are descriptions of a few attributes that are intrinsic to .NET and how they are used to extend the capabilities of the C#.
  1. [MethodImpl(MethodImplOptions.Synchronized)]: is used to specify that a access to a method by multiple threads is protected by a lock to prevent concurrent access to the method and is similar to the synchronized in Java.

  2. [Serializable]: is used to mark a class as serializable and is similar to a Java class implementing the Serializable interface.

  3. [FlagsAttribute]: is used to specify that an enum should support bitwise operations. This is particularly important for enumerations where the target can have multiple values.
    C# Code
    //declaration of bit field enumeration
    [Flags]
    enum ProgrammingLanguages{
    C = 1,
    Lisp = 2,
    Basic = 4,
    All = C | Lisp | Basic
    }

    aProgrammer.KnownLanguages = ProgrammingLanguages.Lisp; //set known languages ="Lisp"
    aProgrammer.KnownLanguages |= ProgrammingLanguages.C; //set known languages ="Lisp C"
    aProgrammer.KnownLanguages &= ~ProgrammingLanguages.Lisp; //set known languages ="C"

    if((aProgrammer.KnownLanguages & ProgrammingLanguages.C) > 0){ //if programmer knows C
    //.. do something
    }
  4. [WebMethod]: is used in combination with ASP.NET to specify that a method should be available over the web as a web service automatically. Doing the same in Java involves configuring JAXP, UDDI, and J2EE as well as have to create an Enterprise Java Bean which involves at least two interfaces and one implementation class plus setting up the deployment descriptor. For more information on webservices in C#, examine the Your First C# Web Service page on CodeProject.
It is possible to access the attributes of a module, class, method or field via reflection. This is particularly useful for seeing if a class supports certain behavior at runtime or for extracting metadata about a class for usage by others. Developers can create their own custom attributes by subclassing the System.Attribute class. What follows is an example of using an attribute to provide information about the author of a class then using reflection to access that information.
C# Code
using System;
using System.Reflection;
[AttributeUsage(AttributeTargets.Class)]
public class AuthorInfoAttribute: System.Attribute{

string author;
string email;
string version;


public AuthorInfoAttribute(string author, string email){

this.author = author;
this.email = email;

}


public string Version{


get{
return version;
}

set{
version = value;
}

}


public string Email{


get{
return email;
}

}

public string Author{


get{
return author;
}

}

}



[AuthorInfo("Dare Obasanjo", "kpako@yahoo.com", Version="1.0")]
class HelloWorld{

}



class AttributeTest{

public static void Main(string[] args){

/* Get Type object of HelloWorld class */
Type t = typeof(HelloWorld);

Console.WriteLine("Author Information for " + t);
Console.WriteLine("=================================");

foreach(AuthorInfoAttribute att in t.GetCustomAttributes(typeof(AuthorInfoAttribute), false)){

Console.WriteLine("Author: " + att.Author);
Console.WriteLine("Email: " + att.Email);
Console.WriteLine("Version: " + att.Version);

}//foreach

}//Main

}
Java annotations provide a way to add annotations (i.e. metadata) to an package, type, method, parameter, member or local variable. There are only three built-in annotations provided in the Java language which are listed below.
  1. @Override: is used to specify that a method is intended to override a method in a base class. If the annotated method does not override a method in the base class then an error is issued during compilation.

  2. @Deprecated: is used to indicate that a particular method has been deprecated. If the annotated method is used then a warning is issued during compilation.

  3. @SuppressWarnings: is used to prevent particular warnings from being issued by the compiler. This annotation optionally takes the name of the specific warning to suppress as an argument.
As in C# it is possible to access the annotations on a module, class, method or field via reflection. However a key difference between C# attributes and Java annotations is that one can create meta-annotations (i.e. annotations on annotations) in Java but can not do the same in C#. Developers can create their own custom annotations by creating an annotation type which is similar to an interface except that the keyword @interface is used to define it. What follows is an example of using an attribute to provide information about the author of a class then using reflection to access that information.
Java Code
import java.lang.annotation.*;
import java.lang.reflect.*;

@Documented //we want the annotation to show up in the Javadocs
@Retention(RetentionPolicy.RUNTIME) //we want annotation metadata to be exposed at runtime
@interface AuthorInfo{
String author();
String email();
String version() default "1.0";
}

@AuthorInfo(author="Dare Obasanjo", email="kpako@yahoo.com")
class HelloWorld{

}

public class Test{

public static void main(String[] args) throws Exception{

/* Get Class object of HelloWorld class */
Class c = Class.forName("HelloWorld");
AuthorInfo a = (AuthorInfo) c.getAnnotation(AuthorInfo.class);

System.out.println("Author Information for " + c);
System.out.println("=======================================");
System.out.println("Author: " + a.author());
System.out.println("Email: " + a.email());
System.out.println("Version: " + a.version());

}
}

Calling Base Class Constructors and Constructor Chaining

C# and Java automatically call base class constructors, and both provide a way to call the constructor of the base class with specific parameters. Similarly both languages enforce that the call to the base class constructor occurs before any initializations in the derived constructor which prevents the derived constructor from using members that are yet to be initialized. The C# syntax for calling the base class constructor is reminiscent of the C++ initializer list syntax.
Both languages also provide a way to call a constructor from another which allows one to reduce the amount of code duplication that can occur in constructors. This practice is typically called constructor chaining.
C# Code

using System;

class MyException: Exception
{

private int Id;

public MyException(string message): this(message, null, 100){ }

public MyException(string message, Exception innerException):
this(message, innerException, 100){ }

public MyException(string message, Exception innerException, int id):
base(message, innerException){

this.Id = id;
}

}

Java Code


class MyException extends Exception{

private int Id;


public MyException(String message){

this(message, null, 100);
}

public MyException(String message, Exception innerException){

this(message, innerException, 100);

}

public MyException( String message,Exception innerException, int id){

super(message, innerException);
this.Id = id;

}
}

Variable Length Parameter Lists

In C and C++ it is possible to specify that a function takes a variable number of arguments. This functionality is used extensively in the printf and scanf family of functions. Both C# and Java allow one to define a parameter that indicates that a variable number of arguments are accepted by a method. In C#, the mechanism for specifying that a method accepts a variable number of arguments is by using the params keyword as a qualifier to the last argument to the method which should be an array. In Java, the same effect is achieved by appending the string "..." to the typename of the last argument to the method.
C# Code
using System;

class ParamsTest{

public static void PrintInts(string title, params int[] args){

Console.WriteLine(title + ":");

foreach(int num in args)
Console.WriteLine(num);

}


public static void Main(string[] args){

PrintInts("First Ten Numbers in Fibonacci Sequence", 0, 1, 1, 2, 3, 5, 8, 13, 21, 34);
}

}
Java Code
class Test{

public static void PrintInts(String title, Integer... args){

System.out.println(title + ":");

for(int num : args)
System.out.println(num);

}

public static void main(String[] args){

PrintInts("First Ten Numbers in Fibonacci Sequence", 0, 1, 1, 2, 3, 5, 8, 13, 21, 34);
}

}

Array Declarations

Java has two ways in which one can declare an array, one which is backwards compatible with the notation used in C & C++ and another which is generally accepted as being clearer to read, C# uses only the latter array declaration syntax.
C# Code

int[] iArray = new int[100]; //valid, iArray is an object of type int[]
float fArray[] = new float[100]; //ERROR: Won't compile

Java Code

int[] iArray = new int[100]; //valid, iArray is an object of type int[]
float fArray[] = new float[100]; //valid, but isn't clear that fArray is an object of type float[]

Declaring Constants

To declare constants in Java the final keyword is used. Final variables can be set either at compile time or run time. In Java, when the final is used on a primitive it makes the value of the primitive immutable while when used on object references it makes the reference constant meaning that the reference can only point to only one object during its lifetime. Final members can be left uninitialized when declared but then must be defined in the constructor.
To declare constants in C# the const keyword is used for compile time constants while the readonly keyword is used for runtime constants. The semantics of constant primitives and object references in C# is the same as in Java.
Unlike C++, it is not possible to specify an immutable class via language constructs in either C# or Java. Neither is it possible to create a reference through which it's impossible to modify a mutable object.
C# Code

using System;

public class ConstantTest{

/* Compile time constants */
const int i1 = 10; //implicitly a static variable

// code below won't compile because of 'static' keyword
// public static const int i2 = 20;

/* run time constants */
public static readonly uint l1 = (uint) DateTime.Now.Ticks;

/* object reference as constant */
readonly Object o = new Object();

/* uninitialized readonly variable */
readonly float f;


ConstantTest() {
// unitialized readonly variable must be initialized in constructor
f = 17.21f;
}

}

Java Code

import java.util.*;

public class ConstantTest{

/* Compile time constants */
final int i1 = 10; //instance variable
static final int i2 = 20; //class variable

/* run time constants */
public static final long l1 = new Date().getTime();

/* object reference as constant */
final Vector v = new Vector();

/* uninitialized final */
final float f;


ConstantTest() {
// unitialized final variable must be initialized in constructor
f = 17.21f;
}

}
NOTE: The Java language also supports having final parameters to a method. This functionality is non-existent in C#.


The primary use of final parameters is to allow arguments to a method to be accessible from within inner classes declared in the method body.

Primitive Types

For every Java primitive type there is a corresponding C# type which has the same name (except for byte). The byte type in Java is signed and is thus analagous to the sbyte type in C# and not the byte type.C# also has unsigned versions of some primitives such as ulong, uint, ushort and byte . The only significantly different primitive in C# is the decimal type, a type which stores decimal numbers without rounding errors (at the cost of more space and less speed).

Below are different ways to declare real valued numbers in C#.
C# Code

decimal dec = 100.44m; //m is the suffix used to specify decimal numbers
double dbl = 1.44e2d; //e is used to specify exponential notation while d is the suffix used for doubles

Reflection

The ability to discover the methods and fields in a class as well as invoke methods in a class at runtime, typically called reflection, is a feature of both Java and C#. The primary difference between reflection in Java versus reflection in C# is that reflection in C# is done at the assembly level while reflection in Java is done at the class level. Since assemblies are typically stored in DLLs, one needs the DLL containing the targeted class to be available in C# while in Java one needs to be able to load the class file for the targeted class. The examples below which enumerate the methods in a specified class should show the difference between reflection in C# and Java.
C# Code

using System;
using System.Xml;
using System.Reflection;
using System.IO;

class ReflectionSample {


public static void Main( string[] args){


Assembly assembly=null;
Type type=null;
XmlDocument doc=null;

try{
// Load the requested assembly and get the requested type
assembly = Assembly.LoadFrom("C:\\WINNT\\Microsoft.NET\\Framework\\v1.0.2914\\System.XML.dll");
type = assembly.GetType("System.Xml.XmlDocument", true);

//Unfortunately one cannot dynamically instantiate types via the Type object in C#.
doc = Activator.CreateInstance("System.Xml","System.Xml.XmlDocument").Unwrap() as XmlDocument;

if(doc != null)
Console.WriteLine(doc.GetType() + " was created at runtime");
else
Console.WriteLine("Could not dynamically create object at runtime");

}catch(FileNotFoundException){
Console.WriteLine("Could not load Assembly: system.xml.dll");
return;
}catch(TypeLoadException){
Console.WriteLine("Could not load Type: System.Xml.XmlDocument from assembly: system.xml.dll");

return;
}catch(MissingMethodException){
Console.WriteLine("Cannot find default constructor of " + type);
}catch(MemberAccessException){
Console.WriteLine("Could not create new XmlDocument instance");
}

// Get the methods from the type
MethodInfo[] methods = type.GetMethods();

//print the method signatures and parameters
for(int i=0; i < methods.Length; i++){

Console.WriteLine ("{0}", methods[i]);

ParameterInfo[] parameters = methods[i].GetParameters();

for(int j=0; j < parameters.Length; j++){
Console.WriteLine (" Parameter: {0} {1}", parameters[j].ParameterType, parameters[j].Name);
}

}//for (int i...)

}

}
Java Code

import java.lang.reflect.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

class ReflectionTest {

public static void main(String[] args) {

Class c=null;
Document d;

try{

c = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument().getClass();
d = (Document) c.newInstance();

System.out.println(d + " was created at runtime from its Class object");

}catch(ParserConfigurationException pce){
System.out.println("No document builder exists that can satisfy the requested configuration");
}catch(InstantiationException ie){
System.out.println("Could not create new Document instance");
}catch(IllegalAccessException iae){
System.out.println("Cannot access default constructor of " + c);
}

// Get the methods from the class
Method[] methods = c.getMethods();

//print the method signatures and parameters
for (int i = 0; i < methods.length; i++) {

System.out.println( methods[i]);

Class[] parameters = methods[i].getParameterTypes();

for (int j = 0; j < parameters.length; j++) {
System.out.println("Parameters: " + parameters[j].getName());
}

}
}
}
One might notice from the above code samples that there is slightly more granularity in the C# Reflection API than the Java Reflection API as can be seen by the fact that C# has a ParameterInfo class which contains metadata about the parameters of a Method while Java uses Class objects for that which lose some information such as the name of the parameter.
Sometimes there is a need to obtain the metadata of a specific class encapsulated as an object. This object is the java.lang.Class object in Java and the System.Type object in C#. To retrieve this metadata class from an instance of the target class, the getClass() method is used in Java while the GetType() method is used in C#. If the name of the class is known at compile time then one can avoid creating an instance of the class just to obtain the metadata class by doing the following
C# Code

Type t = typeof(ArrayList);

Java Code

Class c = java.util.Arraylist.class; /* Must append ".class" to fullname of class */

Access Modifiers

Below is a table mapping C# access modifiers to Java's. C++ fans who were disappointed when Sun changed the semantics of the protected keyword in Java 2 will be happy to note that the C# protected keyword has the same semantics as the C++ version. This means that a protected member can only be accessed by member methods in that class or member methods in derived classes but is inaccessible to any other classes. The internal modifier means that the member can be accessed from other classes in the same assembly as the class. The internal protected modifier means that a member can be accessed from classes that are in the same assembly or from derived classes.
C# access modifier Java access modifier
private private
public public
internal protected
protected N/A
internal protected N/A
NOTE: The default accessibility of a C# field or method when no access modifier is specified is private while in Java it is protected (except that derived classes from outside the package cannot inherit the field).

Constructors, Destructors and Finalizers

The syntax and semantics for constructors in C# is identical to that in Java. C# also has the concept of destructors which use syntax similar to C++ destructor syntax but have the mostly the same semantics as Java finalizers. Although finalizers exist doing work within them is not encouraged for a number of reasons including the fact that there is no way to control the order of finalization which can lead to interesting problems if objects that hold references to each other are finalized out of order. Finalization also causes more overhead because objects with finalizers aren't removed after the garbage collection thread runs but instead are eliminated after the finalization thread runs which means they have to be maintained in the system longer than objects without finalizers. Below are equivalent examples in C# and Java.
NOTE: In C#, destructors(finalizers) automatically call the base class finalizer after executing which is not the case in Java.
C# Code
using System;

public class MyClass {

static int num_created = 0;
int i = 0;

MyClass(){
i = ++num_created;
Console.WriteLine("Created object #" + i);
}

~MyClass(){
Console.WriteLine("Object #" + i + " is being finalized");
}

public static void Main(string[] args){

for(int i=0; i < 10000; i++)
new MyClass();

}

}

Java Code
public class MyClass {

static int num_created = 0;
int i = 0;

MyClass(){
i = ++num_created;
System.out.println("Created object #" + i);
}

public void finalize(){
System.out.println("Object #" + i + " is being finalized");
}

public static void main(String[] args){

for(int i=0; i < 10000; i++)
new MyClass();

}

}

Synchronizing Methods and Code Blocks

In Java it is possible to specify synchronized blocks of code that ensure that only one thread can access a particular object at a time and then create a critical section of code. C# provides the lock statement which is semantically identical to the synchronized statement in Java.
C# Code

public void WithdrawAmount(int num){

lock(this){

if(num < this.amount)
this.amount -= num;

}

}

Java Code

public void withdrawAmount(int num){

synchronized(this){

if(num < this.amount)
this.amount -= num;

}

}
Both C# and Java support the concept of synchronized methods. Whenever a synchronized method is called, the thread that called the method locks the object that contains the method. Thus other threads cannot call a synchronized method on the same object until the object is unlocked by the first thread when it finishes executing the synchronized method. Synchronized methods are marked in Java by using the synchronized keyword while in C# it is done by annotating the method with the [MethodImpl(MethodImplOptions.Synchronized)] attribute. Examples of synchronized methods are shown below

C# Code
using System;
using System.Runtime.CompilerServices;

public class BankAccount{

[MethodImpl(MethodImplOptions.Synchronized)]
public void WithdrawAmount(int num){

if(num < this.amount)
this.amount - num;

}

}//BankAccount

Java Code

public class BankAccount{

public synchronized void withdrawAmount(int num){

if(num < this.amount)
this.amount - num;

}

}//BankAccount

Namespaces vs Packages

A C# namespace is a way to group a collection of classes and is used in a manner similar to Java's package construct. Users of C++ will notice the similarities between the C# namespace syntax and that in C++. In Java, the package names dictate the directory structure of source files in an application whereas in C# namespaces do not dictate the physical layout of source files in directories only their logical structure. Examples below:
C# Code

namespace com.carnage4life{

public class MyClass {

int x;

void doStuff(){}

}

}

Java Code
package com.carnage4life;

public class MyClass {

int x;

void doStuff(){}

}
C# namespace syntax also allows one to nest namespaces in the following way
C# Code
using System;

namespace Company{

public class MyClass { /* Company.MyClass */

int x;

void doStuff(){}

}

namespace Carnage4life{

public class MyOtherClass { /* Company.Carnage4life.MyOtherClass */

int y;

void doOtherStuff(){}

public static void Main(string[] args){

Console.WriteLine("Hey, I can nest namespaces");
}

}// class MyOtherClass
}// namespace Carnage4life
}// namespace Company

Run Time Type Identification (is operator)

The C# is operator is completely analogous to Java's instanceof operator. The two code snippets below are equivalent.

C# Code

if(x is MyClass)
MyClass mc = (MyClass) x;

Java Code

if(x instanceof MyClass)
MyClass mc = (MyClass) x;

Main Method

The entry point of both C# and Java programs is a main method. There is a superficial difference in that the Main method in C# begins with an uppercase "M" (as do all .NET Framework method names, by convention) while the main method in Java begins with a lowercase "m" (as do all Java method names, by convention). The declaration for the main method is otherwise the same in both cases except for the fact that parameter to the Main() method in C# can have a void parameter.
C# Code
using System;

class A{

public static void Main(String[] args){

Console.WriteLine("Hello World");

}
}

Java Code
class B{

public static void main(String[] args){

System.out.println("Hello World");

}
}
It is typically recommended that one creates a main method for each class in an application to test the functionality of that class besides whatever main method actually drives the application. For instance it is possible to have two classes, A and B, which both contain main methods. In Java, since a class is the unit of compilation then all one has to do is invoke the specific class one wants run via the command line to run its main method. In C# one can get the same effect by compiling the application with the /main switch to specify which main should be used as the starting point of the application when the executable is created. Using test mains in combination with conditional compilation via preprocessor directives is a powerful testing technique.

Java Example
C:\CodeSample> javac A.java B.java

C:\CodeSample> java A
Hello World from class A

C:\CodeSample> java B
Hello World from class B

C# Example
C:\CodeSample> csc /main:A /out:example.exe A.cs B.cs

C:\CodeSample> example.exe
Hello World from class A

C:\CodeSample> csc /main:B /out:example.exe A.cs B.cs

C:\CodeSample> example.exe
Hello World from class B
So in Java's favor, one doesn't have to recompile to change which main is used by the application while a recompile is needed in a C# application. However, On the other hand, Java doesn't support conditional compilation, so the main method will be part of even your released classes.

Boxing

In situations where value types need to be treated as objects, the .NET and Java runtimes automatically converts value types to objects by wrapping them within a heap-allocated reference type in a process called boxing. The process of automatically convert an object to its corresponding value type such as converting an instance of java.lang.Integer to an int is known as unboxing. Below are examples of various situations where boxing occurs in both runtimes.


C# Code
using System;
using System.Collections;

//stack allocated structs also need to be boxed to be treated as objects
struct Point{

//member fields
private int x;
private int y;

public Point (int x, int y){

this.x = x;
this.y = y;

}

public override string ToString(){

return String.Format("({0}, {1})", x, y);

}


}//Point

class Test{


public static void PrintString(object o){

Console.WriteLine(o);

}

public static void Main(string[] args){

Point p = new Point(10, 15);
ArrayList list = new ArrayList();
int z = 100;

PrintString(p); //p boxed to object when passed to PrintString
PrintString(z); //z boxed to object when passed to PrintString

// integers and float boxed when stored in collection
// therefore no need for Java-like wrapper classes
list.Add(1);
list.Add(13.12);
list.Add(z);

for(int i =0; i < list.Count; i++)
PrintString(list[i]);

}

}

Java Code
import java.util.*;

class Test{

public static void PrintString(Object o){

System.out.println(o);

}

public static void PrintInt(int i){

System.out.println(i);

}

public static void main(String[] args){

Vector list = new Vector();
int z = 100;
Integer x = new Integer(300);
PrintString(z); //z boxed to object when passed to PrintString
PrintInt(x); //x unboxed to int when passed to PrintInt

// integers and float boxed when stored in collection
// therefore no need for Java wrapper classes
list.add(1);
list.add(13.12);
list.add(z);

for(int i =0; i < list.size(); i++)
PrintString(list.elementAt(i));

}

}

Member Initialization at Definition and Static Constructors

Instance and static variables can be initialized at their point of definition in both C# and Java. If the member variable is an instance variable, then initialization occurs just before the constructor is called. Static members are initialized sometime before the first usage of the member and before the first creation of an instance of the class. It is also possible to specify a block of code that should run before the class is used either via creation of an instance variable or invocation of a static method. These code blocks are called are called static constructors in C# and static initialization blocks in Java. Static constructors are invoked before the first invocation of a static method in the class and before the first time an instance of the class is created.
C# Code
using System;

class StaticInitTest{


string instMember = InitInstance();

string staMember = InitStatic();


StaticInitTest(){
Console.WriteLine("In instance constructor");
}

static StaticInitTest(){
Console.WriteLine("In static constructor");
}


static String InitInstance(){
Console.WriteLine("Initializing instance variable");
return "instance";
}

static String InitStatic(){
Console.WriteLine("Initializing static variable");
return "static";
}


static void DoStuff(){
Console.WriteLine("Invoking static DoStuff() method");
}


public static void Main(string[] args){

Console.WriteLine("Beginning main()");

StaticInitTest.DoStuff();

StaticInitTest sti = new StaticInitTest();

Console.WriteLine("Completed main()");

}

}

Java Code
class StaticInitTest{


String instMember = initInstance();

String staMember = initStatic();


StaticInitTest(){
System.out.println("In instance constructor");
}

static{
System.out.println("In static constructor");
}


static String initInstance(){
System.out.println("Initializing instance variable");
return "instance";
}

static String initStatic(){
System.out.println("Initializing static variable");
return "static";
}


static void doStuff(){
System.out.println("Invoking static DoStuff() method");
}


public static void main(String[] args){

System.out.println("Beginning main()");

StaticInitTest.doStuff();

StaticInitTest sti = new StaticInitTest();

System.out.println("Completed main()");

}

}


OUTPUT FROM BOTH EXAMPLES:
In static constructor
Beginning main()
Invoking static DoStuff() method
Initializing instance variable
Initializing static variable
In instance constructor
Completed main()

Throwing and Catching Exceptions

Exceptions in C# and Java share a lot of similarities. Both languages support the use of the try block for indicating guarded regions, the catch block for handling thrown exceptions and the finally block for releasing resources before leaving the method. Both languages have an inheritance hierarchy where all exceptions are derived from a single Exception class. Exceptions can be caught and rethrown after some error handling occurs in both languages. Finally, both languages provide a mechanism for wrapping exceptions in one another for cases where a different exception is rethrown from the one that was caught. An example of using the exception wrapping capability is a three tier application where a SQLException is thrown during database access but is caught, examined, then an application specific exception is thrown. In this scenario the application specific exception can be initialized with the original SQLException so handlers of the application specific exception can access the original exception thrown if needed. Below are two equivalent code samples that show the similarities between exceptions in both languages.


NOTE: Although exceptions in both languages support methods for getting a stack trace, only Java exceptions have methods that allow one to alter the stack trace.
C# Code
using System;
using System.IO;

class MyException: Exception{

public MyException(string message): base(message){ }

public MyException(string message, Exception innerException):
base(message, innerException){ }


}

public class ExceptionTest {


static void DoStuff(){
throw new FileNotFoundException();
}


public static void Main(string[] args){

try{

try{

DoStuff();
return; //won't get to execute

}catch(IOException ioe){ /* parent of FileNotFoundException */

throw new MyException("MyException occured", ioe); /* rethrow new exception with inner exception specified */
}

}finally{

Console.WriteLine("***Finally block executes even though MyException not caught***");

}

}//Main(string[])

} // ExceptionTest

Java Code

class MyException extends Exception{

public MyException(String message){ super(message); }

public MyException(String message, Exception innerException){ super(message, innerException); }


}

public class ExceptionTest {


static void doStuff(){
throw new ArithmeticException();
}


public static void main(String[] args) throws Exception{

try{

try{

doStuff();
return; //won't get to execute

}catch(RuntimeException re){ /* parent of ArithmeticException */

throw new MyException("MyException occured", re); /* rethrow new exception with cause specified */
}

}finally{

System.out.println("***Finally block executes even though MyException not caught***");

}

}//main(string[])

} // ExceptionTest

Inheritance in c# and java

Interfaces, Yes. Multiple Inheritance, No

C#, like Java, supports the concept of an interface which is akin to a pure abstract class. Similarly C# and Java both allow only single inheritance of classes but multiple inheritance (or implementation) of interfaces.

Unextendable Classes

Both Java and C# provide mechanisms to specify that a class should be the last one in an inheritance hierarchy and cannot be used as a base class. In Java this is done by preceding the class declaration with the final keyword while in C# this is done by preceding the class declaration with the sealed keyword. Below are examples of classes that cannot be extended in either language
C# Code
sealed class Student {
string fname;
string lname;
int uid;
void attendClass() {}
}

Java Code
final class Student {
String fname;
String lname;
int uid;
void attendClass() {}
}



Inheritance Syntax

C# uses C++ syntax for inheritance, both for class inheritance and interface implementation as opposed to the extends and implements keywords.
C# Code
using System;

class B:A, IComparable{

int CompareTo(){}

public static void Main(String[] args){

Console.WriteLine("Hello World");

}
}

Java Code
class B extends A implements Comparable{

int compareTo(){}

public static void main(String[] args){

System.out.println("Hello World");

}
}
Since C# is aimed at transitioning C++ developers the above syntax is understandable although Java developers may pine for the Java syntax especially since it is clear from looking at the class declaration in the Java version whether the class is subclassing a class or simply implementing an interface while it isn't in the C# version without intimate knowledge of all the classes involved. Although it should be noted that in .NET naming conventions, interface names have an upper-case "I" prepended to their names (as in IClonable), so this isn't an issue for programs that conform to standard naming conventions.

Strings Are Immutable

C# has a System.String class which is analogous to the java.lang.String class. Both classes are immutable meaning that the values of the strings cannot be changed once the strings have been created. In both instances methods that appear to modify the actual content of a string actually create a new string to return, leaving the original string unchanged. Thus the following C# and Java code does not modify the string in either case
C# Code
String csString = "Apple Jack";
csString.ToLower(); /* Does not modify string, instead returns lower case copy of string */

Java Code
String jString = "Grapes";
jString.toLowerCase(); /* Does not modify string, instead returns lower case copy of string */
To create a string-like object that allows modification in C# it is advisable to use the System.Text.StringBuilder class whereas in Java one would use the java.lang.StringBuffer class.

NOTE: In C#, the string class can either be written as string or String.

Methods

No Global Methods


Just like Java and unlike C++, methods in C# have to be part of a class either as member or static methods.

Arrays Can Be Jagged

In languages like C and C++, each subarray of a multidimensional array must have the same dimensions. In Java and C# arrays do not have to be uniform because jagged arrays can be created as one-dimensional arrays of arrays. In a jagged array the contents of the array are arrays which may hold instances of a type or references to other arrays. For this reason the rows and columns in a jagged array need not have uniform length as can be seen from the following code snippet:
 int [][]myArray = new int[2][]; 
myArray[0] = new int[3];
myArray[1] = new int[9];
The above code snippet is valid for both C# and Java.

Heap Based Classes and Garbage Collection

In Java objects are created on the heap using the new keyword. Most classes in C# are created on the heap by using the new keyword. Also just as the JVM manages the destruction of objects so also does the CLR via a Mark and Compact garbage collection algorithm

NOTE: C# also supports stack-based classes, called value types.

Keyword Jumble

There are a large number of syntactic similarities between Java and C#, similarly almost every Java keyword has a C# equivalent except for a few like transient, throws and strictfp. Below is a table of Java and C# keywords with the Java keywords in red while the equivalent C# keywords are in blue.
C# keyword Java keyword C# keyword Java keyword C# keyword Java keyword C#
keyword
Java
keyword
abstract abstract extern native operator N/A throw throw
as N/A false false out N/A true true
base super finally finally override N/A try try
bool boolean fixed N/A params ... typeof N/A
break break float float partial N/A uint N/A
byte N/A for for private private ulong N/A
case case foreach for protected N/A unchecked N/A
catch catch get N/A public public unsafe N/A
char char goto goto1 readonly N/A ushort N/A
checked N/A if if ref N/A using import
class class implicit N/A return return value N/A
const const1 in N/A sbyte byte virtual N/A
continue continue int int sealed final void void
decimal N/A interface interface set N/A volatile volatile
default default internal protected short short where extends
delegate N/A is instanceof sizeof N/A while while
do do lock synchronized stackalloc N/A yield N/A
double double long long static static
:
extends
else else namespace package string N/A
:
implements
enum N/A new new struct N/A N/A strictfp
event N/A null null switch switch N/A throws
explicit N/A object N/A this this N/A transient2
NOTE: Although goto and const are Java language keywords they are unused in the Java language.
NOTE: The [NonSerialized] attribute in C# is equivalent to the transient keyword in Java.

Of Virtual Machines and Language Runtimes

Just like Java is typically compiled to Java byte code which then runs in managed execution environment (the Java Virtual Machine or JVM) so also is C# code compiled to an Intermediate Language (IL) which then runs in the Common Language Runtime (CLR). Both platforms support native compilation via Just In Time compilers.

NOTE: While the Java platform supports interpretation of byte code or byte code being JITed then run natively, the .NET platform only supports native execution of C# code because the IL code is always natively compiled before running.

We are all objects

Just like Java, C# has a single rooted class hierarchy where all classes in C# are subclasses of System.Object the same way all Java classes are subclasses of java.lang.Object. The methods of the two languages' Object classes share some similarities (e.g. System.Object's ToString() to java.lang.Object's toString()) and differences (System.Object does not have analogs to wait(), notify() or notifyAll() in java.lang.Object).

NOTE: In C#, the object class can either be written as object or Object. The lower case "object" is a C# keyword which is replaced with the class name "System.Object" during compilation.

Sunday, February 6, 2011

C# interview questions and answers


  1. What’s the advantage of using System.Text.StringBuilder over System.String? StringBuilder is more efficient in the cases, where a lot of manipulation is done to the text. Strings are immutable, so each time it’s being operated on, a new instance is created.
  2. Can you store multiple data types in System.Array? No.
  3. What’s the difference between the System.Array.CopyTo() and System.Array.Clone()? The first one performs a deep copy of the array, the second one is shallow.
  4. How can you sort the elements of the array in descending order? By calling Sort() and then Reverse() methods.
  5. What’s the .NET datatype that allows the retrieval of data by a unique key? HashTable.
  6. What’s class SortedList underneath? A sorted HashTable.
  7. Will finally block get executed if the exception had not occurred? Yes.
  8. What’s the C# equivalent of C++ catch (…), which was a catch-all statement for any possible exception? A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}.
  9. Can multiple catch blocks be executed? No, once the proper catch code fires off, the control is transferred to the finally block (if there are any), and then whatever follows the finally block.
  10. Why is it a bad idea to throw your own exceptions? Well, if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block? Throwing your own exceptions signifies some design flaws in the project.
  11. What’s a delegate? A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers.
  12. What’s a multicast delegate? It’s a delegate that points to and eventually fires off several methods.
  13. How’s the DLL Hell problem solved in .NET? Assembly versioning allows the application to specify not only the library it needs to run (which was available under Win32), but also the version of the assembly.
  14. What are the ways to deploy an assembly? An MSI installer, a CAB archive, and XCOPY command.
  15. What’s a satellite assembly? When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.
  16. What namespaces are necessary to create a localized application? System.Globalization, System.Resources.
  17. What’s the difference between // comments, /* */ comments and /// comments? Single-line, multi-line and XML documentation comments.
  18. How do you generate documentation from the C# file commented properly with a command-line compiler? Compile it with a /doc switch.
  19. What’s the difference between <c> and <code> XML documentation tag? Single line code example and multiple-line code example.
  20. Is XML case-sensitive? Yes, so <Student> and <student> are different elements.
  21. What debugging tools come with the .NET SDK? 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.
  22. What does the This window show in the debugger? It points to the object that’s pointed to by this reference. Object’s instance data is shown.
  23. 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.
  24. What’s the difference between the Debug class and Trace class? Documentation looks the same.Use Debug class for debug builds, use Trace class for both debug and release builds.
  25. Why are there five tracing levels in System.Diagnostics.TraceSwitcher? The tracing dumps can be quite verbose and for some applications that are constantly running you run the risk of overloading the machine and the hard drive there. Five levels range from None to Verbose, allowing to fine-tune the tracing activities.
  26. Where is the output of TextWriterTraceListener redirected? To the Console or a text file depending on the parameter passed to the constructor.
  27. How do you debug an ASP.NET Web application? Attach the aspnet_wp.exe process to the DbgClr debugger.
  28. What are three test cases you should go through in unit testing? Positive test cases (correct data, correct output), negative test cases (broken or missing data, proper handling), exception test cases (exceptions are thrown and caught properly).
  29. Can you change the value of a variable while debugging a C# application? Yes, if you are debugging via Visual Studio.NET, just go to Immediate window.
  30. Explain the three services model (three-tier application). Presentation (UI), business (logic and underlying code) and data (from storage or other sources).
  31. What are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET? SQLServer.NET data provider is high-speed and robust, but requires SQL Server license purchased from Microsoft. OLE-DB.NET is universal for accessing other sources, like Oracle, DB2, Microsoft Access and Informix, but it’s a .NET layer on top of OLE layer, so not the fastest thing in the world. ODBC.NET is a deprecated layer provided for backward compatibility to ODBC engines.
  32. What’s 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.
  33. What is the wildcard character in SQL? Let’s 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%’.
  34. Explain ACID rule of thumb for transactions. Transaction must be Atomic (it is one unit of work and does not dependent on previous and following transactions), Consistent (data is either committed or roll back, no “in-between” case where something has been updated and something hasn’t), Isolated (no transaction sees the intermediate results of the current transaction), Durable (the values persist if the data had been committed even if the system crashes right after).
  35. What connections does Microsoft SQL Server support? Windows Authentication (via Active Directory) and SQL Server authentication (via Microsoft SQL Server username and passwords).
  36. Which one is trusted and which one is untrusted? Windows Authentication is trusted because the username and password are checked with the Active Directory, the SQL Server authentication is untrusted, since SQL Server is the only verifier participating in the transaction.
  37. Why would you use untrusted verificaion? Web Services might use it, as well as non-Windows applications.
  38. What does the parameter Initial Catalog define inside Connection String? The database name to connect to.
  39. What’s the data provider name to connect to Access database? Microsoft.Access.
  40. What does Dispose method do with the connection object? Deletes it from the memory.
  41. What is a pre-requisite for connection pooling? Multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings.