Archives by Tag 'C# Generic'
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 this method behavior could be [...]
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 = i.GetValueOrNull();
Console.Writeline( i.HasValue );
// output: false
