{"id":18798686,"url":"https://github.com/liteobject/reference.semantics","last_synced_at":"2026-01-02T03:30:12.717Z","repository":{"id":90789139,"uuid":"552453622","full_name":"LiteObject/Reference.Semantics","owner":"LiteObject","description":null,"archived":false,"fork":false,"pushed_at":"2022-11-26T17:24:33.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-29T18:22:39.149Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/LiteObject.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":"2022-10-16T16:24:18.000Z","updated_at":"2022-11-26T16:02:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"e535c427-c464-4546-98ec-0fc67c8a264f","html_url":"https://github.com/LiteObject/Reference.Semantics","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2FReference.Semantics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2FReference.Semantics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2FReference.Semantics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiteObject%2FReference.Semantics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LiteObject","download_url":"https://codeload.github.com/LiteObject/Reference.Semantics/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239727055,"owners_count":19687099,"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":[],"created_at":"2024-11-07T22:12:39.138Z","updated_at":"2026-01-02T03:30:12.653Z","avatar_url":"https://github.com/LiteObject.png","language":"C#","readme":"# C# Reference Semantics with Value Types\nReference Semantics allow value types to be used like reference types.\n\n* `in` parameters\n  - The method doesn't modify the value of the argument used as this parameter.\n  - Unlike a `ref` or `out` parameter, you don't need to apply the `in` modifier at the call site.\n  - Use `in` parameters for large structs.  \n  - The `in` modifier can also be used with reference types or numeric values. However, the benefits in those cases are minimal, if any.\n* `ref` returns\n  - Returns a reference to value type, not a copy of the value\n  - Lifetime of the returned value must exceed the lifetime of the called method, e.g. a reference to a field or method argument, NOT a variable in the called method (also not allowed on `async` methods)\n  - Modifying this reference is the same as modifying the original value\n  - Add `ref` modifier to the method declaration return type, and to `return` statement\n* `ref` local  \n  - Assigning a `ref` return to a new variable will create a copy (The variable is a value type, not a reference!)\n  - A `ref` local is a variable that is a reference to a value tupe. Accessing the variable accesses the original value\n  - Use a `ref` local to store the `ref` return result\n  - Type inference with `var` will get the value type, not the `ref` modifier - requires `ref var` to work as expected\n* `ref readonly` returns\n  - Extends `ref` locals and returns\n  - Return a value type by reference, but caller is not allowed to modify  \n* `readonly` struct\n  - Indicates that a type is immutable.\n  - All field members must be read-only.\n  - All properties must be read-only, including auto-implemented properties.  \n* `ref struct`\n  - Use a `ref` struct or a `readonly ref` struct, such as `Span\u003cT\u003e` or `ReadOnlySpan\u003cT\u003e`, to work with blocks of memory as a sequence of bytes.\n--- \n## Pass By Reference VS Value\n\n| Pass By Reference | Pass By Value |\n|:---|:---|\n| A variable of a reference type is a refernce to the actual object on the heap | A variable for a value type is the value inself |\n| Passing a reference type to a method is just passing this reference | Passing a value type to a method _copies_ the value |\n| The caller and the called method see the same object on the heap |  |\n|  | Assigning a value type to a new variable also _copies_ the value |\n|  | Original value is unmodified |\n|  | (Copies aren't actually that expensive) |\n\n---\n## Using value types minimizes the number of allocation operations:\n*  Storage for value types is stack-allocated for local variables and method arguments.\n*  Storage for value types that are members of other objects is allocated as part of that object, not as a separate allocation.\n*  Storage for value type return values is stack allocated.\n\n## Contrast that with reference types in those same situations:\n* Storage for reference types is heap allocated for local variables and method arguments. The reference is stored on the stack.\n* Storage for reference types that are members of other objects are separately allocated on the heap. The containing object stores the reference.\n* Storage for reference type return values is heap allocated. The reference to that storage is stored on the stack.\n---\n## Links:\n* [Write safe and efficient C# code](https://learn.microsoft.com/en-us/dotnet/csharp/write-safe-efficient-code)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliteobject%2Freference.semantics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliteobject%2Freference.semantics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliteobject%2Freference.semantics/lists"}