Posts Tagged ‘C#’

JSON to C# object to JSON

Posted: September 8, 2011 in C#, Javascript, JQuery, JSON
Tags: , , ,

public class SearchFilter
public string SearchCol { get; set; }
public string SearchOp { get; set; }
public string SearchText { get; set; }
public int PageNo { get; set; }
using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
public class JSONHelper
public static T Deserialise<T>(string json)
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
return obj;

public static string Serialize<T>(T obj)
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream ms = new MemoryStream())
serializer.WriteObject(ms, obj);
return Encoding.Default.GetString(ms.ToArray());
Usage –

SearchFilter lSearchFilter = JSONHelper.Deserialise<SearchFilter>(“{‘SearchCol’:’Col1′,’SearchOp’:’contains’,’SearchText’:’hi’}”);

System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
<code block or method call or service call>
timer.ElapsedMilliseconds.ToString(); // capture this value using some logging mechanism to check it or you can debug it to see the value

There are many tools available in market to see the time scanning at each method between request to response life cycle.

C# Operators

Posted: October 15, 2010 in C#

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand

A nullable type can contain a value, or it can be undefined. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable type to a non-nullable type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable type is currently undefined, an InvalidOperationException exception will be thrown.

    static int? GetNullableInt()
        return null;

    static string GetStringValue()
        return null;

    static void Main()
        // ?? operator example.
        int? x = null;

        // y = x, unless x is null, in which case y = -1.
        int y = x ?? -1;

        // Assign i to return value of method, unless
        // return value is null, in which case assign
        // default value of int to i.
        int i = GetNullableInt() ?? default(int);

        string s = GetStringValue();
        // ?? also works with reference types.
        // Display contents of s, unless s is null,
        // in which case display “Unspecified”.
        Console.WriteLine(s ?? “Unspecified”);


Posted: July 27, 2010 in C#, Globalization
Tags: ,

Saving date for specific culture …

System.IFormatProvider format = new System.Globalization.CultureInfo(“en-US”, true);
DateTime dt = DateTime.Parse(“Sun Feb 01 00:03:43 2009”, format);


DateTime dt = DateTime.ParseExact(“Fri Aug 01 09:03:43 2003”, “ddd MMM dd HH:mm:ss yyyy”, new System.Globalization.CultureInfo(“en-US”, true));

DateTime.Parse(“Sun Feb 01 00:03:43 2009”) will parse the date string based on the current user culture which will fail the conversion for some clutures. So it is good to save date with some specific culture.

ParseExact – parses to specified format.

Developers SDK

Posted: July 16, 2010 in C#, Dev Tools
Tags: ,

Facebook C# SDK –

Useful for integrating sign up, Address Book Importer, content sharing/sync, analytics … etc

Developer’s Blog:

Facebook – Source:

Hashcode in C#

Posted: June 3, 2010 in C#

Read about Hashcode here

From my colleagues, I found that – .net framework does not guarantee same hash function between different versions, this includes the different versions in place on 32 bit and 64 bit systems. Not only does it not guarantee that it will be the same, but it is quite different. For consistent hash codes, you cannot use the framework hash code function, so you have create a managed implementation of the 32/64 bit .net string hash function.

I will update here more when I come across more specific scenarios