Archives by Tag 'C#'

C# To Upper Case First Letter of a String

By admin - Last updated: Thursday, October 2, 2008

Utilize a simple extension method to capitalize the first letter of a string. public class Program     {         static void Main(string[] args)         {             string s = “toUpperFirstLetter”;             // ToUpperFirstLetter is the extension method defined […]

Separator Delimited ToString for Array, List, Dictionary, Generic IEnumerable

By admin - Last updated: Sunday, September 21, 2008

Often programmers want to convert an array into one string with for example comma delimited. Traditionally, in C#, it can be achieved by int[] array = {1,2,3}; string delimited = string.Join(”,”, array); Console.WriteLine(delimited ) // output: "1,2,3"   However, string.Join does not apply to any generic IEnumerable object. It would be so much easier if […]

C# Pointer in unsafe context

By admin - Last updated: Saturday, September 20, 2008

Pointer usage in C# in unsafe context static unsafe void Main() {     int i;     int[] array = { 11, 22, 33 };     // pointer pi has the address of variable i     int* pi = &i;     i = 0;     // dereference the pi, i.e. […]

Get Current Directory while running as WindowsService

By admin - Last updated: Monday, September 8, 2008

During development, while you run the application as a console application or windows form application, often the code needs to load a specific file within the same folder of the exe assembly resides. The file can be located by using the file name directory since the default location will be the exe’s directory. For instance, […]

WCF BasicHttpBinding equivalent CustomBinding

By admin - Last updated: Wednesday, August 6, 2008

In real world , web service of WCF in basichttpbinding might not be flexible enough to meet enterprise application requirements. What programmers are looking at is a more robust way of developing web service application in WCF. CustomBinding as the name describes that it alllows users design their own web service binding. At the point, […]

C# XmlSerializer, Add an attribute to an array element

By admin - Last updated: Friday, June 27, 2008

In order words, add an attribute to an object element after xml serialization, If you want something like, <Rats count=“2”>   <Rat>little rat</Rat>   <Rat>old rat</Rat> </Rats> The C# code is [XmlType(“Rats”)]     public class Rats     {         [XmlAttribute(“count”)]         public int Count { get; set; […]

C# auto property

By admin - Last updated: Tuesday, March 25, 2008

ReSharper is good tool, it taught me the auto property in .NET 3.5 today. // traditional property declaration     public class Foo     {         private string _name;         public string Name         {             get { return _name; […]

C# Extension method, GetValueOrNull for value type object

By admin - Last updated: Wednesday, March 12, 2008

Return null if the value type object has default value. public static T? GetValueOrNull<t>( this T value ) where T : struct {    if( value.Equals( default( T ) ) )       return new Nullable<t>();    else       return value; } // Usage int i = default(int); //0 int? i = […]