{"id":22065758,"url":"https://github.com/karenpayneoregon/indexes-ranges-csharp","last_synced_at":"2026-05-01T01:32:21.456Z","repository":{"id":110840598,"uuid":"365905962","full_name":"karenpayneoregon/indexes-ranges-csharp","owner":"karenpayneoregon","description":"C# 8 indexes and ranges code samples","archived":false,"fork":false,"pushed_at":"2021-06-06T19:39:30.000Z","size":132,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T18:17:23.519Z","etag":null,"topics":["csharp","indexes","ranges","unit-testing","visual-studio"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/karenpayneoregon.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-10T03:23:11.000Z","updated_at":"2021-06-06T19:39:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"dc268e21-7d57-4e3a-b430-7ead9a7d0a04","html_url":"https://github.com/karenpayneoregon/indexes-ranges-csharp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/karenpayneoregon/indexes-ranges-csharp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Findexes-ranges-csharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Findexes-ranges-csharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Findexes-ranges-csharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Findexes-ranges-csharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/karenpayneoregon","download_url":"https://codeload.github.com/karenpayneoregon/indexes-ranges-csharp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2Findexes-ranges-csharp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32482460,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"ssl_error","status_checked_at":"2026-04-30T13:12:06.837Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","indexes","ranges","unit-testing","visual-studio"],"created_at":"2024-11-30T19:21:48.027Z","updated_at":"2026-05-01T01:32:21.421Z","avatar_url":"https://github.com/karenpayneoregon.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C# 8 Ranges and Indices in detail\n\n![img](RangeUnitTest/assets/unitTesting.png) ![img](RangeUnitTest/assets/Versions.png)\n\n---\n\n![img](RangeUnitTest/assets/StartEndHat.png)\n\nLearn how to work with C# 8 Range and Indexes going beyond the code samples found done by Microsoft and other entities on the web.\n\nThere are code samples that work with\n\n- Strings\n- Dates\n- Numerics\n- Classes/list\n\nEach code sample are done in unit test methods where the category/trait for each are marked with `[TestTraits(Trait.xxxx)]` where xxxx has been defined in the following `enum`.\n\n```csharp\npublic enum Trait\n{\n    PlaceHolder,\n    RangesIndices,\n    CreateTextFile,\n    Contacts,\n    Cities,\n    Strings,\n    Dates,\n    Numbers\n}\n```\n\n# Documentation\n\n- Each unit test describes it's intent and verified with assertions.\n- Some test use [IEqualityComparer](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iequalitycomparer-1?view=net-5.0)\u0026lt;T\u0026gt; to compare actual results with expected results as shown below.\n\n```csharp\nusing System;\nusing System.Collections.Generic;\n\nnamespace RangeUnitTest.Classes\n{\n    public class ContactIdFirstNameLastNameEqualityComparer : IEqualityComparer\u003cContacts\u003e\n    {\n        public bool Equals(Contacts x, Contacts y)\n        {\n            if (ReferenceEquals(x, y)) return true;\n            if (ReferenceEquals(x, null)) return false;\n            if (ReferenceEquals(y, null)) return false;\n            if (x.GetType() != y.GetType()) return false;\n            return x.ContactId == y.ContactId \u0026\u0026 x.FirstName == y.FirstName \u0026\u0026 x.LastName == y.LastName;\n        }\n\n        public int GetHashCode(Contacts obj)\n        {\n            return HashCode.Combine(obj.ContactId, obj.FirstName, obj.LastName);\n        }\n    }\n}\n```\n\n\u003c/br\u003e\n\n# Data sources\n\nFor many of the code samples, information is read from text files\n\n- Using json\n\n``` csharp\nusing System;\nusing System.Collections.Generic;\nusing System.IO;\n\nnamespace RangeUnitTest.Classes\n{\n    public class MockedData\n    {\n        public static string ContactFileName { get; } = \"contacts.json\";\n\n        public static List\u003cContacts\u003e ReadContacts()\n        {\n            try\n            {\n                var json = File.ReadAllText(ContactFileName);\n                return JSonHelper.ConvertJSonToObject\u003cList\u003cContacts\u003e\u003e(json);\n            }\n            catch (Exception)\n            {\n                return null;\n            }\n\n        }\n    }\n\n}\n```\n\n- Reading plain text\n\n```csharp\nusing System.IO;\nusing System.Linq;\n\nnamespace RangeUnitTest.Classes\n{\n    public class FileOperations\n    {\n        public static string[] OregonCities() =\u003e File.ReadAllLines(\"OregonCityNames.txt\");\n        public static string[] OregonCitiesFirstTen() =\u003e OregonCities().Take(10).ToArray();\n    }\n}\n```\n\n# Base classes/Interfaces\n\nTo keep test methods clean, base classes, partial classes and Interfaces are used.\n\n![img](RangeUnitTest/assets/base.png)\n\n# Visualization\n\nTo get a visual representation of indices on a list see the files under the `Dump` folder in tangent with various test methods.\n\n\n\n\n\n\n# Example test method\n\n```csharp\n[TestMethod]\n[TestTraits(Trait.Contacts)]\npublic void BetweenContacts_1()\n{\n    /*\n     * First contact for between\n     */\n    var firstContact = new ContactName() { FirstName = \"Fr�d�rique\", LastName = \"Citeaux\" };\n\n    /*\n     * Last contact for between\n     */\n    var lastContact = new ContactName() { FirstName = \"Elizabeth\", LastName = \"Brown\" };\n\n    var contactsArray = MockedData.ReadContacts().ToArray();\n    var contacts = contactsArray.ContactsListIndices();\n\n    var (startIndex, endIndex) = contacts.BetweenContacts(firstContact, lastContact);\n\n    var contactsBetweenTwo = contactsArray[startIndex..endIndex];\n    \n    Assert.IsTrue(\n        contactsBetweenTwo\n            .SequenceEqual(\n                ExpectedContacts, \n                    new ContactIdFirstNameLastNameEqualityComparer()));\n\n}\n```\n\n---\n\n\n**From Microsoft**\n\nRanges and indices provide a succinct syntax for accessing single elements or ranges in a sequence.\n\nAn index expression typically returns the type of the elements of a sequence. A range expression typically returns the same sequence type as the source sequence.\n\n![img](RangeUnitTest/assets/contacts.png)\n\n\n\n\n# Microsoft docs\n\n[Explore indexs and ranges](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/ranges-indexes)\n\n# See also\n\n\n[Visual Studio: Different methods to display unit test](https://social.technet.microsoft.com/wiki/contents/articles/51303.visual-studio-different-methods-to-display-unit-test.aspx)\n\n[Get started with Live Unit Testing](https://docs.microsoft.com/en-us/visualstudio/test/live-unit-testing-start?tabs=csharp\u0026view=vs-2019)\n\n[Original GitHub page](https://github.com/karenpayneoregon/karenpayneoregon.github.io/blob/master/csharp/indices-ranges.md)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarenpayneoregon%2Findexes-ranges-csharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarenpayneoregon%2Findexes-ranges-csharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarenpayneoregon%2Findexes-ranges-csharp/lists"}