{"id":25979238,"url":"https://github.com/bjorg/nullablebyexample","last_synced_at":"2026-04-17T03:33:31.164Z","repository":{"id":66875530,"uuid":"245482546","full_name":"bjorg/NullableByExample","owner":"bjorg","description":"Samples showing how null-aware coding works in C#","archived":false,"fork":false,"pushed_at":"2020-03-19T06:17:53.000Z","size":13,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-11T19:06:21.188Z","etag":null,"topics":[],"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/bjorg.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":"2020-03-06T17:43:26.000Z","updated_at":"2022-02-28T02:25:52.000Z","dependencies_parsed_at":"2023-03-11T00:24:49.649Z","dependency_job_id":null,"html_url":"https://github.com/bjorg/NullableByExample","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bjorg/NullableByExample","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjorg%2FNullableByExample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjorg%2FNullableByExample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjorg%2FNullableByExample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjorg%2FNullableByExample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bjorg","download_url":"https://codeload.github.com/bjorg/NullableByExample/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjorg%2FNullableByExample/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31913652,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T18:22:33.417Z","status":"online","status_checked_at":"2026-04-17T02:00:06.879Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2025-03-05T06:28:58.963Z","updated_at":"2026-04-17T03:33:31.157Z","avatar_url":"https://github.com/bjorg.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nullable by Example\n\n## Getting Started with Null-Aware Code\n1. Enable nullable types for the entire project by adding the `\u003cNullable\u003eenable\u003c/Nullable\u003e` element to your _.csproj_ files.\n1. Add the `#nullable disable` pragma to every source file you want to remain null-unaware.\n1. As you work on each file, remove the `#nullable` pragma and address any warnings.\n\n## Null-Aware Code Exercises\nThis repository contains two exercise files with their corresponding solutions.\n* [Exercise_Methods.cs](Exercise_Methods.cs) focuses on nullable annotations for methods. This exercise covers the nullable type suffix `?`, as well as the following attributes: [`NotNull`](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.notnullattribute?view=netcore-3.1), [`NotNullWhen`](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.notnullwhenattribute?view=netcore-3.1), and [`NotNullIfNotNull`](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.notnullifnotnullattribute?view=netcore-3.1).\n* [Exercise_Properties.cs](Exercise_Properties.cs) focuses on nullable annotations for properties. This exercise covers the following attributes: [`AllowNull`](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.allownullattribute?view=netcore-3.1) and [`DisallowNull`](https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.disallownullattribute?view=netcore-3.1).\n\n## Mixing Null-Aware and Null-Unaware Code\nMixing null-aware and null-unaware code has some unfortunate, but understandable, negative side-effects. To demonstrate, this  repository contains two classes which call each other. The first class is null-aware ([`Mixed_NullAware.cs`](Mixed_NullAware.cs)) and the second class is not ([`Mixed_NullUnaware.cs`](Mixed_NullUnaware.cs)). Neither code shows any warnings. The null-unaware code does not provide sufficient information to null-aware code to make a determination that a null exception could occur. And, of course, the null-unaware code doesn't take advantage of the null annotations in the null-aware code.\n\nThe short of it, is that as projects are converted from null-unaware to null-aware, code that were previously warning-free and null-aware may start to show new warnings, because the compiler is supplied new information with each converted class.\n\n## Personal Anecdotes\n* In [LambdaSharp](https://github.com/LambdaSharp/LambdaSharpTool) parser, helper functions return null when something cannot be parsed and emit an error. In one case, I was checking if the parsed result did not match the expected type and would emit an error, forgetting that `null` results also emitted an error message. After making the parser code null-aware, my mistake became apparent and was quickly fixed.\n* Consider doing `x.Foo ?? throw new InvalidOperationException()` instead of this `x.Foo!`, because `NullReferenceException` can happen anywhere, which can make it hard to track down exactly which expression is the culprit.\n* Nullable annotations have proven very useful to validate expectations, which may be different from the time a type is declared to when it is used.\n* Note that a null-aware file will trust the type annotations of a non-aware file. This means that as you convert files to become null-aware, warning may pop-up in files you already converted. Optimally, you should try to convert files from least dependent to most dependent (similar to a build system).\n\n## Additional Reading\n* [Tutorial: Express your design intent more clearly with nullable and non-nullable reference types](https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/nullable-reference-types)\n* [Tutorial: Migrate existing code with nullable reference types](https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/upgrade-to-nullable-references)\n* [Update libraries to use nullable reference types and communicate nullable rules to callers](https://docs.microsoft.com/en-us/dotnet/csharp/nullable-attributes)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbjorg%2Fnullablebyexample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbjorg%2Fnullablebyexample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbjorg%2Fnullablebyexample/lists"}