Created
August 11, 2024 13:00
-
-
Save karenpayneoregon/bcfbf3873af0b54c5bd4eff19a0f0729 to your computer and use it in GitHub Desktop.
For article using GitHub Copilot in Visual Studio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var (friends, exception) = await FileOperations.ReadFriends("people.txt"); | |
| if (exception is null) | |
| { | |
| // read data | |
| } | |
| else | |
| { | |
| // handle exception | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class Friend | |
| { | |
| public string FirstName { get; set; } | |
| public string LastName { get; set; } | |
| public DateOnly BirthDate { get; set; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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