I hope the .NET developers are enjoying with C# 3.0 lambda expressions. This is one of the easiest way for removing items from the generic list object based on a condition.

_sessionList.RemoveAll(x => x.MarkforRemoval == true);

The above code removes all the items marked for removal. This also avoid the error “Collection was modified; enumeration operation may not execute” if you are checking the conditions in the foreach loop and removing it.

Quick snippet, hopefully useful.


 
Categories: .NET

It is very easy to format the datetime to string in .NET and this is commonly used by the developers while displaying the date & time in desired format. But if you would like to store the formatted value in datetime variable, the value changes according to the .NET default format.

Default datetime format in .NET is

image

We will try this to format the value,

image

When we try to assign the formatted value to a datetime variable the format again changed to default format

image

To avoid this, use DateTime.ParseExact

DateTime dt = DateTime.ParseExact(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);

Where is this useful?
If you develop client server application the .NET stores the datetime value with timezone. The above syntax is timezone free, which will be useful across the application.

Also refer this link for DateTime best practices http://msdn.microsoft.com/en-us/library/ms973825.aspx


 
Categories: .NET