Sunday, 20 October 2013

C# interview question string reference type basics

String data type

String is a reference data type in C# .NET framework. Since it is a reference type in .NET framework it has to be with new keyword or by initializing as a literal by providing a value. The keyword string (s in small case) is an alias for String (S in capital case). Both the types string and String are same.


When two string variables are compared, the comparison happens between the content to which the variables store reference. C# doesn't compare the references but instead compares the values to which the string variables hold reference. Thus the operators == and != both carry out intended comparison. The Equals method in string class also performs comparison between two string types. The default Equals method performs case-sensitive and culture-insensitive comparison. Other overloads of Equals method provide desired case and culture based comparison.


Strings in C# .NET are immutable i.e. once string object is created, its contents cannot be changed. In the below example, first we create a initialize a string variable with certain value. The effect of this is that an object with the value "hello" is created on heap and its reference gets assigned to the variable myStr. In the second line, when we assign new value to myStr by appending "world", a new object with the value "helloworld" is created and returned back. Thus myStr now has reference to a new object with new value. The previous object which contained value "hello" is now not used in any variables and so will be cleaned by GC.

string myStr = "hello";
myStr = myStr + "world";

More reading:
  1. Check out what is String.Intern
Hope this is useful.

C# interview question - value and reference types

Value types

Value types store data. These are allocated on the stack. E.g. int, bool, structs, enums, float, decimal
Declaration is enough to allocate space for value type.
More reading:
  1. Value Types documentation

Reference types

Conceptually same as pointers. They store reference to actual data and not the data. E.g. String, Class types such System.IO.StringReader, user defined classes, etc.
Important to note that all arrays are references even if defined type of array is of value type. Using new keyword is essential to initialize and allocate space for reference types.
More reading:
  1. Reference Types documentation
More interesting reading:

Saturday, 19 October 2013

TFS proxy server configuration in Visual Studio

If you work in a larger team that uses TFS for source control, then most likely your admins have setup a TFS proxy server to improve TFS performance for you as a TFS user.

Using TFS proxy server makes file downloads faster if the source files you are accessing is recently/frequently fetched.

Note the name of your TFS proxy server and follow below steps to configure using it in Visual Studio:

  1. Open Visual Studio and select Tools -> Options
  2. In the Options box, click on "Source Control" node and from the drop down select "Visual Studio Team Foundation Server" as "Current source control plug-in"
  3. Next click on "Visual Studio Team Foundation Server" node within "Source Control" node on left hand side and enable the "Use proxy server for file downloads" checkbox
  4. Enter the proxy server name and port number respectively
  5. Click OK
Your proxy configuration will take effect and next Get operation will go through proxy.
Note that if a Get operation is performed during configuring the proxy server, there is a chance that Visual Studio might have to be restarted for proxy to take effect.

Hope this helps.

Friday, 18 October 2013

TFS create workitem and get workitem URL/link/hyperlink

The workitem creation using TFS is well known. Here I am just sharing the code snippet which retrieves the URL for a newly created workitem. The URL returned is same as the URL generated when "Send to Outlook" button is clicked in TFS against a workitem.

WorkItemStore _wiStore = new WorkItemStore(tfsServer); //tfsServer is your TFS server URL

var project = _wiStore.Projects[<bugs project>]; //Put your project name containing bug type workitems

WorkItemType workItemType = project.WorkItemTypes["Bug"]; //Create new bug workitemtype

WorkItem _workItem = new WorkItem(workItemType); //Create bug workitem object

TswaClientHyperlinkService _hyperlinkService = tfsServer.GetService<TswaClientHyperlinkService>(); //Initialize TFS' hyperlink service

_workItem.Fields["Title"].Value = title;
_workItem.Fields["Area Path"].Value = areaPath;
_workItem.Fields["Iteration Path"].Value = iteration;
//initialize other WorkItem fields
var validationErrors = _workItem.Validate();
if (validationErrors.Count == 0)
string bugUrl = _hyperlinkService.GetArtifactViewerUrl(_workItem.Uri).ToString(); //The URL we are looking for

Hope this helps.

Fetch WorkItems with TFS API WorkItemStore NullReferenceException

While trying to retrieve TFS workitems, following code snippet is usually used:

var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://<server>:<port>/tfs/<collection>"));
var _workItemStore = projectCollection.GetService<WorkItemStore>();

This works well with .NET 4.5 Framework. But if the installed version is lower than 4.5 a NullReferenceException is thrown. This can be fixed by using a different way of initializing WorkItemStore object as below:

var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://<server>:<port>/tfs/<collection>"));
var _workItemStore = new WorkItemStore(projectCollection);

Hope this is helpful.

Using database table as a queue

There is lot of information out there regarding this particular topic.
I recently had the opportunity to evaluate this approach.


The requirement was to scale out a windows service across multiple boxes and enable each windows service instance to process incoming data independently. Job of the windows service is to read incoming data and process it one by one.


There are several approaches to achieve this. Simplest one is a file-based approach where each box processes incoming data which has been divided into multiple files and copied to a location accessible to it. This location can be a shared directory on remote machine or a location on the individual boxes.
The advantages with such an approach are:
  1. Simple to understand and implement
  2. Easy to debug/maintain/troubleshoot
  3. Easy to scale by adding new boxes
  4. Easy to load balance by putting the boxes behind a vip
But, there are some disadvantages in this approach:
  1. Need for a layer to partition and copy incoming data such that the partitioned data is available to the boxes
  2. If the boxes need to access the partitioned files from a shared location then there are possibilities of latency/staleness due to inherent delays in file sharing protocols.
  3. If it is required to maintain state of the file processing then additional implementation needs to be done to maintain state. E.g. if the contents of the file are Orders in case of an ecommerce site, then depending on the use case it would be required to know whether the Order processed was successful or failure or needs retries etc.
The requirement mentioned above fits in a queue processing pattern. There are at least 2 popular and scalable ways to address queue processing.
  1. Using database table as a queue
  2. Using a message queue/service bus queue/cloud based queue kind of infrastructure (a la Azure Storage Queue)
The mechanism by introducing a new piece of architechture such as database or storage queue would be to load items in the queue from producer end and drain/remove items in the queue from consumer's end.
Let us see advantages of using a database:
  1. The items to be processed are available in a single place - DB table.
  2. We can introduce a column to track status to indicate whether the item has been processed or not.
  3. Improved load-sharing/reliability - the multiple boxes accessing the table can simply keep processing unprocessed items, if some of the boxes go down others will keep working and complete all items eventually.
  4. Out of box support for simultaneous access. Mutual exclusion can be a problem if file based approach is used.
The disadvantages however are:
  1. Allocation of DB class machines/hardware to install DB - this is a capex
  2. Overhead of maintaining a DB which will add to opex.
In case of a storage queue, the advantages are similar to that of DB table. Storage queues offer clear implementation and operations for the Queue abstract data type. Scalability also comes out of the box. Though cost wise storage queues in cloud might be cheaper, but there would be some substantial initial cost that would have to be paid in terms of newness of system, learning curve, implementation, maintenance and production operability.
For my requirement, a file based approach was used ultimately based on agreement with the larger internal forum. Unless it is essential for your requirement to pay DB or storage queue costs, file based approach would most of the times be the desired approach.

If you are looking for more information visit below links: