{"id":17349072,"url":"https://github.com/arturwincenciak/ref-arg","last_synced_at":"2026-05-01T15:39:09.682Z","repository":{"id":85783130,"uuid":"581661776","full_name":"ArturWincenciak/Ref-Arg","owner":"ArturWincenciak","description":"Pass a value type variable by reference as a parameter of a method","archived":false,"fork":false,"pushed_at":"2022-12-26T01:06:25.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-02-24T10:50:23.786Z","etag":null,"topics":["csharp","csharp-code","dotnet","dotnet-core","internals"],"latest_commit_sha":null,"homepage":"https://teovincentblog.wordpress.com/2022/12/26/ref-returns-ref-locals-ref-arg/","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/ArturWincenciak.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":"2022-12-23T21:47:49.000Z","updated_at":"2022-12-26T10:45:39.000Z","dependencies_parsed_at":"2023-07-22T10:30:33.151Z","dependency_job_id":null,"html_url":"https://github.com/ArturWincenciak/Ref-Arg","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ArturWincenciak/Ref-Arg","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArturWincenciak%2FRef-Arg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArturWincenciak%2FRef-Arg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArturWincenciak%2FRef-Arg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArturWincenciak%2FRef-Arg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ArturWincenciak","download_url":"https://codeload.github.com/ArturWincenciak/Ref-Arg/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArturWincenciak%2FRef-Arg/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32503202,"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":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"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":["csharp","csharp-code","dotnet","dotnet-core","internals"],"created_at":"2024-10-15T16:54:35.405Z","updated_at":"2026-05-01T15:39:09.664Z","avatar_url":"https://github.com/ArturWincenciak.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ref Arg\n\n## Overview\n\nThis repository contains code demonstrating the difference between passing arguments by value and by reference in C#. The code includes examples of passing various types (e.g. int, struct, class) as arguments to methods, and modifying the values of the arguments within the methods.\n\nBlog post: [Ref Returns, Ref Locals, Ref Arg](https://teovincentblog.wordpress.com/2022/12/26/ref-returns-ref-locals-ref-arg)\n\n## Notes\nKeep in mind that passing arguments by reference can lead to more difficult to predict code behavior and more difficult debugging. Therefore, caution should be taken and the appropriateness of using reference arguments should be considered.\n\n## Drivers\nWhen writing highly performant applications, it is often beneficial to use value types such as structs instead of reference types like class. This is because structs are stored on the stack rather than the heap, which means the garbage collector has less work to do as it does not need to track and clean up unused structs.\n\nAnother technique to improve the performance of your program is to pass value types as reference arguments in methods. This avoids the overhead of copying the bytes of the value into the local scope of the method, especially when working with large value types or frequently calling the method.\n\n## To dive deeper\nSee the continuation on `ref loclas` and `ref returns` that repo: [Ref-Lokals-Ref-Returns](https://github.com/ArturWincenciak/Ref-Locals-Ref-Returns)\n\n## Output\nThe code will output the values of various variables before and after they are passed as arguments to methods. This will illustrate the difference between passing by value and by reference, as well as the impact of modifying the values of the arguments within the methods.\n\n```\nAlright ref!\n\nPassing an argument by reference.\n\n#01: globalInt = 100\n\nArgIntMethod #1: argInt = 100\nArgIntMethod #2: argInt = 200\n#02: globalInt = 100\n\nArgRefIntMethod #1: refArgInt = 100\nArgRefIntMethod #2: refArgInt = 200\n#03: globalInt = 200\n\n#04: globalStruct = [IntProp: 1000, StrProp: _str_in_struct_, ClassProp: [DoubleProp: 1000.1, StrProp: _str_in_class_, StructInClassProp: [FloatProp: 1000.2]], StructInStructProp: [FloatProp: 1000.3]]\n\nArgStructMethod: #1: argStruct = [IntProp: 1000, StrProp: _str_in_struct_, ClassProp: [DoubleProp: 1000.1, StrProp: _str_in_class_, StructInClassProp: [FloatProp: 1000.2]], StructInStructProp: [FloatProp: 1000.3]]\nArgStructMethod: #2: argStruct = [IntProp: 2000, StrProp: _str_in_struct_modified_, ClassProp: [DoubleProp: 2000.1, StrProp: _str_in_class_modified_, StructInClassProp: [FloatProp: 2000.2]], StructInStructProp: [FloatProp: 2000.3]]\n#05: globalStruct = [IntProp: 1000, StrProp: _str_in_struct_, ClassProp: [DoubleProp: 2000.1, StrProp: _str_in_class_modified_, StructInClassProp: [FloatProp: 2000.2]], StructInStructProp: [FloatProp: 1000.3]]\n\nArgRefStructMethod: #1: refStruct = [IntProp: 1000, StrProp: _str_in_struct_, ClassProp: [DoubleProp: 2000.1, StrProp: _str_in_class_modified_, StructInClassProp: [FloatProp: 2000.2]], StructInStructProp: [FloatProp: 1000.3]]\nArgRefStructMethod: #2: refStruct = [IntProp: 2000, StrProp: _str_in_struct_modified_, ClassProp: [DoubleProp: 3000.1, StrProp: _str_in_class_modified_modified_, StructInClassProp: [FloatProp: 3000.2]], StructInStructProp: [FloatProp: 2000.3]]\n#06: globalStruct = [IntProp: 2000, StrProp: _str_in_struct_modified_, ClassProp: [DoubleProp: 3000.1, StrProp: _str_in_class_modified_modified_, StructInClassProp: [FloatProp: 3000.2]], StructInStructProp: [FloatProp: 2000.3]]\n\n#07: globalClass = [DoubleProp: 10.1, StrProp: _str_, StructInClassProp: [FloatProp: 10.2]]\n\nEditPropsOfArgClassMethod: #1: argClass = [DoubleProp: 10.1, StrProp: _str_, StructInClassProp: [FloatProp: 10.2]]\nEditPropsOfArgClassMethod: #2: argClass = [DoubleProp: 20.1, StrProp: modified_, StructInClassProp: [FloatProp: 20.2]]\n#08: globalClass = [DoubleProp: 20.1, StrProp: modified_, StructInClassProp: [FloatProp: 20.2]]\n\nEditPropsOfArgRefClassMethod: #1: refClass = [DoubleProp: 20.1, StrProp: modified_, StructInClassProp: [FloatProp: 20.2]]\nEditPropsOfArgRefClassMethod: #2: refClass = [DoubleProp: 30.1, StrProp: modified_, StructInClassProp: [FloatProp: 30.2]]\n#09: globalClass = [DoubleProp: 30.1, StrProp: modified_, StructInClassProp: [FloatProp: 30.2]]\n\nCreateNewUsingArgClassMethod: #1: argClass = [DoubleProp: 30.1, StrProp: modified_, StructInClassProp: [FloatProp: 30.2]]\nCreateNewUsingArgClassMethod: #2: argClass = [DoubleProp: 123456789, StrProp: _the_new_one_without_ref_, StructInClassProp: [FloatProp: 123456790]]\n#10: globalClass = [DoubleProp: 30.1, StrProp: modified_, StructInClassProp: [FloatProp: 30.2]]\n\nCreateNewUsingArgRefClassMethod: #1: refClass = [DoubleProp: 30.1, StrProp: modified_, StructInClassProp: [FloatProp: 30.2]]\nCreateNewUsingArgRefClassMethod: #2: refClass = [DoubleProp: 987654321, StrProp: _the_new_one_by_ref_, StructInClassProp: [FloatProp: 987654340]]\n#11: globalClass = [DoubleProp: 987654321, StrProp: _the_new_one_by_ref_, StructInClassProp: [FloatProp: 987654340]]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farturwincenciak%2Fref-arg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farturwincenciak%2Fref-arg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farturwincenciak%2Fref-arg/lists"}