{"id":26249749,"url":"https://github.com/noncommunicado/kutcode.optionality","last_synced_at":"2026-01-22T23:50:33.321Z","repository":{"id":198207200,"uuid":"700038594","full_name":"noncommunicado/KutCode.Optionality","owner":"noncommunicado","description":"Optional object type","archived":false,"fork":false,"pushed_at":"2025-04-02T08:19:37.000Z","size":105,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-24T02:07:25.318Z","etag":null,"topics":["csharp","dotnet","dotnetcore","optional","optional-type","patterns"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/KutCode.Optionality/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/noncommunicado.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2023-10-03T20:31:50.000Z","updated_at":"2025-04-02T08:19:45.000Z","dependencies_parsed_at":"2024-10-30T13:32:43.217Z","dependency_job_id":null,"html_url":"https://github.com/noncommunicado/KutCode.Optionality","commit_stats":null,"previous_names":["hamaronooo/kutcode.optional","hamaronooo/kutcode.optionality","noncommunicado/kutcode.optionality"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noncommunicado%2FKutCode.Optionality","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noncommunicado%2FKutCode.Optionality/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noncommunicado%2FKutCode.Optionality/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noncommunicado%2FKutCode.Optionality/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/noncommunicado","download_url":"https://codeload.github.com/noncommunicado/KutCode.Optionality/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250546080,"owners_count":21448260,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","dotnet","dotnetcore","optional","optional-type","patterns"],"created_at":"2025-03-13T15:49:29.534Z","updated_at":"2026-01-22T23:50:33.287Z","avatar_url":"https://github.com/noncommunicado.png","language":"C#","readme":"# \u003cimg src=\"./icon/icon.png\" style=\"width: 30px\" /\u003e KutCode.Optionality\n\n.NET library that provides a type-safe way to handle nullable values in C#. It helps prevent null reference exceptions and makes null handling more explicit and safer.\n\n## ⚙️ Features\n\n- 🛡️ Type-safe handling of nullable values for both reference and value types\n- 🎯 Two main types: `Optional\u003cT\u003e` for reference types and `OptionalValue\u003cT\u003e` for value types\n- 🔄 Built-in JSON serialization support using `System.Text.Json`\n- 🎨 Clean and fluent API for working with optional values\n- 🚀 Zero dependencies on external packages\n\n## 📦 Installation\n\nKutCode.Optionality is designed to work with `net7.0` and higher.\n\nInstall via NuGet Package Manager:\n\n```powershell\nInstall-Package KutCode.Optionality\n```\n\nOr via the .NET CLI:\n\n```shell\ndotnet add package KutCode.Optionality\n```\n\nAll versions can be found [here](https://www.nuget.org/packages/KutCode.Optional.Core/).\n\n## 🚀 Basic Usage\n\n### Creating Optional Values\n\n```csharp\n// For reference types\nOptional\u003cPerson\u003e person = Optional.From(new Person());\nOptional\u003cPerson\u003e emptyPerson = Optional.None\u003cPerson\u003e();\n\n// For value types\nOptionalValue\u003cint\u003e number = new OptionalValue\u003cint\u003e(42);\nOptionalValue\u003cint\u003e emptyNumber = OptionalValue\u003cint\u003e.None;\n```\n\n### Checking for Values\n\n```csharp\nvar person = GetPerson();\nif (person.HasValue)\n{\n    Console.WriteLine(person.Value.Name);\n}\nelse\n{\n    Console.WriteLine(\"Person is not found\");\n}\n```\n\n### Functional Approach with IfHasValue and IfEmpty\n\n```csharp\nvar person = GetPerson();\n\n// Execute code only if value exists\nperson.IfHasValue(p =\u003e Console.WriteLine(p.Name));\n\n// Execute code only if value is missing\nperson.IfEmpty(() =\u003e Console.WriteLine(\"Person not found\"));\n\n// Chain methods together\nperson\n    .IfHasValue(p =\u003e ProcessPerson(p))\n    .IfEmpty(() =\u003e LogMissingPerson());\n```\n\n### Pattern Matching with TryGetValue\n\n```csharp\n// Using pattern matching style with out parameter\nif (person.TryGetValue(out var p))\n{\n    Console.WriteLine(p.Name);\n}\n```\n\n### Safe Value Access with Fallback\n\n```csharp\nvar person = GetPerson();\n\n// Get value or fallback to default\nvar name = person.Fallback(new Person { Name = \"Unknown\" }).Name;\n\n// Try multiple fallback values\nvar personWithFallback = person.FallbackCoalesce(\n    GetPersonFromCache(),\n    GetPersonFromBackup(),\n    new Person { Name = \"Unknown\" }\n);\n```\n\n### Implicit Conversions\n\n```csharp\nOptional\u003cPerson\u003e person = Optional.From(new Person());\nHandlePerson(person); // Implicitly converts to Person\n\nvoid HandlePerson(Person person)\n{\n    // Work with non-null person\n}\n```\n\n## 🔄 LINQ Extensions\n\nThe library provides LINQ extension methods for working with collections of Optional objects:\n\n```csharp\n// Filter out None values and get the actual values\nvar people = GetPeople(); // Returns IEnumerable\u003cOptional\u003cPerson\u003e\u003e\nvar validPeople = people.WhereHasValue(); // Returns IEnumerable\u003cPerson\u003e\n\n// Transform values that exist\nvar names = people.SelectIfHasValue(p =\u003e p.Name); // Returns IEnumerable\u003cstring\u003e\n\n// Works with OptionalValue too\nvar ages = GetAges(); // Returns IEnumerable\u003cOptionalValue\u003cint\u003e\u003e\nvar validAges = ages.WhereHasValue(); // Returns IEnumerable\u003cint\u003e\n```\n\n## 📝 JSON Serialization\n\nThe library provides built-in JSON serialization support:\n\n```csharp\npublic class Person\n{\n    public string Name { get; init; }\n    public Optional\u003cint\u003e Age { get; init; }\n    public OptionalValue\u003cDateTime\u003e BirthDate { get; init; }\n}\n\nvar person = new Person \n{\n    Name = \"John\",\n    Age = Optional.From(30),\n    BirthDate = new OptionalValue\u003cDateTime\u003e(new DateTime(1990, 1, 1))\n};\n\nvar json = JsonSerializer.Serialize(person);\n// Result: {\"Name\":\"John\",\"Age\":30,\"BirthDate\":\"1990-01-01T00:00:00\"}\n```\n\n### Null Handling in JSON\n\n```csharp\nvar person = new Person \n{\n    Name = \"John\",\n    Age = Optional.None\u003cint\u003e(),\n    BirthDate = OptionalValue\u003cDateTime\u003e.None\n};\n\nvar json = JsonSerializer.Serialize(person);\n// Result: {\"Name\":\"John\",\"Age\":null,\"BirthDate\":null}\n```\n\n## ⚠️ Best Practices\n\n1. Use `Optional\u003cT\u003e` for reference types and `OptionalValue\u003cT\u003e` for value types\n2. Prefer `HasValue` checks before accessing `Value`\n3. Use `TryGetValue` for pattern matching style access\n4. Use `IfHasValue` and `IfEmpty` for functional programming style\n5. Use `WhereHasValue` and `SelectIfHasValue` for LINQ operations\n6. Use `Fallback` and `FallbackCoalesce` for safe value access with defaults\n7. Use JSON serialization attributes when needed:\n   ```csharp\n   [JsonConverter(typeof(OptionalJsonConverterFactory))]\n   public Optional\u003cPerson\u003e Person { get; init; }\n   ```\n\n## 🤝 Contributing\n\nFeel free to contribute to this project by:\n- 🐛 Reporting bugs\n- 💡 Suggesting features\n- 🔧 Submitting pull requests\n\n## 📞 Support\n\nFor questions and support:\n- 📱 [Telegram](https://t.me/noncommunicado)\n- 📧 Create an issue in the repository\n  \nIf you wanna to buy me a coffee 😃, I will be grateful for any tokens in TON network:  \n💎 `noncommunicado.ton`  \n💎 `UQD0zFgp0p-eFnbL4cPA6DYqoeWzGbCA81KuU6BKwdFmf8jv`","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoncommunicado%2Fkutcode.optionality","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnoncommunicado%2Fkutcode.optionality","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoncommunicado%2Fkutcode.optionality/lists"}