Friday, January 25, 2008

String Comparison in C#, an interesting topic !

There are many ways of string comparison in C#. Following are some of them:

1. Using ==

2. Using String.Equals(a, b)

3. Using Object.Equals(a, b)

4. Using String.Compare(a, b)

5. Using a.Equals(b)

Here both a and b are string variables.

The overloaded == operator and String.Equals are equal in speed. The Object.Equals is slightly slower because it is virtual in nature. If Equals method is used, then String.Equals is a better option than Object.Equals. The a.Equals(b) is more slower than the earlier, but this is only significant in very large numbers of equality checks.

All the options 1, 2, 3 and 5 check the equality from the string content, rather than the reference. The option 3 is calling the
String.Equals because Equals is a virtual function.

String.Compare in other hand is totally different and it is not used for the same purpose. It is much slower than all other methods when checking if the strings are equal. All others check if the strings are equal, but the String.Compare, compares the strings more deeply and returns a number which indicates the lexical relationship between the two strings, so it is not really advised to use String.Compare when just checking if the strings contain the same text i.e. checking if the strings are equal.

Comments are Welcome :)

Thursday, January 24, 2008

VB.NET To C#.NET Code Converter Tool

Following links provide a tool for code conversion from C# to VB.NET and viceversa:

Wednesday, January 16, 2008

Visual Studio 2008 Shortcuts !

Friday, January 11, 2008

What is active directory?

Active Directory (AD) is an implementation of LDAP directory services by Microsoft for use primarily in Windows environments. Its main purpose is to provide central authentication and authorization services for Windows based computers. Active Directory also allows administrators to assign policies, deploy software, and apply critical updates to an organization. Active Directory stores information and settings in a central database. Active Directory networks can vary from a small installation with a few hundred objects, to a large installation with millions of objects.Microsoft Active Directory provides a directory service that allows organizations to administer their networked resources. The networked resources include users, computers etc. Thus it acts as a central store for users in a particular network. This store can be used for authentication purpose. Applications can request the active directory service for user authentication.

A sample code to authenticate a user against the active directory.

/// <summary>
/// int: Interactive Logon.
/// </summary>
public const int LOGON32_LOGON_INTERACTIVE = 2;
/// <summary>
/// int: Default Provider.
/// </summary>
public const int LOGON32_PROVIDER_DEFAULT = 0;
/// <summary>
/// int: Network Logon.
/// </summary>
public const int LOGON32_LOGON_NETWORK = 3;

/// <summary>
/// LogonUser is a Windows API, which is used to logon a win user.
/// </summary>
/// <param name="lpszUserName">string: User Name.</param>
/// <param name="lpszDomain">string: Domain name.</param>
/// <param name="lpszPassword">string: Password.</param>
/// <param name="dwLogonType">int: Logon Type.</param>
/// <param name="dwLogonProvider">int: Logon provider.</param>
/// <param name="phToken">ref IntPtr: Logon Token.</param>
/// <returns>int: Status of the user logon.</returns>
/// <remarks>Use to Logon the user to Windows network.</remarks> [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int LogonUser(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

/// <summary>
/// This method authenticates the user agains the current domain
/// and returns a boolean value.
/// </summary>
/// <returns>bool: Indicating whether the authentication
/// is successfull.</returns>
public bool AuthenticateUser()
string _domainName;
IntPtr _token = IntPtr.Zero;
_domainName = Environment.GetEnvironmentVariable("USERDOMAIN");
//LogonUser to check Windows Logon.
int _intResult LogonUser("USER_NAME", _domainName, "PASSWORD",

return Convert.ToBoolean(_intResult); ;