Monday, September 28, 2009

Enable Sql Server Authentication Mode Over Network

Check the followings:
1. Right click the root node on Object Explorer, in Server Properties window->Security->Server authentication: SQL Server and Windows Authentication mode
2. Create a new user for login.
3. Right click the user on Object Explorer, in Login Properties window:
    3.1 User Mapping: Check the desired database to access
    3.2 Status->Permission to connect to database engine: Grant; Login: Enabled
4. Sql Server Configuration Manager -> Enable TCP/IP and Named Pipes

Thursday, September 17, 2009

Object.GetHashCode, Object.Equals, operator==, and related

After some research, I summarize some points below as guidelines.

1. Object.Equals method and operator== are supposed to be used interchangeably. (It applies to all the .net classes) Therefore, when implementing a new class, it's a good idea to always implement both of them.
Equals() is usually used (implemented) for value equality and Operator== is used for reference equality by default. When a type is an immutable type (e.g. String), overloading Operator== to compare value equality makes sense.

Guidelines for Overloading Equals() and Operator==

Override equals on overloading operator equals

When should I use == and when should I use Equals

2. It is important to override GetHashCode when overriding Equals method. Although failure to do so will not always break your code, things can go wrong if the class is used as a key in a dictionary etc. In that situation, if the hash-code for two items does not match, they may never be considered equal (Equals will never be called). To write the GetHashCode, we need to make sure that: if two objects are equal, then they must return the same value for GetHashCode. On the other hand, we do NOT have to make sure two different objects always return different values for GetHashCode.

Why is it important to override GetHashCode when Equals method is overriden in C#

3. A good implementation of GetHashCode that can be used in many situations:
public int GetHashCode()
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + field1.GetHashCode();
hash = hash * 23 + field2.GetHashCode();
hash = hash * 23 + field3.GetHashCode();
return hash;
What is the best algorithm for an overridden System.Object.GetHashCode

4. Implementing IEquatable Properly