Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
ref
keywordYou use the ref
keyword in the following contexts:
public void M(ref int refParameter)
{
refParameter += 42;
}
ref return
.public ref int RefMax(ref int left, ref int right)
{
if (left > right)
{
return ref left;
}
else
{
return ref right;
}
}
public void M2(int variable)
{
ref int aliasOfvariable = ref variable;
}
public ref int RefMaxConditions(ref int left, ref int right)
{
ref int returnValue = ref left > right ? ref left : ref right;
return ref returnValue;
}
struct
declaration, to declare a ref struct
. For more information, see the ref
structure types article.public ref struct CustomRef
{
public ReadOnlySpan<int> Inputs;
public ReadOnlySpan<int> Outputs;
}
ref struct
definition, to declare a ref
field. For more information, see the ref
fields section of the ref
structure types article.public ref struct RefFieldExample
{
private ref int number;
}
allows ref struct
types.class RefStructGeneric<T, S>
where T : allows ref struct
where S : T
{
// etc
}
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Please sign in to use this experience.
Sign in