C# .NET - Coding tips - Asked By Leonie Kruger on 22-Jul-13 03:47 PM


I am new to C# and would appreciate some coding tips on the following program. I created a csv file called nameSurname.csv. The first column in my file is the name and second surname. I would like to do the following:
- display the name and surname on Console (without commas)
-write a third column to my csv called nameSurname which concatenated name and surname.

How can I improve on my coding? Is there any standards that I do not adhere to?

Thanks in advance.















static void Main()


int count;

count = 0;

// itterate through lines in file to create a concatenate a new string

string[] nameSurnameLine;//concatenated string of name and surname

string[] fileLines = File.ReadAllLines(@"C:\Users\LVK\Desktop\NameSurname.csv");

nameSurnameLine =

new string[fileLines.Length];

foreach (string value in fileLines)


string newLine;

int pos;

int start = 0;

newLine =

' '.ToString();


StringBuilder nameSurname = new System.Text.StringBuilder();

for (pos = 0; pos < value.Length; pos++)


if (value[pos] == ',')


// create a new line to write to the file. This line will constist out of old values

//and the new concatenated string

Console.Write(value.Substring(start, pos));

Console.Write(" ");

newLine = value.Substring(start, pos);

nameSurname.Append(value.Substring(start, pos));

start = pos + 1;


if ((pos==value.Length-1)& (start !=0) )


//if you are at the end of the line, add the value column to the new line and lastly add the

//concatenated string

newLine +=


Console.Write(value.Substring(start, pos-start+1));

newLine += value.Substring(start, pos - start + 1);

newLine +=


nameSurname.Append(value.Substring(start, pos - start + 1));

newLine += nameSurname.ToString();

nameSurnameLine[count] = newLine;






File.WriteAllLines(@"C:\Users\LVK\Desktop\NameSurname.csv", nameSurnameLine);







Robbe Morris replied to Leonie Kruger on 22-Jul-13 05:19 PM
var mystring = "robbe,is,a,super,neat,guy";
var pieces = mystring.Split(",".ToCharArray());

for(int i=0;i<pieces.Length;i++)
Leonie Kruger replied to Robbe Morris on 23-Jul-13 02:27 AM
Thanks so much Robbe. I would like to display the line as it is, just without the ','. The example you gave me displays each word in a different line. I could use the write function, but then there will be no spaces in between the words. Please see below if it is the best way to add the space. Is there maybe a simpler way?

Console.Write(pieces[i] + ' ')
Robbe Morris replied to Leonie Kruger on 23-Jul-13 09:13 AM
That's fine.  I was just showing you to use the Split function to convert your comma delimited string to words versus parsing each position.
Leonie Kruger replied to Robbe Morris on 23-Jul-13 12:02 PM
Thank you :-)