Skip to content

Added section on Out and Ref parameters #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
_site/
.sass-cache/
.jekyll-metadata

# Visual Studio cache/options directory
.vs/
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,33 @@ someobject.SomeGenericMethod[int](10)
someobject.SomeGenericMethod[str]("10")
```

## Out and Ref parameters

When a managed method has `out` or `ref` parameters, the arguments appear as
normal arguments in Python, but the return value of the method is modified.
There are 3 cases:

1. If the method is `void` and has one `out` or `ref` parameter, the method returns
the value of that parameter to Python. For example, if `someobject` has
a managed method with signature `void SomeMethod1(out arg)`, it is called like so:
```
new_arg = someobject.SomeMethod1(arg)
```
where the value of `arg` is ignored, but its type is used for overload resolution.

2. If the method is `void` and has multiple `out`/`ref` parameters, the method returns
a tuple containing the `out`/`ref` parameter values. For example, if `someobject` has
a managed method with signature `void SomeMethod2(out arg, ref arg2)`, it is called like so:
```
new_arg, new_arg2 = someobject.SomeMethod2(arg, arg2)
```

3. Otherwise, the method returns a tuple containing the return value followed by the
`out`/`ref` parameter values. For example:
```
found, new_value = dictionary.TryGetValue(key, value)
```

## Delegates And Events

Delegates defined in managed code can be implemented in Python.
Expand All @@ -274,6 +301,9 @@ d = AssemblyLoadEventHandler(my_handler)
AppDomain.CurrentDomain.AssemblyLoad += d
```

Delegates with `out` or `ref` parameters can be implemented in Python by
following the convention described in [Out and Ref parameters].

Multicast delegates can be implemented by adding more callable objects to
a delegate instance:

Expand Down