Archives by Tag 'C#'
C# To Upper Case First Letter of a String
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
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
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
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
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
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
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
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 = [...]