int index = myList.FindIndex(a => a.Prop == oProp);
If you don't want to use LINQ, then:
int index;
for (int i = 0; i < myList.Count; i++)
{
if (myList[i].Prop == oProp)
{
index = i;
break;
}
}
int index = myList.FindIndex(a => a.Prop == oProp);
If you don't want to use LINQ, then:
int index;
for (int i = 0; i < myList.Count; i++)
{
if (myList[i].Prop == oProp)
{
index = i;
break;
}
}
DateTime dt;
if (DateTime.TryParseExact(str, "M/d/yyyy", null, DateTimeStyles.None, out dt) == true)
{
//your date is valid and is in dt
}
1
2
3
4
5
6
7
| public static class Logger { public static void Log( string message) { File.AppendAllText( "C:\\LogFile.txt" , DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":\t" + message + Environment.NewLine); } } |
DateTime.Now.ToString("yyyyMMddHHmmssfff")
string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}",DateTime.Now)
;
There are following custom format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M) and z (time zone).
With Extension Method
Usage:
string result = "myfile.txt".AppendTimeStamp();
//test20160604234625642.txt
public static class MyExtensions
{
public static string AppendTimeStamp(this string fileName)
{
return string.Concat(
Path.GetFileNameWithoutExtension(fileName),
DateTime.Now.ToString("yyyyMMddHHmmssfff"),
Path.GetExtension(fileName)
);
}
}