Rajesh kumar’s Blog

If you have knowledge, let others light their candles at it.

Archive for the ‘.Net’ Category

Create non zero indexed arrays in C#

Posted by Rajesh Kumar on December 19, 2011

99.99% of the time, we write our code with zero indexed arrays. However, C# allows us to create non zero based (single or multi dimensional arrays).

The syntax from MSDN is:

public static Array CreateInstance(   
                Type elementType,   
                int[] lengths,
                int[] lowerBounds)

CreateInstance is a static method available in Array class. Here, variable "lengths" indicates the dimension of the array and "lowerBounds" indicates what is the starting lower bound of each dimension array.

To understand the concept in a simple way, let’s consider the single dimension array and the following code is used to create the instance

// Create the instance of an Array   
Array myArray = Array.CreateInstance(typeof(int), new[] { 4 }, new[] { 2011 });
// Set the values to array  
myArray.SetValue(5, 2005);   
myArray.SetValue(6, 2006);   
myArray.SetValue(7, 2007);   
myArray.SetValue(8, 2008);   
// Display the lower bound of the array  
// Display the upper bound of the array  
// Display all the items in the array  
for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++)  


1. More meaningful index, based on scenario.


  1. Slow performance.
  2. Non CLS complaint.

Posted in .Net | Tagged: , , | Leave a Comment »

Debug the .Net code from remote machine

Posted by Rajesh Kumar on July 11, 2011

Debugging the code from development environment is very easy. The actual problem starts when we want to debug the code that is already deployed in some system (local system, remote system, production server etc.).

It’s because, such environments will not have any SDK installed. However, Microsoft still makes developer’s life easier by allowing us to debug such deployed code from our local machine. This article tries to explain it.

Limitation: Visual Studio Professional, Premium and Ultimate versions can be used to debug the code. Visual studio Express does NOT allow you to debug the code through a process.

What needs to be done?

We need to do two things for debugging from remote machine:

  • Preparing remote system.
  • Attach the deployed code to source code from local machine.

What are required?

We need two things to start the remote debugging:

  • PDB files
  • MSVSMON.exe
  • You must have access to the remote system.
  • Your remote and local system must be in same domain (or in trusted domain list).

If you are not sure what are pdb files, A program database (PDB) file holds debugging and project state information that allows incremental linking of a debug configuration of your program. A PDB file is created when you build with /debug. (extracted from MSDN).

MSVSMON (Microsft Visual Studio remote debugging monitor) acts as an agent from remote system for Visual Studio. It comes with SDK installation and is generally available at C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Remote Debugger\x64\msvsmon.exe. (x64 indicates your operating system mode).

The most important point to remember is, we must have the same version of PDB files of the original source code deployed on remote machine. Please read this article for more information.

Preparing remote system:

Please feel free to copy the entire x64 (as mentioned above) to the remote machine where the code is deployed and run the msvsmon.exe file. You may see the screen as given below:


Now, move all .pdb files to the bin directory of the deployed application. Now, it’s the time to identify the process id of the application that you want to debug. Task Manager shows list of processes running on the current (remote)and its Ids against them. It is very straight forward for finding process Ids for services or exe files. However, it is bit different for web application. Please read my article here to find out more.

Attach the deployed code to source code from local machine:

Open the source code in Visual Studio. Go to “Debug –> Attach to Process” option. You should be able to view “Attach to Process” window.

Leave the “Transport” to Default. In the “Qualifier” text box, type the text as <DomainName\UserName@ServerName> as displayed in msvsmon.exe window given above and then hit Enter key.

With this, Visual studio makes the connection to remote system and gets all processes list running on remote machine. You may see the screens in local machine and remote machine as below:

image image

Now chose, the right process Id and hit “Attach” button. With this, you are ready to debug the code. Happy debugging Smile.

References: MSDN

Posted in .Net, Debugging, IIS, Visual Studio | Tagged: | 5 Comments »

Compare WCF with other Technologies

Posted by Rajesh Kumar on April 20, 2010

The below diagram (extracted from Microsoft web) gives us the comparison of WCF with other services:

Posted in .Net, WCF | Leave a Comment »

.net based Product Activation

Posted by Rajesh Kumar on September 24, 2009

Add License Validation, Product Activation, and Installation Keys to your .Net based application. Prevent casual copying and keep your customers honest. Similar to Microsoft style product activation where the product key is validated against the machine specific identifiers and validated against an internet server. Supports server based creation, maintenance and validation of install key codes.

Find more information with Source code at: InstallKey

Posted in .Net | Leave a Comment »

Visual Studio 2008 Extentions / Power Commands

Posted by Rajesh Kumar on September 24, 2009

Microsoft has released extensions for Visual Studio which makes developers life easy. It has the following features:

  • Enable/Disable PowerCommands in Options dialog
  • Format document on save / Remove and Sort Usings on save
  • Clear All Panes
  • Copy Path
  • Email CodeSnippet
  • Insert Guid Attribute
  • Show All Files
  • Undo Close
  • Collapse Projects
  • Copy Class
  • Paste Class
  • Copy References
  • Paste References
  • Copy As Project Reference
  • Edit Project File
  • Open Containing Folder
  • Open Command Prompt
  • Unload Projects
  • Reload Projects
  • Remove and Sort Usings
  • Extract Constant
  • Clear Recent File List
  • Clear Recent Project List
  • Transform Templates
  • Close All
  • You can find more detailed information and msi file from: PowerCommands

    Posted in .Net, Visual Studio | Leave a Comment »

    Remove unused namespaces from Visual Studio

    Posted by Rajesh Kumar on August 12, 2009

    When we add any new class file, Visual Studio 2008 by default adds few namespaces depending on the project type. But, in real time – we will not be using one or all of the namespaces.

    At the same time, most of us know only few namespace of the objects that we are going to use. So, it is a bit difficult to identify and remove the namespace. So, how can we make the code clean?

    Fortunately, Visual Studio allows to Sort, Remove and Remove & Sort unused namespaces. The following pictures shows you how the class file looks like before and after removing the unused namespaces:

    Default Class Structure

    Default Class StructureContext menu in Visual Studio


    Context menu in Visual Studio

    Context menu in Visual Studio

    Class structure after Removing and Sorting unused namespaces

    Class structure after Removing and Sorting unused namespaces

    This is a very cool and useful feature 🙂

    Posted in .Net, Visual Studio | 2 Comments »

    Single Sign On Alpha 1.1 released

    Posted by Rajesh Kumar on July 28, 2009

    Development of Single Sign On is going smoothly. There is a good response for Alpha 1.0 version.

    As per Codeplex’s statistics, there were 122 downloads.

    I have released Alpha 1.1 version on July 10, 2009. This version includes a Silverlight based UI to test the Single Sign On Service. UI displays the list of Online users and Requested Tokens.

    Please have a look at SSO project at: http://sso.codeplex.com/

    Posted in .Net, Single Sign On, WCF | Leave a Comment »

    Single Sign On – Light Weight

    Posted by Rajesh Kumar on July 2, 2009

    As a journey towards the Single Sign On solution, I have creted a project in Codeplex. It is just an initial version.

    Developed using WCF, ASP.Net and Silverlight to track the online users. Please have a look at: http://sso.codeplex.com/

    Posted in .Net, Single Sign On, WCF | Leave a Comment »

    Rampup (Ascend to new heights in your career)

    Posted by Rajesh Kumar on June 10, 2009

    If at all you are looking for one or more of the below:

    • Web Development with ASP.NET
    • Move from PHP to ASP.NET
    • Move from ASP to ASP.NET
    • Develop Windows Mobile 6 Applications
    • SharePoint for Developers – Part 1
    • SharePoint for Developers – Part 2
    • Visual Studio 2008
    • For the VS 2002/2003 Developer: Learn VS 2005
    • For the VB 6.0 Developer: Learn VB 2005
    • For the Java Developer: Learn .NET
    • Developer Basics

    Its worth browsing:   http://msdn.microsoft.com/hi-in/rampup/default(en-us).aspx

    Posted in .Net | Leave a Comment »

    Behavior of dynamic type in C# 4.0

    Posted by Rajesh Kumar on June 3, 2009

    If you wish to get the type of any object dynamically and then invoke its methods are properties at run time, then C# 4.0 provides a new type called dynamic.

    With this dynamic type, we can avoid compile time exceptions for method or property call. Compiler will just ignore dynamic types at the time of compilation. However, they will be evaluated at run time.

    During the compile time, the variables of dynamic types are compiled into “object” type. So, type “dynamic” exists only at compile time, but not at run time.

    This dynamic type can be used in following scenarios:

    • A private field
    • A public property
    • Method return type

    Posted in .Net | Leave a Comment »