Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created August 11, 2024 13:00
Show Gist options
  • Select an option

  • Save karenpayneoregon/bcfbf3873af0b54c5bd4eff19a0f0729 to your computer and use it in GitHub Desktop.

Select an option

Save karenpayneoregon/bcfbf3873af0b54c5bd4eff19a0f0729 to your computer and use it in GitHub Desktop.
For article using GitHub Copilot in Visual Studio
var (friends, exception) = await FileOperations.ReadFriends("people.txt");
if (exception is null)
{
// read data
}
else
{
// handle exception
}
internal class FileOperations
{
public static async Task<(List<Friend> data, Exception exception)> ReadFriends(string filePath)
{
List<Friend> friends = new List<Friend>();
try
{
string[] lines = (await File.ReadAllLinesAsync(filePath))
.Where(line => !string.IsNullOrWhiteSpace(line))
.ToArray();
for (int index = 0; index < lines.Length; index += 4)
{
string firstName = lines[index].Trim();
string lastName = lines[index + 1].Trim();
DateOnly birthDate = DateOnly.FromDateTime(DateTime.Parse(lines[index + 2].Trim()));
Friend friend = new Friend
{
FirstName = firstName,
LastName = lastName,
BirthDate = birthDate
};
friends.Add(friend);
}
return (friends, null);
}
catch (Exception ex)
{
return (null, ex);
}
}
}
internal class Friend
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateOnly BirthDate { get; set; }
}
Karen
Payne
09/24/1956
------------------------------------
Jim
Beam
08/23/1923
------------------------------------
Anne
Jones
12/14/1987
------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment