C#idiomatic C#
refcsharp/language-reference/keywords

Language

The `ref` keyword

Understand the different uses for the `ref` keyword and get more information on those uses

  • page csharp/language-reference/keywords/ref
  • updated 01/22/2026

You use the ref keyword in the following contexts:

  • In a method signature and in a method call, to pass an argument to a method by reference.

    sample · csharpdocs/csharp/language-reference/keywords/snippets/refKeyword.csPassByReference

    csharp
    public class RefKeywordExamples
    {
        // <PassByReference>
        public void M(ref int refParameter)
        {
            refParameter += 42;
        }
        // </PassByReference>
    
        // <ReturnByReference>
        public ref int RefMax(ref int left, ref int right)
        {
            if (left > right)
            {
                return ref left;
            }
            else
            {
                return ref right;
            }
        }
        // </ReturnByReference>
    
        // <LocalRef>
        public void M2(int variable)
        {
            ref int aliasOfvariable = ref variable;
        }
        // </LocalRef> 
    
        // <ConditionalRef>
        public ref int RefMaxConditions(ref int left, ref int right)
        {
            ref int returnValue = ref left > right ? ref left : ref right;
            return ref returnValue;
        }
        // </ConditionalRef>
    }
    
    // <SnippetRefStruct>
    public ref struct CustomRef
    {
        public ReadOnlySpan<int> Inputs;
        public ReadOnlySpan<int> Outputs;
    }
    // </SnippetRefStruct>
    
    // <SnippetRefField>
    public ref struct RefFieldExample
    {
        private ref int number;
    }
    // </SnippetRefField>
    
    // <SnippetRefGeneric>
    class RefStructGeneric<T, S>
        where T : allows ref struct
        where S : T
    {
        // etc
    }
    // </SnippetRefGeneric>
  • In a method signature, to return a value to the caller by reference. For more information, see ref return.

    sample · csharpdocs/csharp/language-reference/keywords/snippets/refKeyword.csReturnByReference

    csharp
    public class RefKeywordExamples
    {
        // <PassByReference>
        public void M(ref int refParameter)
        {
            refParameter += 42;
        }
        // </PassByReference>
    
        // <ReturnByReference>
        public ref int RefMax(ref int left, ref int right)
        {
            if (left > right)
            {
                return ref left;
            }
            else
            {
                return ref right;
            }
        }
        // </ReturnByReference>
    
        // <LocalRef>
        public void M2(int variable)
        {
            ref int aliasOfvariable = ref variable;
        }
        // </LocalRef> 
    
        // <ConditionalRef>
        public ref int RefMaxConditions(ref int left, ref int right)
        {
            ref int returnValue = ref left > right ? ref left : ref right;
            return ref returnValue;
        }
        // </ConditionalRef>
    }
    
    // <SnippetRefStruct>
    public ref struct CustomRef
    {
        public ReadOnlySpan<int> Inputs;
        public ReadOnlySpan<int> Outputs;
    }
    // </SnippetRefStruct>
    
    // <SnippetRefField>
    public ref struct RefFieldExample
    {
        private ref int number;
    }
    // </SnippetRefField>
    
    // <SnippetRefGeneric>
    class RefStructGeneric<T, S>
        where T : allows ref struct
        where S : T
    {
        // etc
    }
    // </SnippetRefGeneric>
  • In a declaration of a local variable, to declare a reference variable.

    sample · csharpdocs/csharp/language-reference/keywords/snippets/refKeyword.csLocalRef

    csharp
    public class RefKeywordExamples
    {
        // <PassByReference>
        public void M(ref int refParameter)
        {
            refParameter += 42;
        }
        // </PassByReference>
    
        // <ReturnByReference>
        public ref int RefMax(ref int left, ref int right)
        {
            if (left > right)
            {
                return ref left;
            }
            else
            {
                return ref right;
            }
        }
        // </ReturnByReference>
    
        // <LocalRef>
        public void M2(int variable)
        {
            ref int aliasOfvariable = ref variable;
        }
        // </LocalRef> 
    
        // <ConditionalRef>
        public ref int RefMaxConditions(ref int left, ref int right)
        {
            ref int returnValue = ref left > right ? ref left : ref right;
            return ref returnValue;
        }
        // </ConditionalRef>
    }
    
    // <SnippetRefStruct>
    public ref struct CustomRef
    {
        public ReadOnlySpan<int> Inputs;
        public ReadOnlySpan<int> Outputs;
    }
    // </SnippetRefStruct>
    
    // <SnippetRefField>
    public ref struct RefFieldExample
    {
        private ref int number;
    }
    // </SnippetRefField>
    
    // <SnippetRefGeneric>
    class RefStructGeneric<T, S>
        where T : allows ref struct
        where S : T
    {
        // etc
    }
    // </SnippetRefGeneric>
  • As the part of a conditional ref expression or a ref assignment operator.

    sample · csharpdocs/csharp/language-reference/keywords/snippets/refKeyword.csConditionalRef

    csharp
    public class RefKeywordExamples
    {
        // <PassByReference>
        public void M(ref int refParameter)
        {
            refParameter += 42;
        }
        // </PassByReference>
    
        // <ReturnByReference>
        public ref int RefMax(ref int left, ref int right)
        {
            if (left > right)
            {
                return ref left;
            }
            else
            {
                return ref right;
            }
        }
        // </ReturnByReference>
    
        // <LocalRef>
        public void M2(int variable)
        {
            ref int aliasOfvariable = ref variable;
        }
        // </LocalRef> 
    
        // <ConditionalRef>
        public ref int RefMaxConditions(ref int left, ref int right)
        {
            ref int returnValue = ref left > right ? ref left : ref right;
            return ref returnValue;
        }
        // </ConditionalRef>
    }
    
    // <SnippetRefStruct>
    public ref struct CustomRef
    {
        public ReadOnlySpan<int> Inputs;
        public ReadOnlySpan<int> Outputs;
    }
    // </SnippetRefStruct>
    
    // <SnippetRefField>
    public ref struct RefFieldExample
    {
        private ref int number;
    }
    // </SnippetRefField>
    
    // <SnippetRefGeneric>
    class RefStructGeneric<T, S>
        where T : allows ref struct
        where S : T
    {
        // etc
    }
    // </SnippetRefGeneric>
  • In a struct declaration, to declare a ref struct. For more information, see the ref structure types article.

    sample · csharpdocs/csharp/language-reference/keywords/snippets/refKeyword.csSnippetRefStruct

    csharp
    public class RefKeywordExamples
    {
        // <PassByReference>
        public void M(ref int refParameter)
        {
            refParameter += 42;
        }
        // </PassByReference>
    
        // <ReturnByReference>
        public ref int RefMax(ref int left, ref int right)
        {
            if (left > right)
            {
                return ref left;
            }
            else
            {
                return ref right;
            }
        }
        // </ReturnByReference>
    
        // <LocalRef>
        public void M2(int variable)
        {
            ref int aliasOfvariable = ref variable;
        }
        // </LocalRef> 
    
        // <ConditionalRef>
        public ref int RefMaxConditions(ref int left, ref int right)
        {
            ref int returnValue = ref left > right ? ref left : ref right;
            return ref returnValue;
        }
        // </ConditionalRef>
    }
    
    // <SnippetRefStruct>
    public ref struct CustomRef
    {
        public ReadOnlySpan<int> Inputs;
        public ReadOnlySpan<int> Outputs;
    }
    // </SnippetRefStruct>
    
    // <SnippetRefField>
    public ref struct RefFieldExample
    {
        private ref int number;
    }
    // </SnippetRefField>
    
    // <SnippetRefGeneric>
    class RefStructGeneric<T, S>
        where T : allows ref struct
        where S : T
    {
        // etc
    }
    // </SnippetRefGeneric>
  • In a ref struct definition, to declare a ref field. For more information, see the ref fields section of the ref structure types article.

    sample · csharpdocs/csharp/language-reference/keywords/snippets/refKeyword.csSnippetRefField

    csharp
    public class RefKeywordExamples
    {
        // <PassByReference>
        public void M(ref int refParameter)
        {
            refParameter += 42;
        }
        // </PassByReference>
    
        // <ReturnByReference>
        public ref int RefMax(ref int left, ref int right)
        {
            if (left > right)
            {
                return ref left;
            }
            else
            {
                return ref right;
            }
        }
        // </ReturnByReference>
    
        // <LocalRef>
        public void M2(int variable)
        {
            ref int aliasOfvariable = ref variable;
        }
        // </LocalRef> 
    
        // <ConditionalRef>
        public ref int RefMaxConditions(ref int left, ref int right)
        {
            ref int returnValue = ref left > right ? ref left : ref right;
            return ref returnValue;
        }
        // </ConditionalRef>
    }
    
    // <SnippetRefStruct>
    public ref struct CustomRef
    {
        public ReadOnlySpan<int> Inputs;
        public ReadOnlySpan<int> Outputs;
    }
    // </SnippetRefStruct>
    
    // <SnippetRefField>
    public ref struct RefFieldExample
    {
        private ref int number;
    }
    // </SnippetRefField>
    
    // <SnippetRefGeneric>
    class RefStructGeneric<T, S>
        where T : allows ref struct
        where S : T
    {
        // etc
    }
    // </SnippetRefGeneric>
  • In a generic type declaration to specify that a type parameter allows ref struct types.

    sample · csharpdocs/csharp/language-reference/keywords/snippets/refKeyword.csSnippetRefGeneric

    csharp
    public class RefKeywordExamples
    {
        // <PassByReference>
        public void M(ref int refParameter)
        {
            refParameter += 42;
        }
        // </PassByReference>
    
        // <ReturnByReference>
        public ref int RefMax(ref int left, ref int right)
        {
            if (left > right)
            {
                return ref left;
            }
            else
            {
                return ref right;
            }
        }
        // </ReturnByReference>
    
        // <LocalRef>
        public void M2(int variable)
        {
            ref int aliasOfvariable = ref variable;
        }
        // </LocalRef> 
    
        // <ConditionalRef>
        public ref int RefMaxConditions(ref int left, ref int right)
        {
            ref int returnValue = ref left > right ? ref left : ref right;
            return ref returnValue;
        }
        // </ConditionalRef>
    }
    
    // <SnippetRefStruct>
    public ref struct CustomRef
    {
        public ReadOnlySpan<int> Inputs;
        public ReadOnlySpan<int> Outputs;
    }
    // </SnippetRefStruct>
    
    // <SnippetRefField>
    public ref struct RefFieldExample
    {
        private ref int number;
    }
    // </SnippetRefField>
    
    // <SnippetRefGeneric>
    class RefStructGeneric<T, S>
        where T : allows ref struct
        where S : T
    {
        // etc
    }
    // </SnippetRefGeneric>