I’m a beginner c# programmer and I’m curious which of the variants from bellow it’s better from the point of view of the compiler and so on.
Variant 1
string file = "20071201.22002300.wmv"; //name of the file is YYYYMMDD.HHMMHHMM.wmv
string[] tmp= file.Split('.');
DateTime startDate= new DateTime(Convert.ToInt32(tmp[0].Substring(0, 4)),
Convert.ToInt32(tmp[0].Substring(4, 2)),
Convert.ToInt32(tmp[0].Substring(6, 2)),
Convert.ToInt32(tmp[1].Substring(0, 2)),
Convert.ToInt32(tmp[1].Substring(2, 2)), 0);
DateTime endDate =new DateTime(Convert.ToInt32(tmp[0].Substring(0, 4)),
Convert.ToInt32(tmp[0].Substring(4, 2)),
Convert.ToInt32(tmp[0].Substring(6, 2)),
Convert.ToInt32(tmp[1].Substring(4, 2)),
Convert.ToInt32(tmp[1].Substring(6, 2)), 0);
Variant 2
string file = "20071201.22002300.wmv"; //name of the file is YYYYMMDD.HHMMHHMM.wmv
string[] tmp= file.Split('.');
int year = Convert.ToInt32(tmp[0].Substring(0, 4));
int month= Convert.ToInt32(tmp[0].Substring(4, 2));
int day = Convert.ToInt32(tmp[0].Substring(6, 2));
int hour = Convert.ToInt32(tmp[1].Substring(0, 2));
int minute = Convert.ToInt32(tmp[1].Substring(2, 2));
int endHour= Convert.ToInt32(tmp[1].Substring(4, 2));
int endMinute = Convert.ToInt32(tmp[1].Substring(6, 2));
DateTime startDate= new DateTime(year, month, day, hour, minute, 0);
DateTime startDate= new DateTime(year, month, day, endHour, endMinute, 0);
Generally i prefer to work as in first variant because i’m not working as a programmer so i don’t work together with other programmers, I’m just creating tools to help in my job , and also i consider that the first variant it’s easier for the compiler.
Please correct if i’m wrong and thanks in advance
I prefer the second because it is obvious what it is doing.
Also, you are not calling
Convert.ToInt32(tmp[0].Substring(0, 4))etc. multiple times which you are doing in the first example.Update
Even better, use something like this: