Monday, July 14, 2008

What is the difference between a.Equals(b) and a == b?

The Equals method is just a virtual one defined in System.Object, and overridden by whichever classes choose to do so. The == operator is an operator which can be overloaded by classes, but which usually has identity behaviour.

For reference types where == has not been overloaded, it compares whether two references refer to the same object - which is exactly what the implementation of Equals does in System.Object.

Value types do not provide an overload for == by default. However, most of the value types provided by the framework provide their own overload. The default implementation of Equals for a value type is provided by ValueType, and uses reflection to make the comparison, which makes it significantly slower than a type-specific implementation normally would be. This implementation also calls Equals on pairs of references within the two values being compared.

However, the main difference between the two types of comparison in normal use (where you're unlikely to be defining your own value types very often) is polymorphism. Operators are overloaded, not overridden, which means that unless the compiler knows to call the more specific version, it'll just call the identity version.

To illustrate that, here's an example:

using System;

public class Test

{

static void Main()

{

// Create two equal but distinct strings

string a = new string(new char[] {'h', 'e', 'l', 'l', 'o'});

string b = new string(new char[] {'h', 'e', 'l', 'l', 'o'});

Console.WriteLine (a==b);

Console.WriteLine (a.Equals(b));

// Now let's see what happens with the same tests but

// with variables of type object

object c = a;

object d = b;

Console.WriteLine (c==d);

Console.WriteLine (c.Equals(d));

}

}

The results are:

True

True

False

True

The third line is False because the compiler can only call the non-overloaded version of = as it doesn't know that the contents of c and d are both string references. As they are references to different strings, the identity operator returns false.

So, when should you use which operator? for almost all reference types, use Equals when you want to test equality rather than reference identity. The exception is for strings - comparing strings with == does make things an awful lot simpler and more readable but you need to remember that both sides of the operator must be expressions of type string in order to get the comparison to work properly.

For value types, I'd normally use == for easier-to-read code. Things would get tricky if a value type provided an overload for == which acted differently to Equals, but I'd consider such a type very badly designed to start with.

What is strong-typing versus weak-typing? Which is preferred? Why?

In strongly-typed programming languages you usually have to declare variables prior to using them. Strong-typing is the strict enforcement of [type] rules. All types (int, short, long, string) are know at compile time and are statically bound. So C# is a strongly-typed language because variables must be assigned a type before you use them. If you came from the ASP world then you will remember having to use either Javascript or VBScript. Variables that you declare in either of those languages can hold any data type which makes it weakly-typed.

But lets take this up a level. Instead to talking about programming languages, lets talk about strongly-typed/weakly-typed objects. The DataSet object is a great example. If we use a weakly-typed dataset, the developer needs to know the name of the table and the name of the field being requested.

Since we are just passing strings, this code will compile and will not show any possible problems (like typing the name of the table wrong) until run time.

string s = (string) myDataSet.Tables["Customers"].Rows[0]["CustomerID"];


If our Dataset is Strongly-Typed, we are able to access the names of the tables and columns directly. Any errors are caught at compile time.

string s = myDataSet.Customers[0].CustomerID;

In simple manner we can say Strong type is checking the types of variables as soon as possible, usually at compile time. While weak typing is delaying checking the types of the system as late as possible, usually to run-time. Which is preferred depends on what you want. For scripts & quick stuff you’ll usually want weak typing, because you want to write as much less (is this a correct way to use Ensligh?) code as possible. In big programs, strong typing can reduce errors at compile time.

ref: http://www.dotnetdoc.com/PermaLink,guid,941038f1-f683-4caf-a425-e653c1968a15.aspx

What is the difference between an EXE and a DLL?

DLL - Dynamic Link Library

An ActiveX Dll runs is an in process server running in the same memory space as the client process.

EXE – Executable File

An ActiveX Exe is an out of process server which runs in its own separate memory space.

Advantages of ActiveX Dll-

1) An in-process component shares its client’s address space, so property and method calls don’t have to be marshaled. This results in much faster performance.

Disadvantages of ActiveX Dll-

1) If an unhandled error occurs it will cause the client process to stop operating.

Advantages of ActiveX Exe-

1) The component can run as a standalone desktop application, like Microsoft Excel or Microsoft Word, in addition to providing objects.

2) The component can process requests on an independent thread of execution, notifying the client of task completion using events or asynchronous call-backs. This frees the client to respond to the user.

3) If an error occurs the client processes can continue to operate.

Disadvantages of ActiveX Exe-

1) Generally slower than an ActiveX dll alternative

ref: http://forums.msdn.microsoft.com/en/csharpgeneral/thread/24c60557-4718-4a9f-bf87-a7d122cc7455/